trying to modify unit-spawner type

Place to get help with not working mods / modding interface.
xattus
Inserter
Inserter
Posts: 24
Joined: Tue Apr 19, 2016 1:34 pm
Contact:

trying to modify unit-spawner type

Post 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.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: trying to modify unit-spawner type

Post 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"].
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
xattus
Inserter
Inserter
Posts: 24
Joined: Tue Apr 19, 2016 1:34 pm
Contact:

Re: trying to modify unit-spawner type

Post 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 ["
daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: trying to modify unit-spawner type

Post 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 = 
quick links: log file | graphical issues | wiki
xattus
Inserter
Inserter
Posts: 24
Joined: Tue Apr 19, 2016 1:34 pm
Contact:

Re: trying to modify unit-spawner type

Post by xattus »

^^
That did it. Thanks a lot! :D
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: trying to modify unit-spawner type

Post 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!
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: trying to modify unit-spawner type

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Post Reply

Return to “Modding help”