Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mk-fg/acf2f31741d72eb0e9c2394ea3e5a7aa to your computer and use it in GitHub Desktop.
Save mk-fg/acf2f31741d72eb0e9c2394ea3e5a7aa to your computer and use it in GitHub Desktop.
Factorio SwitchButton-1_0 mod fixes 0.1.9 -> 0.1.10
From aed1fdd316d3fad21d051d0264769b2b675d01e3 Mon Sep 17 00:00:00 2001
From: Mike Kazantsev
Date: Sat, 5 Sep 2020 20:14:29 +0500
Subject: [PATCH 2/5] Fix using mod-local variable, as it will cause mp desync
for later-connecting players
---
control.lua | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/control.lua b/control.lua
index b2d865a..1a22fb9 100644
--- a/control.lua
+++ b/control.lua
@@ -2,14 +2,13 @@
"Keybindused" is only to control the script (on_gui_opened), so it only work when his value is 0,
and prevent to close the gui when the keybin key is presed to open the entity gui
]]
-local keybindused = 0
---------------------[BUILD ENTITY FUNCTION]---------------------
local function onBuilt(event)
local switchbutton = event.created_entity
if switchbutton.name == 'switchbutton' then
local control = switchbutton.get_or_create_control_behavior()
- control.enabled=false
+ control.enabled = false
end
end
@@ -18,7 +17,7 @@ local function onPaste(event)
local switchbutton = event.destination
if switchbutton.name == 'switchbutton' then
local control = switchbutton.get_or_create_control_behavior()
- control.enabled=false
+ control.enabled = false
end
end
---------------------[CUSTOM INPUT FUNCTION]---------------------
@@ -26,7 +25,7 @@ local function onKey(event)
local player = game.players[event.player_index]
local entity = player.selected
- keybindused = 1
+ global.keybind_state[event.player_index] = true
if entity and entity.valid then
if entity.name == 'switchbutton' then
local distance = math.abs(player.position.x-entity.position.x)+math.abs(player.position.y-entity.position.y)
@@ -34,7 +33,7 @@ local function onKey(event)
local control = entity.get_or_create_control_behavior()
if settings.startup['ReverseOpenInventory'].value then
player.opened = entity
- keybindused = 0
+ global.keybind_state[event.player_index] = nil
elseif not settings.startup['ReverseOpenInventory'].value then
control.enabled = not control.enabled
end
@@ -46,7 +45,7 @@ end
script.on_event(defines.events.on_gui_opened, function(event)
local player = game.players[event.player_index]
local entity = player.selected
- if entity ~= nil and entity.name == 'switchbutton' and keybindused == 0 then
+ if entity ~= nil and entity.name == 'switchbutton' and not global.keybind_state[event.player_index] then
local control = entity.get_or_create_control_behavior()
if settings.startup['ReverseOpenInventory'].value then
player.opened = nil
@@ -54,8 +53,10 @@ script.on_event(defines.events.on_gui_opened, function(event)
end
end
end)
+
---------------------------[SCRIPTS]---------------------------
script.on_event('switchbutton-keybind', onKey)
script.on_event(defines.events.on_built_entity, onBuilt)
script.on_event(defines.events.on_robot_built_entity, onBuilt)
script.on_event(defines.events.on_entity_settings_pasted,onPaste)
+script.on_init(function() global.keybind_state = {} end)
--
2.28.0
From 070fc81010869f16a653a0aeeb057775ece7659b Mon Sep 17 00:00:00 2001
From: Mike Kazantsev
Date: Sat, 5 Sep 2020 20:22:11 +0500
Subject: [PATCH 3/5] Add default switchbutton signal (signal-check - a check
mark)
---
control.lua | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/control.lua b/control.lua
index 1a22fb9..05055c7 100644
--- a/control.lua
+++ b/control.lua
@@ -9,7 +9,8 @@ local function onBuilt(event)
if switchbutton.name == 'switchbutton' then
local control = switchbutton.get_or_create_control_behavior()
control.enabled = false
-
+ control.parameters = {parameters={{ index=1,
+ signal={type='virtual', name='signal-check'}, count=1 }}}
end
end
-------------------[COPY/PASTE ENTITY FUNCTION]------------------
--
2.28.0
From cd1114ff1e1187a3f581067f72ac3c4e74b7626f Mon Sep 17 00:00:00 2001
From: Mike Kazantsev
Date: Sat, 5 Sep 2020 20:30:47 +0500
Subject: [PATCH 4/5] Fix copy-pasting settings - now copies signal and
enabled/disabled state
---
control.lua | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/control.lua b/control.lua
index 05055c7..069d401 100644
--- a/control.lua
+++ b/control.lua
@@ -15,10 +15,12 @@ local function onBuilt(event)
end
-------------------[COPY/PASTE ENTITY FUNCTION]------------------
local function onPaste(event)
- local switchbutton = event.destination
- if switchbutton.name == 'switchbutton' then
- local control = switchbutton.get_or_create_control_behavior()
- control.enabled = false
+ local sb1, sb2 = event.source, event.destination
+ if sb1.name == 'switchbutton' and sb2.name == 'switchbutton' then
+ local control1 = sb1.get_or_create_control_behavior()
+ local control2 = sb2.get_or_create_control_behavior()
+ control2.enabled = control1.enabled
+ control2.parameters = control1.parameters
end
end
---------------------[CUSTOM INPUT FUNCTION]---------------------
--
2.28.0
From 5c69d204b3a072863181f6e83b6fe8e1cfee6545 Mon Sep 17 00:00:00 2001
From: Mike Kazantsev
Date: Sat, 5 Sep 2020 20:39:46 +0500
Subject: [PATCH 5/5] Add entity filters to event handling, handle
script-raised events as well
---
control.lua | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/control.lua b/control.lua
index 069d401..2463164 100644
--- a/control.lua
+++ b/control.lua
@@ -5,7 +5,7 @@
---------------------[BUILD ENTITY FUNCTION]---------------------
local function onBuilt(event)
- local switchbutton = event.created_entity
+ local switchbutton = event.created_entity or event.entity -- last one for revive event
if switchbutton.name == 'switchbutton' then
local control = switchbutton.get_or_create_control_behavior()
control.enabled = false
@@ -58,8 +58,11 @@ script.on_event(defines.events.on_gui_opened, function(event)
end)
---------------------------[SCRIPTS]---------------------------
-script.on_event('switchbutton-keybind', onKey)
-script.on_event(defines.events.on_built_entity, onBuilt)
-script.on_event(defines.events.on_robot_built_entity, onBuilt)
-script.on_event(defines.events.on_entity_settings_pasted,onPaste)
+local event_filter = {{filter='name', name='switchbutton'}}
script.on_init(function() global.keybind_state = {} end)
+script.on_event('switchbutton-keybind', onKey)
+script.on_event(defines.events.on_built_entity, onBuilt, event_filter)
+script.on_event(defines.events.on_robot_built_entity, onBuilt, event_filter)
+script.on_event(defines.events.script_raised_built, onBuilt, event_filter)
+script.on_event(defines.events.script_raised_revive, onBuilt, event_filter)
+script.on_event(defines.events.on_entity_settings_pasted, onPaste)
--
2.28.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment