Time for action - instantiating a structure
1. To set the projectile mass, we can use:
octave:32>projectile.mass = 10.1 projectile = { mass = 10.100 }
2. The velocity field is set in a similar fashion:
octave:33>projectile.velocity = [1 0 0] projectile = { mass = 10.100 velocity = 0 0 }
3. We can also set the text field as usual:
octave:34>projectile.type = "Cannonball" projectile = { mass = 10.100 velocity = 1 0 0 type = Cannonball }
and so on for position and whatever else could be relevant.
What just happened?
Command 32 instantiates a structure variable with the name projectile
by assigning a field named mass
the value 10.100. At this point, the structure variable only contains this one field.
In Commands 33 and 34, we then add two new fields to the structure variable. These fields are named velocity
and type
. It is, of course, possible to keep adding new fields to the structure.
Instead of typing in one structure field at a time, you can use the struct
function. (In the next chapter...