structures

Marty Billingsley marty at vertex.ucls.uchicago.edu
Mon Mar 8 14:20:19 EST 2004


(top posted, 'cos I included three replies down below)

Thanks for the answers.  To reply to jbv, structures were
indeed created to combine different data types, which isn't
an issue in Transcript.  However, they have the advantage
over multi-dimensional arrays in that you don't have to
remember the position of the data in order to access it.
In that way, to the student, structures are "cleaner".

Rob's idea of using properties is pretty cool, although
a little contrived -- not so straightforward a solution.

I think the three parallel arrays are the way to go, the
most straightforward concept for the student to grasp.

Thanks!!

  - marty


what we're talking about:

I asked:
> Is there a way to create structures in Transcript?
> I want to represent an entity that has several attributes
> (actually, I want to represent an array of such entities).
> So, is there something similar to the "struct" that exists
> in programming languages like c++ and scheme?
>
> For example, one of my students wants to create an array
> of bullets for his video game.  For each bullet we need to
> know the following:
>  - its current availability (already in use, or available)
>  - its movement in the x direction if in use
>  - its movement in the y direction if in use
>
> We could use three parallel arrays, but it seems cleaner to
> use a struct.  Is it possible?

One answer, from jbv:
> IIRC in C structures are composed of elements of
> various natures (variables, arrays, strings...) and
> AFAIK such a concept doesn't exist in Transcript.
>
> As for your example, why don't you simply use
> a 3 dimension array ?
> In which way using a struct. would be cleaner than
> an array ?

Another answer, from Rob Cozens
> The short answer to your question is "no".
>
> Longer answer:
>
> * By "structure", I presume you mean a record variable with multiple
> fields that can be addressed individually.
>
> * Perhaps your student might consider using properties.  Example: a
> radio button or check box's hilite property already tells you if the
> button is available (hilite = false) or selected (hilite =  true).
> One could use an existing button property (eg: armed or hilited or
> disabled) or set a custom property for each bullet.
>
> * You could use 1 array with three items in each value:
>
> put (the hilite of me)&comma&(the loc of me) into bulletArray[bulletNumber]
>
>
> * How are the bullets moved?  If a bullet were to store it's position
> after each movement, it could calculate x & y movement:
>
> 	on bulletMoved
> 		put the loc of me into newLocation
> 		get the oldLocation of me
> 		put (item 1 of newLocation) - (item 1 of it) into xMovement
> 		put (item 2 of newLocation) - (item 2 of it) into yMovement
> 		set the oldLocation of me to newLocation
> 	end bulletMoved
>

And another answer, from Dar Scott:
> 1.
> You can exploit chunks like this:
>
> constant xV = 1
> constant yV = 2
> constant inUse = 3
>
> ...
> if item xV of bullets[i] > maxV then explodeBullet i
> ...
>
> With lines, items and words you might get some structure.
>
> 2.
> You can make virtual dimensions:
>
> ...
> if bullets[i,"xV"] > maxV then explodeBullet i
> ...
>
> 3.
> As you mentioned, you can use three arrays:
>
> ...
> if xVbullets[i] > maxV then explodeBullet i
> ...
>
> Future:  Nested arrays are on the feature request list in bugzilla.


More information about the use-livecode mailing list