Data Realms Fan Forums http://45.55.195.193/ |
|
Accessing "self" Variables In Other Scripts http://45.55.195.193/viewtopic.php?f=73&t=19430 |
Page 1 of 1 |
Author: | Kyred [ Wed Aug 04, 2010 11:01 am ] |
Post subject: | 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! *EDIT Check my 3rd post below for a better example. |
Author: | findude [ Wed Aug 04, 2010 12:02 pm ] |
Post subject: | 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? |
Author: | Kyred [ Wed Aug 04, 2010 12:10 pm ] |
Post subject: | 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? |
Author: | Mind [ Wed Aug 04, 2010 5:32 pm ] |
Post subject: | 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 :> |
Author: | Kyred [ Wed Aug 04, 2010 7:33 pm ] |
Post subject: | 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 :> 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. |
Author: | TheLastBanana [ Wed Aug 04, 2010 8:04 pm ] |
Post subject: | 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. |
Author: | Kyred [ Wed Aug 04, 2010 9:37 pm ] |
Post subject: | 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. |
Author: | TheLastBanana [ Thu Aug 05, 2010 1:36 am ] |
Post subject: | Re: Accessing "self" Variables In Other Scripts |
Ah, that would explain it. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |