[Solved] Modifying underground belts

Place to get help with not working mods / modding interface.
Post Reply
ethical_apple
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Jan 27, 2019 8:24 pm
Contact:

[Solved] Modifying underground belts

Post 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.
Last edited by ethical_apple on Sat Feb 09, 2019 2:48 am, edited 1 time in total.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: Modifying underground belts

Post 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"])

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Modifying underground belts

Post by darkfrei »

Just use "local underground_belt" instead of "local underground-belt".

ethical_apple
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Jan 27, 2019 8:24 pm
Contact:

Re: Modifying underground belts

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

Post Reply

Return to “Modding help”