mickael9 / cut-and-paste

Quickly cut or copy an area and paste it where you want (single-use blueprints). External circuit connections are restored when possible.
MIT License
3 stars 2 forks source link

Not MP safe #2

Closed Nexela closed 7 years ago

Nexela commented 7 years ago

You are conditionally registering/removing on_tick without keeping track syncing with on_load

mickael9 commented 7 years ago

The on_tick event stays registered for only one tick so, I think the chances of this causing a problem are quite low. I'll fix it though.

Is this what you had in mind?

diff --git a/control.lua b/control.lua
index e9846d9..195a0a1 100644
--- a/control.lua
+++ b/control.lua
@@ -359,6 +359,7 @@ script.on_event(defines.events.on_put_item, function(event)

     printf("here we go")
     selection.state = item_state.placing
+    global.on_tick_registered = true
     script.on_event(defines.events.on_tick, on_tick)
 end)

@@ -579,6 +580,7 @@ function on_tick(event)
         end
     end
     script.on_event(defines.events.on_tick, nil)
+    global.on_tick_registered = false
 end

@@ -608,3 +610,9 @@ do

     end)
 end
+
+script.on_load(function()
+    if global.on_tick_registered then
+        script.on_event(defines.events.on_tick, on_tick)
+    end
+end)
Nexela commented 7 years ago

That looks good from up here. Thanks!