Author Topic: Arrays as Object Properties  (Read 1697 times)

0 Members and 1 Guest are viewing this topic.

Offline gumby

Arrays as Object Properties
« on: February 03, 2011, 03:19:41 PM »
Are properties within an object limited to 'singletons' (numbers, strings, etc), or can properties be compound values (arrays)?
Code: [Select]
( class myClass
    ( properties
        propA 0
        propB 0
        propC[5] ( 0 2 3 5 1 )
    )
)
I'm thinking that this is probably not appropriate, and the way to do it is create child object of the myClass object for propC, allowing me to have as many instances of propC as I would like. 


In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline gumby

Re: Arrays as Object Properties
« Reply #1 on: February 03, 2011, 08:56:38 PM »
... and the way to do it is create child object of the myClass object for propC, allowing me to have as many instances of propC as I would like. 
Correction.  PropC wouldn't be a 'child' object - but rather PropC would be it's own (unrelated) object that I'd create a list of within the 'myClass' object (I suppose I'd do this in the init() method of the myClass object?)
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline lskovlun

Re: Arrays as Object Properties
« Reply #2 on: February 04, 2011, 09:34:11 AM »
There is no way to do what you ask. That's part of the reason why they had to introduce the Memory call in SCI01 (but that's an ugly solution to the problem.
There is a hack that you can use, however. Declare a single locsl variable followed by the arrays you need. Then you can (at least from the point of view of the PMachine, not sure about Brian's compiler) index into the large arrays by addressing the lone local variable and using a bit of arithmetic.

So I'm saying something like
Code: [Select]
(local
  baseVar
  array1[10]
  array2[10]
)

(instance Blah of Obj
  (properties
    arrayPos 1
 )
)

(instance Blarg of Obj
  (properties
    arrayPos 11
  )
)
to use the array you then ask for baseVar[arrayPos+i] where i is the index you want.

Actually, you can probably skip the lone local variable and avoid the potential problem with the compiler altogether. Just use the first of your arrays as an index base. Of course, you then subtract one from the arrayPos values above.

Offline gumby

Re: Arrays as Object Properties
« Reply #3 on: February 05, 2011, 04:51:01 PM »
Thanks Lars, that works perfectly (I was able to use the single local variable successfully).  At first glance, your example made no sense to me.  After working through it, here is how I was ultimately able to reference the arrays (using your example):
Code: [Select]
  baseVar[(Blah:arrayPos)]     // Gets the 1st value from array1
  baseVar[(Blarg:arrayPos)]    // Get the 1st value from array2

  baseVar[+ (Blah:arrayPos) 1]     // Gets the 2nd value from array1
  baseVar[+ (Blarg:arrayPos) 1]    // Get the 2nd value from array2
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition


SMF 2.0.14 | SMF © 2017, Simple Machines
Simple Audio Video Embedder

Page created in 0.096 seconds with 23 queries.