Page 1 of 1

How to reference entries in data.raw? (locomotive)

Posted: Sat May 13, 2017 7:53 pm
by Krayt
Hello,

Currently I want to continue making some Factorio mods of minor complexity after having not made a Factorio mod for a year or so. Back then when I made my first small Factorio mods I used to manually copy the existing entries within the *.lua files in the folder "prototypes" and then make some changes to the properties.

This time I used the following tutorial to get back to modding: https://wiki.factorio.com/User:Gangsir/ ... e_creation

In the tutorial a "deepcopy" of an existing data-entry is assigned to a local variable. When trying to start Factorio using the newly created "mod" without real functionality, I am getting the following error:
Error loading mods wrote:

Code: Select all

Failed to load mods: __ArmoredTrain__/data.lua:1: __ArmoredTrain__/prototypes/item.lua:3: attempt to index local 'armoredTrain' (a nil value)
The source code of my mod's item.lua:
item.lua
As far as I understand the situation is comparable to what would be a NullPointerException in Java. So I assume my reference to the "diesel-locomotive" in data.raw is not correct. That's why I can't access attributes of my local variable armoredTrain.

So far my google searches using various search terms didn't get me any further.

It would be good, if some modder (or other user) on the forums could point me to the right direction like a current overview about how and where to access entries in data.raw .

Thanks in advance,

Krayt

Re: How to reference entries in data.raw?

Posted: Sat May 13, 2017 8:06 pm
by prg
Have a look at data/base/prototypes/item/item.lua. "diesel-locomotive" got renamed to just "locomotive".

Re: How to reference entries in data.raw?

Posted: Sat May 13, 2017 8:07 pm
by daniel34
You are using old code to copy the locomotive, the proper code would be

Code: Select all

local xyz = table.deepcopy(data.raw["item-with-entity-data"]["locomotive"])
The "item-with-entity-data" was implemented in 0.13 I think and the (diesel) locomotive was renamed to just "locomotive" in 0.15.

You can use the *.lua files in the prototypes folder to check what the type/name is supposed to be.

Re: How to reference entries in data.raw?

Posted: Sat May 13, 2017 8:13 pm
by Krayt
prg wrote:Have a look at data/base/prototypes/item/item.lua. "diesel-locomotive" got renamed to just "locomotive".
Thank you. Probably I was confused by outdated information on the internet and the fact that the image names etc. still are "diesel-locomotive[...]". Furthermore I had used the wrong type instead of "item-with-entity-data".
Just to have the working code here for future searchers:

Code: Select all

local armoredTrain = table.deepcopy(data.raw["item-with-entity-data"]["locomotive"])

armoredTrain["name"] = "armored-train"
armoredTrain["order"] = "a[train-system]-g[diesel-locomotive]"
armoredTrain["place_result"] = "locomotive"
armoredTrain["stack_size"] = 5

local recipe = table.deepcopy(data.raw["recipe"]["locomotive"])

recipe.enabled = true
recipe.ingredients = {{"locomotive", 2}, {"steel-plate", 200}}
recipe.result = "armored-train"

data:extend({armoredTrain, recipe})
Greetings,

Krayt

//Edit: And daniel thank you too.