Add a property to entity definition (resolved)

Place to get help with not working mods / modding interface.
Post Reply
User avatar
kirazy
Filter Inserter
Filter Inserter
Posts: 416
Joined: Tue Mar 06, 2018 12:18 am
Contact:

Add a property to entity definition (resolved)

Post by kirazy »

So new in 0.18 is the splitter property "structure_patch", which for whatever reason is not on the wiki yet.

I'm trying to update my mod that reskins Bob's basic splitter, only he hasn't chosen to implement structure_patch (which would involve using the new sprites from 0.18, where they split east/west splitters into top/bottom sprite sheets).

There MUST be a way to add the "structure_patch" property, but I'm not sure how to do it.

I had been trying to use this:

Code: Select all

table.insert(data.raw["splitter"]["basic-splitter"], 
{
	structure_patch = 
	{
		north = ...
		east = ...
		south = ...
		west = ...
	}
})
But this, while the game loads, has the effect of adding it nested in curly brackets, e.g.

Code: Select all

["basic-splitter"] = {
      {
        structure_patch = { ...

        }
      },
      animation_speed_coefficient = 32,
      belt_animation_set = nil,
      collision_box = ...
Which buries it and so it's not read. Without wrapping structure_patch = ... in curly brackets, the game doesn't load.

I would like to avoid simply recreating the entity; how to do this properly?
Last edited by kirazy on Sun Feb 02, 2020 3:29 am, edited 1 time in total.

Honktown
Smart Inserter
Smart Inserter
Posts: 1029
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Add a property to entity definition

Post by Honktown »

1) dumb question, what does 0.18 do to make structure_patch work? You may have to hand-edit sprites to be compatible.

2) table.insert inserts a table into the first numbered position which is nil, starting at 1. It inserts at position #table + 1 if there's numbered entries (# gets the number of elements from 1 to the first number that doesn't have nil). If you're trying to add a new table, you have to do thing.exactname = new_table if you want exactname to be the name (literally) and new_table is the variable holding the table, or thing[new_table_key] = whats_storing_your_table if the name string is stored in a variable. If exactname has symbols that can't be part of the thing.namestring sugar (alphabet, number*, or underscore), then you need to do ["this-name-with-symbolsandhyphens@@"].
I have mods! I guess!
Link

User avatar
kirazy
Filter Inserter
Filter Inserter
Posts: 416
Joined: Tue Mar 06, 2018 12:18 am
Contact:

Re: Add a property to entity definition

Post by kirazy »

Honktown wrote:
Sun Feb 02, 2020 2:51 am
1) dumb question, what does 0.18 do to make structure_patch work? You may have to hand-edit sprites to be compatible.

2) table.insert inserts a table into the first numbered position which is nil, starting at 1. It inserts at position #table + 1 if there's numbered entries (# gets the number of elements from 1 to the first number that doesn't have nil). If you're trying to add a new table, you have to do thing.exactname = new_table if you want exactname to be the name (literally) and new_table is the variable holding the table, or thing[new_table_key] = whats_storing_your_table if the name string is stored in a variable. If exactname has symbols that can't be part of the thing.namestring sugar (alphabet, number*, or underscore), then you need to do ["this-name-with-symbolsandhyphens@@"].
1. You tell me? The game implemented it, it's part of the base splitter prototype, and after figuring out my issue, works flawlessly.

2. Turns out I was trying to add a key, and that was as simple as:

Code: Select all

table.insert(data.raw["splitter"]["basic-splitter"],structure_patch)
And then I can just edit the key directly with

Code: Select all

data.raw["splitter"]["basic-splitter"].structure_patch = {...}
So with that done everything works properly and we're gravy. You've been... helpful.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2919
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Add a property to entity definition (resolved)

Post by Optera »

Honktown wrote:
Sun Feb 02, 2020 2:51 am
2. Turns out I was trying to add a key, and that was as simple as:

Code: Select all

table.insert(data.raw["splitter"]["basic-splitter"],structure_patch)
And then I can just edit the key directly with

Code: Select all

data.raw["splitter"]["basic-splitter"].structure_patch = {...}
So with that done everything works properly and we're gravy. You've been... helpful.
Redundant use of table.insert. table.insert is only useful when inserting into dense tables aka arrays.
The table you are adding to is sparse, the 2nd line is all you need.

PS:

Code: Select all

data.raw["splitter"]["basic-splitter"]["structure_patch"] = {...}
is the same command in uniform notation and a little easier to read than switching between notations.

User avatar
kirazy
Filter Inserter
Filter Inserter
Posts: 416
Joined: Tue Mar 06, 2018 12:18 am
Contact:

Re: Add a property to entity definition (resolved)

Post by kirazy »

Optera wrote:
Sun Feb 02, 2020 8:36 am
Honktown wrote:
Sun Feb 02, 2020 2:51 am
2. Turns out I was trying to add a key, and that was as simple as:

Code: Select all

table.insert(data.raw["splitter"]["basic-splitter"],structure_patch)
And then I can just edit the key directly with

Code: Select all

data.raw["splitter"]["basic-splitter"].structure_patch = {...}
So with that done everything works properly and we're gravy. You've been... helpful.
Redundant use of table.insert. table.insert is only useful when inserting into dense tables aka arrays.
The table you are adding to is sparse, the 2nd line is all you need.

PS:

Code: Select all

data.raw["splitter"]["basic-splitter"]["structure_patch"] = {...}
is the same command in uniform notation and a little easier to read than switching between notations.
I can standardize the notation, but when you say the use is redundant...

Prior to the table.insert instruction the property "structure_patch" does not actually exist in that particular entity definition. The second line alone will throw an error. Bob hasn't updated his definitions for his basic belt splitters to include the new property, since he defines everything explicitly rather than copying from base and modifying as necessary. :c

If there's a way to add the key and the pair in the same step I'd love to know. I code as a hobby... :D


Edit: But I was curious and tested removing the table.insert line, and it works.

I'm kind of nonplussed. This gave me an astounding amount of trouble when I first tried to set it up. I'm preeeeetty sure I tried to do it this way the first time and it always complained the field didn't exist.

Honktown
Smart Inserter
Smart Inserter
Posts: 1029
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Add a property to entity definition (resolved)

Post by Honktown »

Missed your previous response :/ . Sorry.

Code: Select all

table.insert(data.raw["splitter"]["basic-splitter"],structure_patch)
That right there inserted what's in structure_patch at data.raw["splitter"]["basic-splitter"][1]. That's why you weren't finding it.

Code: Select all

data.raw["splitter"]["basic-splitter"].structure_patch = {...}
Both creates the key and defines it to contain the table.

Doing

Code: Select all

data.raw["splitter"]["basic-splitter"].structure_patch.thing = ...
Can't work unless structure_patch was already table, which never can happen with insert (insert can only insert at numbered positions).
I have mods! I guess!
Link

User avatar
kirazy
Filter Inserter
Filter Inserter
Posts: 416
Joined: Tue Mar 06, 2018 12:18 am
Contact:

Re: Add a property to entity definition (resolved)

Post by kirazy »

Yep, kinda was figuring that's what had happened. Much appreciate all the help, thanks. :)

Post Reply

Return to “Modding help”