Page 1 of 1

[0.16.20] Adding new global variable

Posted: Tue Jan 30, 2018 8:41 pm
by darius456
My on load function:

Code: Select all

script.on_load (function()
	init_all()

	if debug_timebuttons == 1 then
		for _, player in pairs(game.players) do
			if player.connected then
				player.print("on_load ", debug_color)
			end
		end
	end
	
end)
My init_all function:

Code: Select all

function init_all()
	
	if global.button_count == nil then
		global.button_count = 5
	end	
	
	if global.button_caption == nil then
		global.button_caption = {"x0.5", "x1", "x2", "x3", "x5"}
	end	
	
	if global.button_speed == nil then
		global.button_speed = {0.5, 1, 2 ,3 ,5}
	end	

	if global.button_menu_timer == nil then
		global.button_menu_timer = 0
	end
	
	if global.chspeedoncraftingonoff == nil then
		global.chspeedoncraftingonoff = false
	end
	
	if global.chspeedonminingonoff == nil then
		global.chspeedonminingonoff = false
	end
		
		
	if global.speedatcrafting == nil then
		global.speedatcrafting = 2
	end
	
	if global.speedatstartcrafting == nil then
		global.speedatstartcrafting = 0
	end
	
	if global.show_timer == nil then
		global.show_timer = 1
	end
 
end
When i add new global variable like this:

Code: Select all

   if global.newwariable == nil then
      global.newwariable = 1
   end
I load save game that has pravious version of my mod and this error occured: (when I create new game or load save withaut my mod no error)
Image

Am i doing something wrong with adding new variable?

Re: [0.16.20] Adding new global variable

Posted: Tue Jan 30, 2018 8:50 pm
by Rseding91
darius456 wrote:... Am i doing something wrong with adding new variable?
Thanks for the report and: yes.

You can't change any data in global in the on_load function - that's not what it's for.

I'm going to move this to modding help.

Re: [0.16.20] Adding new global variable

Posted: Tue Jan 30, 2018 10:04 pm
by darius456
Ok,

where should I update global.table except

Code: Select all

script.on_init(function())
I have tried in

Code: Select all

script.on_event(defines.events.on_tick, function(event)
,everything seems to work, but i don't know if it is a good idea.
.
.
.
OGM, so much changed in Factorio API since my last modding attempt. I found web page with Factorio API description and I have found "on_configuration_changed(f)". So everything is clear to me right now.

Re: [0.16.20] Adding new global variable

Posted: Wed Jan 31, 2018 9:11 am
by bobingabout
Also keep in mind that everything in global is saved, and loaded, so variables stored in there act as if the game just continued, and never was interrupted with a save and re-loaded.

When doing scripting, I typically create a table in global with on_init, and maybe check through it in on_configuration_changed.
Also for crashproofing check to see if the data I'm trying to access exists before trying to access it, such as this line would appear at the start of every script that checks player data:

Code: Select all

if global.players and global.players[event.player_index] then
Actual code is a bit more involved than that, such as actually re-running a global.players table creator if it isn't found, but that's just being overly redundant.