pyrovoice Posted May 28, 2015 Share Posted May 28, 2015 Hi guys, i'm making an item that can pop a lrge number of resources, each of them having a custom number of copies and a time. I wrote this : local multiplicables ={ CUTGRASS(elementName="cutgrass", numberCreated=5, timeToCreate=10), LOG(elementName="log", numberCreated=3, timeToCreate=10)} But it threw an error. Would you know how I write a list of spawnable prefabs and retains the number of copies and time informations ? Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/ Share on other sites More sharing options...
pyrovoice Posted May 28, 2015 Author Share Posted May 28, 2015 this list is supposed to show which elements this can duplicate, and keep their informations* Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641627 Share on other sites More sharing options...
Mobbstar Posted May 28, 2015 Share Posted May 28, 2015 (edited) Hi guys, i'm making an item that can pop a lrge number of resources, each of them having a custom number of copies and a time. I wrote this : local multiplicables ={ CUTGRASS(elementName="cutgrass", numberCreated=5, timeToCreate=10), LOG(elementName="log", numberCreated=3, timeToCreate=10)} But it threw an error. Would you know how I write a list of spawnable prefabs and retains the number of copies and time informations ? You accidentally used class syntax, try table syntax. local multiplicables ={ cutgrass = {elementName="cutgrass", numberCreated=5, timeToCreate=10}, log = {elementName="log", numberCreated=3, timeToCreate=10}} You can remove the field "elementName" entirely in that case, since the key is the name already. Or you leave the string keys out to make a numeric array:local multiplicables ={ {elementName="cutgrass", numberCreated=5, timeToCreate=10}, --key is 1 {elementName="log", numberCreated=3, timeToCreate=10} --key is 2} EDIT: May I suggest this table tutorial? Classes are some kind of mad hack that isn't standard Lua, but they're essentially duplicatable tables. Edited May 28, 2015 by Mobbstar Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641628 Share on other sites More sharing options...
pyrovoice Posted May 28, 2015 Author Share Posted May 28, 2015 (edited) Yayy ! Thanks a lot I'm quite new to LUA, do you know how I should access the element ? Given this : local multiplicables ={ cutgrass = {"cutgrass", numberCreated=5, timeToCreate=10}, --key is 1 log = {"log", numberCreated=3, timeToCreate=10} --key is 2} can I do multiplicable.cutgrass ? Or do I do multiplicable[0].name, multiplicable[0].numberCreated ... Edited May 28, 2015 by pyrovoice Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641633 Share on other sites More sharing options...
Mobbstar Posted May 28, 2015 Share Posted May 28, 2015 (edited) Yayy ! Thanks a lot I'm quite new to LUA, do you know how I should access the element ? Given this : local multiplicables ={ cutgrass = {"cutgrass", numberCreated=5, timeToCreate=10}, --key is 1 log = {"log", numberCreated=3, timeToCreate=10} --key is 2} can I do multiplicable.cutgrass ? Or do I do multiplicable[0].name, multiplicable[0].numberCreated ... Yes, multiplicable.cutgrass works. But you misunderstood what I mean with "You can remove the field elementName", you left the strings in there (as said, they are automatically assigned to the key 1, because Lua prefers to start at 1). The alternative is to not set keys and make a list instead (as said above). If you want the items to be chosen randomly, that's the better option.In fact, here's a compact way to pick a random element from a table in Lua:--you can pick the index using "myTable[key]", similiar to Java--you can get the current count of elements in an array using #myTable (only lists without gaps work)--math.random(n) returns a random number from (including) 1 to (including) n--thus, the following gets a random number keymath.random( #myTable )--successfully picking random elementsmyTable[ math.random( #myTable ) ] EDIT: If you want to spawn all items, you should use pairs() Edited May 28, 2015 by Mobbstar Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641640 Share on other sites More sharing options...
pyrovoice Posted May 28, 2015 Author Share Posted May 28, 2015 ok, I could get it to work. I did : local multiplicables ={cutgrass = {debugName="cutgrass", numberCreated=5, timeToCreate=10},woodLog = {debugName="log", numberCreated=3, timeToCreate=10}} I kept the debug name since log seems to be used in LUA After that, I use it like this : -- Check if the item is in the list based on its namelocal function isMultiplicable(item) name = item.name for k,v in pairs(multiplicables) do if v.debugName == name then return v end end return nilend Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641647 Share on other sites More sharing options...
Mobbstar Posted May 28, 2015 Share Posted May 28, 2015 ok, I could get it to work. I did : local multiplicables ={cutgrass = {debugName="cutgrass", numberCreated=5, timeToCreate=10},woodLog = {debugName="log", numberCreated=3, timeToCreate=10}} I kept the debug name since log seems to be used in LUA After that, I use it like this : -- Check if the item is in the list based on its namelocal function isMultiplicable(item) name = item.name for k,v in pairs(multiplicables) do if v.debugName == name then return v end end return nilend Two minor things:prefabs (items, etc.) don't have a variable "name", but a variable "prefab". That's the one you're trying to use. "log" is the prefab as declared in the game code, that has nothing to do with Lua. The game Don't Starve merely uses that programming language for its scripts and data.Other than that, you're correct. Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641652 Share on other sites More sharing options...
Corrosive Posted May 28, 2015 Share Posted May 28, 2015 @Mobbstar, well it's technically used in math.log... Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641745 Share on other sites More sharing options...
Mobbstar Posted May 28, 2015 Share Posted May 28, 2015 @Mobbstar, well it's technically used in math.log... Well in that case, I'm all hyped for a mod that spawns me three logarithms at the price of one! I hope they're base e, those are the most confusing! Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641748 Share on other sites More sharing options...
Corrosive Posted May 28, 2015 Share Posted May 28, 2015 @Mobbstar, They always felt so natural to me. Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641803 Share on other sites More sharing options...
pyrovoice Posted May 29, 2015 Author Share Posted May 29, 2015 Link to comment https://forums.kleientertainment.com/forums/topic/54521-list-of-valid-items/#findComment-641893 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now