Page 1 of 1
[Done] Create Table Question
Posted: Tue Jun 04, 2019 7:56 pm
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,.
Re: Create Table Question
Posted: Tue Jun 04, 2019 8:09 pm
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
Re: Create Table Question
Posted: Tue Jun 04, 2019 9:32 pm
by TheSAguy
Thanks Bilka!
Re: [Done] Create Table Question
Posted: Wed Jun 05, 2019 2:15 pm
by TheSAguy
Is it possible to create a global table in the Data.lua stage, not Control?
Re: [Done] Create Table Question
Posted: Wed Jun 05, 2019 2:30 pm
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")
Re: [Done] Create Table Question
Posted: Wed Jun 05, 2019 8:56 pm
by TheSAguy
Thanks for the information Deadlock.
I did not know all this!
Re: [Done] Create Table Question
Posted: Wed Jun 05, 2019 9:39 pm
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.