Spawn random items from a list?
Spawn random items from a list?
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.
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?
Do something like this:
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).
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
Re: Spawn random items from a list?
Thanks! That helps immensely.
Re: Spawn random items from a list?
What this hash means?#stuff
Huder's Assets newest: Wooden Shield
Re: Spawn random items from a list?
It means the number of items in table stuff.Huder wrote:What this hash means?#stuff
Seems to be a shortcut for table.getn(stuff)
Puzzle Frameworks - http://www.grimrock.net/forum/viewtopic.php?f=14&t=4564
Magic Pack - https://www.nexusmods.com/grimrock/mods/70
Area of Effect Spell System - http://www.grimrock.net/forum/viewtopic ... 150#p44382
Magic Pack - https://www.nexusmods.com/grimrock/mods/70
Area of Effect Spell System - http://www.grimrock.net/forum/viewtopic ... 150#p44382
Re: Spawn random items from a list?
oh?
# is a shortcut? here i was modifying the tome library script manually...
# 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
SUBMIT YOUR ASSETS! Community Asset Pack (C.A.P.): http://tinyurl.com/bqvykrp
Behold! The HeroQuest Revival!: http://tinyurl.com/cu52ksc
Re: Spawn random items from a list?
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.HaunterV wrote:oh?
# is a shortcut? here i was modifying the tome library script manually...
Puzzle Frameworks - http://www.grimrock.net/forum/viewtopic.php?f=14&t=4564
Magic Pack - https://www.nexusmods.com/grimrock/mods/70
Area of Effect Spell System - http://www.grimrock.net/forum/viewtopic ... 150#p44382
Magic Pack - https://www.nexusmods.com/grimrock/mods/70
Area of Effect Spell System - http://www.grimrock.net/forum/viewtopic ... 150#p44382
- JohnWordsworth
- Posts: 1397
- Joined: Fri Sep 14, 2012 4:19 pm
- Location: Devon, United Kingdom
- Contact:
Re: Spawn random items from a list?
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.
Re: Spawn random items from a list?
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.
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?
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.
Other than that I'd just do:
You'd have roughly 30% chance of spawning either item, monster, or nothing.
Code: Select all
stuff = {"throwing_axe", "long_sword", nil, "pitroot_bread", "leather_boots", "lurker_hood"}
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