Page 2 of 2

Re: Spawn random items from a list?

Posted: Mon Nov 05, 2012 9:33 am
by antti
Batty wrote:You could try putting nil in the table. It might complain "attempt to reference nil" (or crash) or it might just spawn nothing, I haven't tried it.
You can handle the nils like this to dodge errors:

Code: Select all

stuff = {"throwing_axe", "long_sword", nil }
item = stuff[math.random(1,#stuff)]
if item then
  -- spawn item here
end
The essential bit here being the "if item". It just checks if item returns anything (anything besides nil that is), thus avoiding running the code inside the if-then conditional if the item is nil. But Batty's second proposal is what I'd probably use too: that would enable me to add more items to the loot table without needing to add more nils when trying to keep the ratio of found items similar.

Re: Spawn random items from a list?

Posted: Mon Nov 05, 2012 9:44 am
by petri
One peculiarity with Lua is that the table length operator # does not work with tables with 'holes'. So if you have a table with nils, # does not return what you expect. In this case, you can use another value such as 0 to represent missing items.

For example,

Code: Select all

stuff = { "throwing_axe", 0, "long_sword" }
item = stuff[math.random(1,#stuff)]
if item ~= 0 then
 -- spawn item here
end

Re: Spawn random items from a list?

Posted: Mon Nov 05, 2012 9:47 am
by antti
Ah, good to know.

Re: Spawn random items from a list?

Posted: Mon Nov 05, 2012 11:39 am
by ZeroAlligator
Lark created a cryogenic chamber that used a similar concept, here is his code so that you can see how he made this work:

Code: Select all

list = {                        --a list of all possible monsters to spawn randomly.  Note:  groups are not supported; one occupant per chamber, please!
  "crab", 
  "crowern", 
  "goromorg", 
  "green_slime",
  "herder", 
  "herder_big", 
  "herder_small", 
  "ogre", 
  "scavenger", 
  "shrakk_torr", 
  "skeleton_archer", 
  "skeleton_warrior", 
  "snail",
  "spider", 
  "uggardian", 
  "warden", 
  "wyvern"}
  --
  ---  Initial start - only runs once to spawn or detect initial occupant and to give it items randomly  
  --
  if initial then
    initial = false

    if # monsters > 0 then
      occupant = monsters[1]
    else
      if occupant == "random" then
        if random_nil then local plus = 1 else plus = 0 end
        occupant = list[math.random(1, # list + plus)]
        end
      if occupant ~= nil then
        monster = spawn(occupant, self.level, self.x, self.y, self.facing) 
        monster:setLevel(rank)
        monsters = findAll(self.level, self.x, self.y, {":setAIState"})
        workable = findAll(self.level, self.x, self.y, {":addItem", ":setAIState"}, "all")
        end
      end
Stuff like initial and occupant are defined elsewhere in Lark's code, feel free to check his stuff out here.

-Mark

Re: Spawn random items from a list?

Posted: Mon Nov 05, 2012 3:55 pm
by Batty
oooh, scripting goodness in this thread now.

Re: Spawn random items from a list?

Posted: Mon Nov 05, 2012 10:17 pm
by Stevedog
Thank you everyone. Especially to Batty for pointing out the obvious solution with the second example. That was originally what I was planning to do but completely forgot about it when I implemented the item array.

Oh I got Antti and Petri both in here, Score!