Help with initialicing table outside events.

Place to get help with not working mods / modding interface.
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Help with initialicing table outside events.

Post by ficolas »

I have this bit of code

Code: Select all

glob.boilernames={}

remote.addinterface("F-mod", {
	addboiler=function(boilername)
		table.insert(glob.boilernames,boilername)
		game.getplayer().print("Boiler added")
	end 
})
	
remote.call("F-mod", "addboiler", "boiler")
Then, the table is readed at a line, but it allways says it is nil, it works fine in newly created games, but when I use an old save, it doesnt work.
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Help with initialicing table outside events.

Post by drs9999 »

I am not entirely sure,but have you tried to update you modversion?
From my understanding everything in the onInit and outside everyother event is just called once(when you start a new game)
or if you update the modversion.

But like I said I am not sure but it is worth a try, I guess...
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Help with initialicing table outside events.

Post by FreeER »

drs9999 wrote:[...]outside everyother event is just called once(when you start a new game)[...]
You can quickly test this by placing game.player.print("test") on the first line of control.lua. When you do you'll find that it runs once when the game is created and again every time you load the save (which makes sense).
ficolas wrote:but when I use an old save, it doesnt work.
Hm, assuming that you are not building new boilers in the old saves it is possible that glob.boilernames={} is being ran when the save is loaded and deleting everything in the table (and maybe then lua's garbage collector is deleting the empty table?). Why not use glob.boilernames={} in game.oninit (also, not sure how Factorio reacts to a remote interface being called by the same script that created it)

edit: or perhaps you'd need a migration script with "game.reloadscript()"
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Help with initialicing table outside events.

Post by ficolas »

I alredy tried to print, and it works, but the table doesnt.
I also tried with a non empty table glob.boilernames={"boiler"}.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Help with initialicing table outside events.

Post by FreeER »

Why are you wanting the boiler names btw? You could always do something like this (it's how the blast-mining mod can affect mod generated resources (like quartz) as well as vanilla):

Code: Select all

game.oninit(function()
  glob.boilernames = {}
  for k, entity in pairs(game.entityprototypes) do --there is also an item prototypes if you needed it
    if entity.type == "boiler" then
      local boiler = {name=entity.name}
      table.insert(glob.boilernames, boiler)
    end
  end
end)

game.onevent(defines.event.onentitybuilt, function()
  local isboiler = false
  for k, boiler in pairs(glob.boilernames) do 
    if event.entity.name == boiler.name then
      isboiler = true
      break
    end
  end
  if isboiler then
  --code to add boiler entity to tables used in checking for salt extractors (or whatever)
  end
end)
This is actually probably neater than using mod interfaces (though it doesn't allow as much interaction obviously)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
Post Reply

Return to “Modding help”