Data Realms Fan Forums http://45.55.195.193/ |
|
Redefinable Functions http://45.55.195.193/viewtopic.php?f=73&t=17324 |
Page 1 of 1 |
Author: | Kyred [ Tue Dec 08, 2009 9:01 am ] |
Post subject: | Redefinable Functions |
Thought this was an interesting concept. I was looking around at Lua websites and I came across a function called loadstring(). What it does is basically turns a string into a function. For example, Code: f = loadstring("print(\"Moo\""); f(); Code: function f() print("Moo"); end f(); However, the difference is the contents for loadstring() can be stated as a variable, which can change even while the game is running. Furthermore, if you wanted to pass arguments to loadstring, you could do something along the lines of: Code: local ChangeableFunction = assert(loadstring("return function(a,b) local result = a+b; return result; end")); MyFunction = ChangeableFunction(); output = MyFunction(3,5); In the end, output will be 8 (3+5). This means we could have functions that can change in game based on in game conditions. Here's an example, although a somewhat silly one. Lets say you have a scripted actor. It's script calls the function DoSomething() and prints it out to the console. DoSomething() always outputs 5. But the actor gets in range of 250 pixels from another actor, the function DoSomething() changes to output that actor's pointer. Furthermore, now that DoSomething() has been redefined, ANY call to the function DoSomething() will return that actor's pointer. In otherwords, this can be a useful method for variable storage that you want to be transferable. Code: function DoSomething() return 5; end --Update for a scripted actor. function Update(self) for actor in MovableMan.Actors do if SceneMan:ShortestDistance(actor.Pos,self.Pos,true).Magnitude < 250 and DoSomething() == 5 then DoSomething = loadstring("return "..actor); end end local data = DoSomething(); print(data); end |
Author: | Geti [ Tue Dec 08, 2009 11:13 am ] |
Post subject: | Re: Redefinable Functions |
♥♥♥♥ amazing stuff, kryed, thank you for the find/explain. We need a proper index for all these snippets, though i suppose the forum in itself functions that way (look for the topics without how in their title and you're on the right track).. |
Author: | Kyred [ Tue Dec 08, 2009 9:00 pm ] |
Post subject: | Re: Redefinable Functions |
I just successfully redefined the Update(self) function of a weapon in game using this method . Had do to things somewhat hackish, though. Also, it only works on weapons that are already scripted. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |