Fixes some lua problems, specifically with qdeling callbacks and adds lua utility functions (#77468)

## About The Pull Request
See title. Callbacks were getting deleted in SS13.lua which is
non-ideal. They should be automatically collected by the garbage
collector.
Also adds some utility lua functions like SS13.is_valid,
SS13.stop_tracking, SS13.new_untracked, SS13.type and SS13.qdel

## Why It's Good For The Game
Fixes some harddel issues, as well as callbacks clogging up the garbage
subsystem.

## Changelog
🆑
admin: Added new lua functions: SS13.is_valid, SS13.stop_tracking,
SS13.new_untracked, SS13.type and SS13.qdel
/🆑

---------

Co-authored-by: Watermelon914 <3052169-Watermelon914@users.noreply.gitlab.com>
This commit is contained in:
Watermelon914
2023-08-10 06:45:31 -04:00
committed by GitHub
co-authored by Watermelon914
parent 616ef99954
commit c4763f2621
3 changed files with 67 additions and 7 deletions
+27
View File
@@ -156,6 +156,30 @@ The following example spawns a singularity at the caller's current turf:
SS13.new("/obj/singularity", dm.global_proc("_get_step", dm.usr, 0))
```
### SS13.new_untracked(type, ...)
Works exactly like SS13.new but it does not store the value to the lua state's `references` list variable. This means that the variable could end up deleted if nothing holds a reference to it.
### SS13.is_valid(datum)
Can be used to determine if the datum passed is not nil, not undefined and not qdel'd all in one. A helper function that allows you to check the validity from only one function.
Example usage:
```lua
local datum = SS13.new("/datum")
dm.global_proc("qdel", datum)
print(SS13.is_valid(datum)) -- false
local null = nil
print(SS13.is_valid(null)) -- false
local datum = SS13.new("/datum")
print(SS13.is_valid(datum)) -- true
```
### SS13.type(string)
Converts a string into a type. Equivalent to doing `dm.global_proc("_text2path", "/path/to/type")`
### SS13.qdel(datum)
Deletes a datum. You shouldn't try to reference it after calling this function. Equivalent to doing `dm.global_proc("qdel", datum)`
### SS13.await(thing_to_call, proc_to_call, ...)
Calls `proc_to_call` on `thing_to_call`, with `...` as its arguments, and sleeps until that proc returns.
Returns two return values - the first is the return value of the proc, and the second is the message of any runtime exception thrown by the called proc.
@@ -200,6 +224,9 @@ SS13.set_timeout(5, function()
end)
```
### SS13.stop_tracking(datum)
Stops tracking a datum that was created via `SS13.new` so that it can be garbage collected and deleted without having to qdel. Should be used for things like callbacks and other such datums where the reference to the variable is no longer needed.
---
## Internal globals
+7 -1
View File
@@ -168,10 +168,16 @@ GLOBAL_PROTECT(lua_usr)
for(var/datum/lua_editor/editor as anything in editor_list)
SStgui.update_uis(editor)
// Called by lua scripts when they add an atom to var/list/references so that it gets cleared up on delete.
/// Called by lua scripts when they add an atom to var/list/references so that it gets cleared up on delete.
/datum/lua_state/proc/clear_on_delete(datum/to_clear)
RegisterSignal(to_clear, COMSIG_QDELETING, PROC_REF(on_delete))
/// Called by lua scripts when an atom they've added should soft delete and this state should stop tracking it.
/// Needs to unregister all signals.
/datum/lua_state/proc/let_soft_delete(datum/to_clear)
UnregisterSignal(to_clear, COMSIG_QDELETING, PROC_REF(on_delete))
references -= to_clear
/datum/lua_state/proc/on_delete(datum/to_clear)
SIGNAL_HANDLER
references -= to_clear