-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistration.ttslua
45 lines (38 loc) · 1.67 KB
/
registration.ttslua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local protList = {} -- a table where each element is format protList[protName] = <prot> . this is for lookup during initObject (see globals at the bottom)
local instanceList = {} -- a table where each element is format instanceList[protName] = {{guid1 =<data>}, guid2 = <data>, ...}
local function registerProt(prot, name)
if not prot.protName then -- check if there is a name already
if name then
prot.protName = name
else
error("no name provided to function registerProt")
end
end
local name = out.protName -- moving it from table to local var in case it wasn't there already
if protList[name] then -- all new prots are added to a single list for later lookup.
error("Tried to add duplicate prototype " .. name .. "!") -- idk if i should add a check for duplicate values/baseTables
return
else
protList[name] = out
instanceList[name] = {}
end
end
-- followup: maybe append a json to the regular GMNotes? like, maybe using the pattern "ObjProt Metadata:\n(.*)\n$"
local function getObjectProt(obj)
return obj.getGMNotes():match("^(.+)\n") -- prot name is stored in first line of GMNotes.
end
local function initObject(obj) -- call the constructor and bind to an object
local controllerProt = protList[getObjectProt(obj)]
if controllerProt then
return controllerProt:newInstance(obj)
end
end
function onObjectDestroy(dying_object)
local instances = instanceList[getObjectProt(dying_object)] -- check if object has a controller prot
if instances then
local instance = instances[dying_object.guid] -- check for instance because it may have been cleaned up already.
if instance then
instance:cleanupInstance()
end
end
end