Jump to content

List of valid items


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
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.

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...