Re: Accessing "self" Variables outside Create() and Update()
Mind wrote:
Just curious, how exactly does this differ from being inside Update or Create?
And when will the function be performed if it's not inside either?
Thanks :>
Hmm...my lack of sleep seems apparent in my explanation above. My bad.
By "outside Update(self) and Create(self) functions", I mean that it will still be in those functions,
but not the Update() and Create() of that specific self. Allow me to exemplify this better than before.
Here is the Create(self) script from an AHuman with the PresetName of "Ski Dude".
Code:
function Create(self)
self.MaxYCheck = 20;
end
Now, lets say we want to find the value of the of MaxYCheck, but in the Create(self) function of an MOPixel named "DetectPixel". Here's how it's done.
Code:
function Create(self)
local targetActor = nil;
for actor in MovableMan.Actors do
if SceneMan:ShortestDistance(self.Pos,actor.Pos,true).Magnitude < 100 then
if actor.PresetName == "Ski Dude" then
targetActor = actor;
break;
end
end
end
local oldName = targetActor.PresetName;
local rand = math.random(0,9999);
targetActor.PresetName = oldName .. tostring(rand);
local trueActorData = nil;
for index,data in pairs(AHumans) do
if type(data) == "userdata" then
if data.PresetName == oldName .. tostring(rand) then
trueActorData = data;
targetActor.PresetName = oldName;
break;
end
end
end
print("targetActor.MaxYCheck = " .. tostring(targetActor.MaxYCheck));
print("MaxYCheck = " .. tostring(trueActorData.MaxYCheck));
end
The end result will yield this console output:
Notice how targetActor.MaxYCheck is
nil, while trueActorData.MaxYCheck is
20. Yet,
both targetActor and trueActorData point to the same AHuman. Normally, we would have to use messy global variables to reference "self" variables in other scripts, but this work-around eliminates that need.