Spawn random items from a list?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: Spawn random items from a list?

Post 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.
Steven Seagal of gaming industry
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Spawn random items from a list?

Post 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
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: Spawn random items from a list?

Post by antti »

Ah, good to know.
Steven Seagal of gaming industry
User avatar
ZeroAlligator
Posts: 5
Joined: Sun Nov 04, 2012 8:46 am

Re: Spawn random items from a list?

Post 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
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Spawn random items from a list?

Post by Batty »

oooh, scripting goodness in this thread now.
Stevedog
Posts: 17
Joined: Tue Mar 27, 2012 8:58 pm

Re: Spawn random items from a list?

Post 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!
Post Reply