-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIRROR] Cleans up the SS13_base lua file and adds a new lua file for…
… easily handling multiple signals on different objects. (#1905) * Cleans up the SS13_base lua file and adds a new lua file for easily handling multiple signals on different objects. (#82458) ## About The Pull Request Cleaned up the SS13.register_signal and SS13.unregister_signal, removing the weird list shifting. Also adds a new lua file that can be included for the use of registering different signals on various datums and being able to clear them all in 1 function. Removed the make_easy_clear_function option when registering a signal via lua because I don't think it's used by anyone and it lacks any sort of versatility. Users can just create their own function for clearing signals from a datum. Also updates the documentation for HARDDELETES.md as COMSIG_PARENT_QDELETING was renamed to COMSIG_QDELETING ## Why It's Good For The Game New handler file makes registering signals in batches a lot easier if you want to clear them in one go without clearing unrelated callbacks on the same datum. The list shifting in SS13.register_signal had pretty significant performance problems, so removing that will make registering and unregistering signals faster. ## Changelog :cl: admin: LUA - Adds a new library called handler_group. Include it in your files by doing require('handler_group') /:cl: --------- Co-authored-by: Watermelon914 <[email protected]> * Cleans up the SS13_base lua file and adds a new lua file for easily handling multiple signals on different objects. --------- Co-authored-by: Watermelon914 <[email protected]> Co-authored-by: Watermelon914 <[email protected]>
- Loading branch information
1 parent
f13ab18
commit be0bacb
Showing
6 changed files
with
185 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Handler Group | ||
|
||
This module is for registering signals on a datum or several datums and being able to clear them all at once without having to unregister them manually. This is particularly useful if you register signals on a datum and need to clear them later without accidentally unregistering unrelated signals | ||
|
||
## Functions | ||
|
||
### HandlerGroup.new() | ||
Creates a new handler group instance | ||
|
||
### HandlerGroup:register_signal(datum, signal, func) | ||
Registers a signal on a datum, exactly the same as `SS13.register_signal` | ||
|
||
### HandlerGroup:clear() | ||
Clears all registered signals that have been registered by this handler group. | ||
|
||
### HandlerGroup:clear_on(datum, signal, func) | ||
Clears all registered signals that have been registered by this handler group when a signal is called on the specified datum. Additionally, a function can be ran before it is cleared | ||
|
||
### HandlerGroup.register_once(datum, signal func) | ||
Identical to just creating a new HandlerGroup instance and calling `clear_on(datum, signal, func)`. | ||
|
||
The idea is to register a signal and clear it after it has been called once. | ||
|
||
## Examples | ||
|
||
The following examples showcase why using handler groups can make life easier in specific situations. | ||
|
||
### Explode when mob enters location | ||
This function creates a 1 tile-wide explosion at the specified location if a specific mob walks over it. The explosion won't happen if the mob dies. This function should be callable on the same mob for different locations. The function should be self-contained, it should not affect other registered signals that the mob may have registered. | ||
|
||
#### Without Handler Groups | ||
```lua | ||
local function explodeAtLocation(mobVar, position) | ||
local deathCallback | ||
local moveCallback | ||
local function unlinkFromMob() | ||
SS13.unregister_signal(mobVar, "living_death", deathCallback) | ||
SS13.unregister_signal(mobVar, "movable_moved", moveCallback) | ||
end | ||
deathCallback = SS13.register_signal(mobVar, "living_death", function(_, gibbed) | ||
unlinkFromMob() | ||
end) | ||
moveCallback = SS13.register_signal(mobVar, "movable_moved", function(_, oldLoc) | ||
if mobVar:get_var("loc") == position then | ||
-- Creates a 1 tile-wide explosion at the specified position | ||
dm.global_proc("explosion", position, 1, 0, 0) | ||
unlinkFromMob() | ||
end | ||
end) | ||
end | ||
``` | ||
|
||
#### With Handler Groups | ||
```lua | ||
local function explodeAtLocation(mobVar, position) | ||
local handler = handler_group.new() | ||
handler:clear_on(mobVar, "living_death") | ||
handler:register_signal(mobVar, "movable_moved", function(_, oldLoc) | ||
if mobVar:get_var("loc") == position then | ||
-- Creates a 1 tile-wide explosion at the specified position | ||
dm.global_proc("explosion", position, 1, 0, 0) | ||
handler:clear() | ||
end | ||
end) | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
local SS13 = require('SS13') | ||
local HandlerGroup = {} | ||
HandlerGroup.__index = HandlerGroup | ||
|
||
function HandlerGroup.new() | ||
return setmetatable({ | ||
registered = {} | ||
}, HandlerGroup) | ||
end | ||
|
||
-- Registers a signal on a datum for this handler group instance. | ||
function HandlerGroup:register_signal(datum, signal, func) | ||
local callback = SS13.register_signal(datum, signal, func) | ||
if not callback then | ||
return | ||
end | ||
table.insert(self.registered, { datum = datum, signal = signal, callback = callback }) | ||
end | ||
|
||
-- Clears all the signals that have been registered on this HandlerGroup | ||
function HandlerGroup:clear() | ||
for _, data in self.registered do | ||
if not data.callback or not data.datum then | ||
continue | ||
end | ||
SS13.unregister_signal(data.datum, data.signal, data.callback) | ||
end | ||
table.clear(self.registered) | ||
end | ||
|
||
-- Clears all the signals that have been registered on this HandlerGroup when a specific signal is sent on a datum. | ||
function HandlerGroup:clear_on(datum, signal, func) | ||
SS13.register_signal(datum, signal, function(...) | ||
if func then | ||
func(...) | ||
end | ||
self:clear() | ||
end) | ||
end | ||
|
||
-- Registers a signal on a datum and clears it after it is called once. | ||
function HandlerGroup.register_once(datum, signal, func) | ||
local callback = HandlerGroup.new() | ||
callback:clear_on(datum, signal, func) | ||
return callback | ||
end | ||
|
||
|
||
return HandlerGroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
local SSlua = dm.global_vars:get_var("SSlua") | ||
|
||
for _, state in SSlua:get_var("states") do | ||
if state:get_var("internal_id") == dm.state_id then | ||
return { state = state } | ||
end | ||
end | ||
|
||
return { state = nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters