Page 1 of 1

Help with initialicing table outside events.

Posted: Sat Oct 26, 2013 8:08 pm
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.

Re: Help with initialicing table outside events.

Posted: Sat Oct 26, 2013 8:27 pm
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...

Re: Help with initialicing table outside events.

Posted: Sat Oct 26, 2013 9:54 pm
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()"

Re: Help with initialicing table outside events.

Posted: Sat Oct 26, 2013 10:25 pm
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"}.

Re: Help with initialicing table outside events.

Posted: Sun Oct 27, 2013 1:25 am
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)