Members As Arrays In An Array Of Objects

Recommended Posts

I came across a cool feature with the undocumented OOP scripting that I may have missed among the forum docs.  I created a class to group several related member variables.  Then, I built an array of these objects.


As expected, I can access a member variable for an object in the array using g_arrayOfClass[nArrayIndex].member.  However, I was not aware that I could use g_arrayOfClass.member to return an array of the member (one for each object in the object array)!  This is extremely useful for gathering an array of elements in the object list for filtering, etc.  (Currently, my script loops over the object list to check members or builds an array manually when needed.)


I know OOP is officially unsupported, but I am curious if this 'feature' is by design...I don't want to code myself to far into unsupported territory than necessary.

Thanks for your insight.


Link to comment
Share on other sites

Yes, it is actually by design and will be sticking around.  It only works, of course, if all the array elements have the same name.  The nice part is that its by name, not class type.  So, if you have this:


class a
   local x = 1
   local y = 2
class b
   local z = 3
   local x = 4
global x = new(a)
x[1] = new(B)
x.x will return {1,4}, even though the two objects are from two unrelated classes.  It works because the symbol name is the same.  This is one of the advantages of an interpretted language.
x.y and x.z however, will return an error because y and z don't exist in both objects, just one or the other.
Link to comment
Share on other sites

Cool, I will use it for member variables.  I also found that I can use g_arrayOfClass.method() to invoke a method on all objects in the array without writing looping script.  But, it does seem particular to methods that return a value, otherwise I get a "can't pull data from multiple objects if the data containms multiple rows" warning (5.87c).  Adding a return value to methods is simple, but I will refrain from calling methods() this way if it is unsupported.  Thanks again.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.