[Done] Create Table Question

Place to get help with not working mods / modding interface.
Post Reply
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Done] Create Table Question

Post by TheSAguy »

Hi,

Say I have the following:

Code: Select all

for i=1,20 do
	local Item = table.deepcopy(data.raw.item["iron-chest"])
	Item .name="bio-"..Item .name.."-"..i
	data:extend({Item})
end
I have a table, created_items and I want it to look like this:
global.Created_Items = {
"bio-iron-chest-1",
"bio-iron-chest-2",
"bio-iron-chest-3",
"bio-iron-chest-4",
---
"bio-iron-chest-20
}

How would I populate the table dynamically with the item names created?

Thanks,.
Last edited by TheSAguy on Tue Jun 04, 2019 9:32 pm, edited 1 time in total.

Bilka
Factorio Staff
Factorio Staff
Posts: 3157
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Create Table Question

Post by Bilka »

Code: Select all

local foo = {}
for i=1,20 do
	local Item = table.deepcopy(data.raw.item["iron-chest"])
        local name = "bio-"..Item .name.."-"..i
	Item.name = name
        foo[#foo+1] = name
	data:extend({Item})
end
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Create Table Question

Post by TheSAguy »

Thanks Bilka!

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] Create Table Question

Post by TheSAguy »

Is it possible to create a global table in the Data.lua stage, not Control?

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: [Done] Create Table Question

Post by Deadlock989 »

TheSAguy wrote:
Wed Jun 05, 2019 2:15 pm
Is it possible to create a global table in the Data.lua stage, not Control?
No. But you don't need to. Any variable declared without local scope is accessible to all mods during each of the data stages.

So if I declare a table of functions in data.lua:

Code: Select all

deadlock = {} -- not local
function deadlock.hair_styling(parameters) ... end
function deadlock.shampooing(parameters) ... end
function deadlock.buzzcut(parameters) ... end
then any other mod that loads after mine can access those functions in their own data.lua code. Users should check they exist before calling them, obviously.

Alternatively you can set up a separate file:

Code: Select all

-- sometable.lua
local important_table = {
	biscuits = 79,
	oranges = 23,
	cheese_on_toast = 54,
}
return important_table
and then any mod can load up that table at any time with a require:

Code: Select all

-- mymod.lua
local my_important_table = require("__DeadlockImportantTables__.prototypes.sometable")
Image

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] Create Table Question

Post by TheSAguy »

Thanks for the information Deadlock.
I did not know all this!

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: [Done] Create Table Question

Post by Deadlock989 »

Just to avoid misleading - you don't need the __ModName__ prefix in a require() if it's a file from within the same mod, only if it's a file from some other mod. But you can use this method to have "library" mods which provide functions and/or data tables to a bunch of other mods.
Image

Post Reply

Return to “Modding help”