Community

SCI Programming => SCI Syntax Help => Topic started by: gumby on February 03, 2011, 03:19:41 PM

Title: Arrays as Object Properties
Post by: gumby 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. 
Title: Re: Arrays as Object Properties
Post by: gumby 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?)
Title: Re: Arrays as Object Properties
Post by: lskovlun 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.
Title: Re: Arrays as Object Properties
Post by: gumby 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