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