Spawn random items from a list?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Stevedog
Posts: 17
Joined: Tue Mar 27, 2012 8:58 pm

Spawn random items from a list?

Post by Stevedog »

I'm wanting to spawn random items when a script is run. My initial thought was to generate a random number and then have a series of elseif statements.

if random_number == 1 then
spawn(item 1)
elseif random_number == 2 then
spawn (item 2)
ect..

But now I'm wondering if I can't just set up an array and have the script pull a random item from there. However I have not used LUA before and don't know if it can't be done or if I am just getting confused on syntax.
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Spawn random items from a list?

Post by Batty »

Do something like this:

Code: Select all

function randomStuff()

stuff = {"throwing_axe", "long_sword", "pitroot_bread", "leather_boots", "lurker_hood"}
spawn(stuff[math.random(1, #stuff)], level, x, y, facing)

end
stuff is a table, tables in lua start at 1 & not 0 like usual. #stuff is the # of elements in the table so math.random picks a number between 1 & size of stuff (5).
Stevedog
Posts: 17
Joined: Tue Mar 27, 2012 8:58 pm

Re: Spawn random items from a list?

Post by Stevedog »

Thanks! That helps immensely.
User avatar
Huder
Posts: 26
Joined: Tue Apr 24, 2012 11:23 am
Location: Poland Piła
Contact:

Re: Spawn random items from a list?

Post by Huder »

#stuff
What this hash means?
Huder's Assets newest: Wooden Shield
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Spawn random items from a list?

Post by Grimwold »

Huder wrote:
#stuff
What this hash means?
It means the number of items in table stuff.

Seems to be a shortcut for table.getn(stuff)
User avatar
HaunterV
Posts: 676
Joined: Mon Apr 16, 2012 9:54 pm
Location: Barrie, Ontario, Canada

Re: Spawn random items from a list?

Post by HaunterV »

oh?

# is a shortcut? here i was modifying the tome library script manually...
Grimrock Community 'FrankenDungeon 2012. Submit your entry now!: http://tinyurl.com/cnupr7h
SUBMIT YOUR ASSETS! Community Asset Pack (C.A.P.): http://tinyurl.com/bqvykrp
Behold! The HeroQuest Revival!: http://tinyurl.com/cu52ksc
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Spawn random items from a list?

Post by Grimwold »

HaunterV wrote:oh?

# is a shortcut? here i was modifying the tome library script manually...
Yeah you can use # to return the number of items in a table, or the number of characters in a sting it seems... great for looping through tables that you keep adding things to.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Spawn random items from a list?

Post by JohnWordsworth »

I know it's kind of moot for Grimrock Editing, but I seem to remember that table.getn() was deprecated and ceases to exist in Lua 5.2 (Grimrock uses 5.1, and even if they did upgrade, which is unlikely, then I think compatibility mode adds it back in). Anyway, it seems the hash is the preferred way going forward for people expecting to use Lua elsewhere!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Stevedog
Posts: 17
Joined: Tue Mar 27, 2012 8:58 pm

Re: Spawn random items from a list?

Post by Stevedog »

Okay, one more thing. I'm using this format:

stuff = {"throwing_axe", "long_sword", "pitroot_bread", "leather_boots", "lurker_hood"}
spawn(stuff[math.random(1, #stuff)], level, x, y, facing)

and I'm wondering if there is a way to put a blank or nothing object. I would like for the barrel to have either an item, monster or nothing. I have been using a counter item as it doesn't show in the game, but this seems like a kludge.
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Spawn random items from a list?

Post by Batty »

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.

Code: Select all

stuff = {"throwing_axe", "long_sword", nil, "pitroot_bread", "leather_boots", "lurker_hood"}
Other than that I'd just do:

Code: Select all

num = math.random()

if num < 0.3 then
   spawn item
   elseif num > 0.3 and num < 0.6 then
   spawn monster
end
You'd have roughly 30% chance of spawning either item, monster, or nothing.
Post Reply