Jump to content

Recommended Posts

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

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 by Mobbstar

Yayy ! Thanks a lot :grin:

 

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 by pyrovoice

Yayy ! Thanks a lot :grin:

 

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 key

math.random( #myTable )

--successfully picking random elements

myTable[ math.random( #myTable ) ]

 

EDIT: If you want to spawn all items, you should use pairs()

Edited by Mobbstar

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

 

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:

  1. prefabs (items, etc.) don't have a variable "name", but a variable "prefab". That's the one you're trying to use.
  2. "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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...