View unanswered posts | View active topics It is currently Mon May 25, 2020 4:20 pm



Reply to topic  [ 8 posts ] 
 Accessing "self" Variables In Other Scripts 
Author Message
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post Accessing "self" Variables In Other Scripts
Ah-Ha! I finally thought of a way to access "self.variables" outside of the Create(self) and Update(self) functions.

So, pretend that the AHuman "Desired Actor" had a variable stored as "self.value". Normally, you can't access "self.value" outside of that actor's Create(self) and Update(self) functions, unless you use messy globals. This is because "self" is a unique pointer to the actor, different from any other pointer. And it turns out, a reference to the "self" of any spawned and scripted AHumans is stored in the AHumans metatable. The only exception to this, that I know of, is self.PresetName.

All you need to do is make your desired AHuman stand out from the others. And this can be done easily by exploiting the self.PresetName exception.

Here's the trick. Let's say you need to access that variable "self.value" from the scripted AHuman, with the PresetName of "Desired Actor". In the script, change the PresetName of the AHuman you want to access the variables from to some unique name. Then, search through the AHumans metatable, and search for that unique preset name. Then, access the variable you need through that specific pointer.

Code:
local targetActor = nil;
local targetValue = nil;
for actor in MovableMan.Actors do
     if actor.PresetName == "Desired Actor" then
          targetActor = actor;
          break;
     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;
                  break;
            end
      end
end

targetActor.PresetName = oldName;
targetValue = trueActorData.value; --tada!


It's hacky, but it works! :D

*EDIT
Check my 3rd post below for a better example.


Last edited by Kyred on Wed Aug 04, 2010 7:35 pm, edited 1 time in total.



Wed Aug 04, 2010 11:01 am
Profile
User avatar

Joined: Tue Dec 12, 2006 3:10 pm
Posts: 495
Location: Uncertain quantum state
Reply with quote
Post Re: Accessing "self" Variables outside Create() and Update()
I'm not exactly sure what you did there but I'm rather sure that since it involves metatables you are now a wizard.


...it works just fine for MovableMan.Particles and whatnot too, right?


Wed Aug 04, 2010 12:02 pm
Profile
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post Re: Accessing "self" Variables outside Create() and Update()
Unfortunately, it only works with scripted objects. So, it's only useful if a particle has a script already. But, the script can be consisting of empty functions that do nothing.

I wasn't quite sure what you were asking, so I took a stab at it. Did I answer your question?


Wed Aug 04, 2010 12:10 pm
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1357
Location: USA
Reply with quote
Post Re: Accessing "self" Variables outside Create() and Update()
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 :>


Wed Aug 04, 2010 5:32 pm
Profile
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post 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:
Image
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.


Wed Aug 04, 2010 7:33 pm
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3136
Location: A little south and a lot west of Moscow
Reply with quote
Post Re: Accessing "self" Variables In Other Scripts
Out of curiosity, wouldn't it be faster to do something like this?
Code:
   for index,data in pairs(AHumans) do
      if type(data) == "userdata" then
         if data.PresetName == "Ski Dude" then
            if SceneMan:ShortestDistance(self.Pos,data.Pos,true).Magnitude < 100 then
               print(tostring(data.MaxYCheck));
            end
         end
      end
   end

That way you only have to deal with one loop. On the other hand, the AHumans table grows pretty rapidly, so it gets slower as the game goes on.


Wed Aug 04, 2010 8:04 pm
Profile WWW
User avatar

Joined: Sun May 31, 2009 1:04 am
Posts: 308
Reply with quote
Post Re: Accessing "self" Variables In Other Scripts
My understanding is that the list only grows rapidly when you do PresetMan:ReloadAllScripts(), because it doesn't deleted the old function tables.


Wed Aug 04, 2010 9:37 pm
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3136
Location: A little south and a lot west of Moscow
Reply with quote
Post Re: Accessing "self" Variables In Other Scripts
Ah, that would explain it.


Thu Aug 05, 2010 1:36 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 8 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.027s | 13 Queries | GZIP : Off ]