View unanswered posts | View active topics It is currently Fri Dec 27, 2024 8:47 am



Reply to topic  [ 3 posts ] 
 Redefinable Functions 
Author Message
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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();
is the equivalent of coding:
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


Last edited by Kyred on Tue Dec 08, 2009 6:07 pm, edited 1 time in total.



Tue Dec 08, 2009 9:01 am
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post 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)..


Tue Dec 08, 2009 11:13 am
Profile WWW
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post Re: Redefinable Functions
I just successfully redefined the Update(self) function of a weapon in game using this method :grin:. Had do to things somewhat hackish, though. Also, it only works on weapons that are already scripted.


Tue Dec 08, 2009 9:00 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.032s | 13 Queries | GZIP : Off ]