Page 1 of 2
Spawn random items from a list?
Posted: Mon Oct 15, 2012 5:33 am
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.
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 6:00 am
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).
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 6:04 am
by Stevedog
Thanks! That helps immensely.
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 2:15 pm
by Huder
#stuff
What this hash means?
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 2:30 pm
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)
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 3:13 pm
by HaunterV
oh?
# is a shortcut? here i was modifying the tome library script manually...
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 3:19 pm
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.
Re: Spawn random items from a list?
Posted: Mon Oct 15, 2012 11:48 pm
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!
Re: Spawn random items from a list?
Posted: Mon Nov 05, 2012 4:38 am
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.
Re: Spawn random items from a list?
Posted: Mon Nov 05, 2012 5:24 am
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.