Page 1 of 1

onsave function, getting the directory?

Posted: Fri Mar 01, 2013 11:07 am
by ficolas
Is there any way I can get the directory of the save, with the onsave function or something?
I need to manage a list of entities placed by the player, I made the code, and it stores it in a table, until the game calls the onsave event, when this event is called, the code edits a txt file, but the directory of the txt file is... the desktop .-.
So I need to know the directory where the save is to be able to have more than one save at the same time.

Re: onsave function, getting the directory?

Posted: Fri Mar 01, 2013 11:18 am
by kovarex

Code: Select all

-- function called before game is saved
game.onsave = function()
end

Code: Select all

-- function called after game was loaded
game.onload = function()
end
You usualy don't need to use this, as long as you attach your data to glob.
glob is special global variable and all of its contents are saved/loaded automatically.
So as long as you store your table this way:

Code: Select all

glob.mytable = {}
glob.mytable["hello] = 15
You don't have to worry about onsave/onload.

Re: onsave function, getting the directory?

Posted: Fri Mar 01, 2013 11:21 am
by ficolas
So I can trash the code...
but anyways, is a much easier way of doing it :D

Re: onsave function, getting the directory?

Posted: Mon Mar 11, 2013 2:13 pm
by drs9999
Actually I have a similar problem.
And I cant confirm that it is solved by using glob.*
For example my treefarm-mod uses a glob.field {} to store the overall placed treefarm- entities. After restarting factorio and loading the savefile, the treefarms wont work anymore because the glob.field{} is empty ...

Re: onsave function, getting the directory?

Posted: Mon Mar 11, 2013 2:23 pm
by rk84
drs9999 wrote:Actually I have a similar problem.
And I cant confirm that it is solved by using glob.*
For example my treefarm-mod uses a glob.field {} to store the overall placed treefarm- entities. After restarting factorio and loading the savefile, the treefarms wont work anymore because the glob.field{} is empty ...
I just checked you freegame.lua

Its empty because onload sets it to empty.

Code: Select all

...
game.onload = function()
  glob.player = game.getplayer()
  glob.field = {}
  glob.fieldcount = 0
  glob.tickCount = 0
end...
btw you can get field count from table size( # glob.field ) or from game's entity counter ( glob.player.force.getentitycount("field-entityname-here") )

Re: onsave function, getting the directory?

Posted: Mon Mar 11, 2013 3:04 pm
by drs9999
oh boy...
I had to insert this stuff into the onloadfunction because otherwise my game crashes.
But it turns out that I used a 0.2.10-build that was released before it 0.2.10 becomes the actual stable build.

Now everything works fine as far as I can see (if I delete the onload function).

How embarrassing....

Thank you very much, its the first time I am using LUA so I am not very familar with it and if you have more suggestions please post it into the specific thread, that will help me a lot.