Hi there, i'm currently trying to create an object that spawns an item on use, but rather than having a set item or a bunch of if statements in the LUA I would like to use a .treasure file to select which item to spawn. Is there an existing function for this, and if not is there some kind of way of coding it to work, since i'm not that experienced with LUA. Thanks in advance!
This is a good point at which to learn something new about Lua. Lua doesn't have arrays or hashmaps or treenodes, instead it has one feature that that sort of stands in for all of those features at once: tables. Quick guide
Well I get the general idea behind them being mostly similar to list variables in the code i'm used to working with (BYOND code which i'm told is similar to C#), my main problems are: 1) How to get the LUA code to draw from info contained in a .treasure file, and 2) Figuring out the probability stuff - I know the indexes/associations in each member of a treasure drop pool all have to add up to one and then the game rolls to decide which one drops this time, but i'm not sure if there's some inherent function that does this, or if not how to write one that can do this. It seems to be handled inherently in BYOND so i'm kind of out of luck there.
Truthfully, I have no idea how to get your scripts to access JSONs outside of the one it's created for, or if it's even possible. My suggestion was more along the lines of re-implementing your treasurepools as a table within the script itself.
Well here's a snippet from what i'm writing: Code: elseif whichitem == 2 then -- stimpacks local stims = { "redstim", "yellowstim", "greenstim", "bluestim" } for i = math.random(2,10),0,-1 do world.spawnItem(stims[math.random(1,#stims)], entity.position(), 1) So I kinda already do that a bit! Mostly i'm just not sure how to weight the members of the list so some are more likely than others to appear. I'm mostly just trying to keep it elegant and not a giant hell of if/else chains, haha.
Oh, hey. You know more things than I had assumed. Noice. Well, you can implement something akin to a switch statement, as explained here. More code, but more efficient than a massive elseif chain. This isn't exactly elegant, but perhaps try getting a random from 1-100 and testing it against a table. 1-5 can be item A. 6-30 can be item B; item A will drop less often than item B.