@@ -2,25 +2,22 @@ require('mod-gui')
22local Entity = require (" stdlib/entity/entity" )
33local Surface = require (" stdlib/surface" )
44local microcontroller = require (' microcontroller' )
5+ local Event = require (' stdlib/event/event' )
56require (' stdlib/string' )
67require (' constants' )
78require (' stdlib/area/tile' )
89
10+
911function get_player_data (player_index )
10- if global .player_data == nil then
11- global .player_data = {}
12- end
1312 local player_data = global .player_data [player_index ] or {}
1413 return player_data
1514end
1615
1716function set_player_data (player_index , data )
18- if global .player_data == nil then
19- global .player_data = {}
20- end
2117 global .player_data [player_index ] = data
2218end
2319
20+
2421-- Handle MicroController OPEN event.
2522script .on_event (" open-gui" , function (event )
2623 local player_index = event .player_index
302299
303300local update_tick_time = settings .startup [" mc_update_tick_time" ].value
304301script .on_nth_tick (update_tick_time , function ()
305- -- Ensure we have a table to store microcontrollers in the global state.
306- if not global .microcontrollers then
307- global .microcontrollers = {}
308- end
309-
310302 local do_update_gui = true
311303 if update_tick_time < 30 then
312304 do_update_gui = (game .tick % 60 == 0 )
@@ -459,7 +451,16 @@ function microcontrollerGui( player, entity )
459451 set_player_data (player .index , player_data )
460452end
461453
454+ function update_global_data ()
455+ global .microcontrollers = global .microcontrollers or {}
456+ global .player_data = global .player_data or {}
457+ end
458+
459+ Event .register (Event .core_events .init , update_global_data )
460+
462461script .on_configuration_changed (function (event )
462+ update_global_data ()
463+
463464 local mod_changes = event .mod_changes [" m-microcontroller" ]
464465 if not (mod_changes and mod_changes .old_version ) then return end
465466
0 commit comments