onsave function, getting the directory?

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:

onsave function, getting the directory?

Post 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.

kovarex
Factorio Staff
Factorio Staff
Posts: 8194
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: onsave function, getting the directory?

Post 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.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: onsave function, getting the directory?

Post by ficolas »

So I can trash the code...
but anyways, is a much easier way of doing it :D

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: onsave function, getting the directory?

Post 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 ...

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: onsave function, getting the directory?

Post 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") )
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: onsave function, getting the directory?

Post 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.

Post Reply

Return to “Modding help”