Page 1 of 1
[Solved] Modifying underground belts
Posted: Mon Feb 04, 2019 1:08 am
by ethical_apple
Hello, I'm trying to create a mod that has a higher maximum length for new underground belts. However, I'm encountering an issue with the internal and prototype name of "underground-belt" which is not recognized properly due to having the hyphen.
A makeshift solution was:
Code: Select all
data.raw["underground-belt"]["underground-belt"].max_distance = 64
however this directly modifies existing belts, while I want to make new ones altogether.
When trying
Code: Select all
local underground-belt = table.deepcopy(data.raw.underground-belt["underground-belt"])
the hyphen messes with it and it can't be read.
Lua claims there's an unexpected symbol, which is obviously the hyphen, so how can I fix this? Sorry if it's a no-brainer, I'm still new to Lua in general.
Re: Modifying underground belts
Posted: Mon Feb 04, 2019 7:26 am
by eduran
Square brackets are the default way to access table elements by key. The dot syntax only works if the key is string consisting only of alphanumeric characters and underscores. The first character has to be a letter or underscore, not a number. The same is true for variable names.
Code: Select all
local t = {}
t[1] = 1
t["hi"] = 2
t["a-b"] = 3
-- this works:
game.print(t.hi) --> 2
-- this does not work:
game.print(t.1)
game.print(t.a-b)
local underground-belt = 1 -- lua interprets the hyphen as minus and does not know what to do with this statement
-- instead use
game.print(t[1]) --> 1
game.print(t["a-b"]) -->3
local underground_belt
In your case, lua reads the hyphen as a minus. What you are looking for is
Code: Select all
local name_without_special_characters = table.deepcopy(data.raw["underground-belt"]["underground-belt"])
Re: Modifying underground belts
Posted: Mon Feb 04, 2019 8:05 am
by darkfrei
Just use "local underground_belt" instead of "local underground-belt".
Re: Modifying underground belts
Posted: Sat Feb 09, 2019 2:51 am
by ethical_apple
eduran wrote: Mon Feb 04, 2019 7:26 am
Square brackets are the default way to access table elements by key. The dot syntax only works if the key is string consisting only of alphanumeric characters and underscores. The first character has to be a letter or underscore, not a number. The same is true for variable names.
Code: Select all
local t = {}
t[1] = 1
t["hi"] = 2
t["a-b"] = 3
-- this works:
game.print(t.hi) --> 2
-- this does not work:
game.print(t.1)
game.print(t.a-b)
local underground-belt = 1 -- lua interprets the hyphen as minus and does not know what to do with this statement
-- instead use
game.print(t[1]) --> 1
game.print(t["a-b"]) -->3
local underground_belt
In your case, lua reads the hyphen as a minus. What you are looking for is
Code: Select all
local name_without_special_characters = table.deepcopy(data.raw["underground-belt"]["underground-belt"])
Thank you very much. Your solution works and you taught me something along the away.