[0.16.20] Adding new global variable

Place to get help with not working mods / modding interface.
User avatar
darius456
Fast Inserter
Fast Inserter
Posts: 222
Joined: Thu Jan 02, 2014 6:33 am
Contact:

[0.16.20] Adding new global variable

Post 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?
Lenovo Y580 8GB Ram GF660m 128GB SSD W7
Rseding91
Factorio Staff
Factorio Staff
Posts: 14631
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.16.20] Adding new global variable

Post 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.
If you want to get ahold of me I'm almost always on Discord.
User avatar
darius456
Fast Inserter
Fast Inserter
Posts: 222
Joined: Thu Jan 02, 2014 6:33 am
Contact:

Re: [0.16.20] Adding new global variable

Post 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.
Lenovo Y580 8GB Ram GF660m 128GB SSD W7
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: [0.16.20] Adding new global variable

Post 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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
Post Reply

Return to “Modding help”