lambdas in objData (or: the evasive lambda)
Posted: Sat Jan 09, 2010 1:57 am
For a long time we have tried to store functions on objects, for the purpose of saving them across sessions. Up until now this has not been possible, but as a side effect of the new long listing of lambdas, it turns out we can recreate any functions stored on an object. An example
Ummm.... woohoooo 
(func1Again) happily outputs "test" to the console.
Now, func1Again is not the same object as func1, so stuff like closures are not preserved, but otherwise it feels and acts the same.
All in all, good news for dynamic modding
Code: Select all
(block (func1 funcLiteral func1Again)
(setq func1 (lambda Nil (dbgOutput "test")))
(objSetData gPlayerShip 'func func1)
;; now retrieve it as a literal
(setq funcLiteral (objGetData gPlayerShip 'func))
;; now, to convert it, we do some evalling
(setq func1Again (eval (eval funcLiteral)))
(func1Again)
)

(func1Again) happily outputs "test" to the console.
Now, func1Again is not the same object as func1, so stuff like closures are not preserved, but otherwise it feels and acts the same.
All in all, good news for dynamic modding
