Page 1 of 1

trying to modify unit-spawner type

Posted: Sun May 01, 2016 10:55 pm
by xattus
https://wiki.factorio.com/index.php?tit ... it-spawner

Based on this I'm trying to do

Code: Select all

data.raw.unit-spawner["biter-spawner"].resistances = 
	{
		{
		type = "explosion",
		decrease = 15,
		percent = 30
		},
		{
		type = "physical"
		decrease = 2,
		percent = 0
		}
	}
...but I get a syntax error at the hyphen (-). I have modded other types like "car" , "projectile" etc. with data.raw and it works just fine, but in this case I got stuck and I have no idea how to solve this.

Any help would be appreciated.

Re: trying to modify unit-spawner type

Posted: Sun May 01, 2016 11:19 pm
by prg
In case a table index is not a valid Lua identifier you need to use the square bracket syntax, like data.raw.["unit-spawner"]["biter-spawner"].

Re: trying to modify unit-spawner type

Posted: Mon May 02, 2016 12:45 pm
by xattus
Tried it before, but to make sure I gave it a go just now...

Code: Select all

data.raw.["unit-spawner"]["biter-spawner"].resistances = 
	{
		{
		type = "physical",
		decrease = 2,
		percent = 0
		}
	}
"name expected near ["

Re: trying to modify unit-spawner type

Posted: Mon May 02, 2016 12:48 pm
by daniel34
xattus wrote:Tried it before, but to make sure I gave it a go just now...

Code: Select all

data.raw.["unit-spawner"]["biter-spawner"].resistances = 
	{
		{
		type = "physical",
		decrease = 2,
		percent = 0
		}
	}
"name expected near ["
try it without the dot after raw:

Code: Select all

data.raw["unit-spawner"]["biter-spawner"].resistances = 

Re: trying to modify unit-spawner type

Posted: Mon May 02, 2016 6:24 pm
by xattus
^^
That did it. Thanks a lot! :D

Re: trying to modify unit-spawner type

Posted: Tue May 03, 2016 8:14 am
by bobingabout
As a small tutorial here...

There are 2 methods to index an array/table.

The first, which only works if the table value has a name, is to use the path method, or what I call the dot method.
The basics is that you use the name of the element directly, specified by use of a dot, followed by the name. However, only certain character are legal for this syntax (alpha-numeric only, and I'm fairly sure it can't start with a number).

data.raw.item.itemname.values etc...

The other method is indexing method as follows

data.raw[index][index].values etc...

The index can be one of two things.

In cases where the index has a name (like the Path method), you can use this name directly, in quotes, as follows. This is allowed even when illegal characters are used, such as - and +.

data.raw["item"]["itemname"].values etc...

In cases where this index is not known, which happens if you just open a new table with {, commonly when nesting certain things, such as on attack parameters, or animations using stripes, or layers, or maybe when iterating through entire lists, you can use the number to reference this table entry. The index is 1 based, which means the first entry is number 1. Some programming languages will use 0 based (first number is 0) tables instead, but everything in factorio LUA seems to be 1 based.

data.raw[1][1].values etc...

Although the above example is perfectly valid, I have no idea what I'm actually indexing there. It's simply referencing the first table in data.raw, and the first item in that table.

I hope this helps!

Re: trying to modify unit-spawner type

Posted: Tue May 03, 2016 9:20 am
by prg
Key 1 is not simply the same as the first key in a table. data.raw[1] doesn't exist, data.raw[1][1] will blow up since you're indexing nil.

Tables contain key = value mappings. There are different ways of defining tables. If you only specify the values,

Code: Select all

{
    "foo",
    "bar",
    "baz",
}
those values implicitly will have integer keys assigned, starting from 1. The same table with explicit keys would look like

Code: Select all

{
    [1] = "foo",
    [2] = "bar",
    [3] = "baz",
}
If you define a table like

Code: Select all

{
    ["a"] = "foo",
    ["b"] = "bar",
    ["c"] = "baz",
}
or with the short form

Code: Select all

{
    a = "foo",
    b = "bar",
    c = "baz",
}
there simply won't be a key 1.