C Language Reference for Script Programmers - Structures
what are they?
Since messiah uses floating point frames the Frame Number will need to be a float. The Key Value should either be a float or a double depending on the precision you want. Tension, Continuity and Bias should all be floats. And End Behavior and Spline Type can both be ints ( 1 = reset, 2 = reverse, … or something like that).
Since each of the elements are of different types we can’t use an array to group them together, we need some other method. Ideally we’d like to create a “keyframe” type that would allow us to do something like this:
myKey.FrameNumber = 25.0;
Above we create a variable called myKey that is of type keyframe. We then assign values to each of the members of myKey by using the ‘.’ operator to access each element. Let’s look at how we can do this.
defining a new
typedef struct keyframe_St
Let’s go through this line by line. The first line means that we are creating a new type that is a structure. The tag keyframe_St is called the structure tag, we don’t really need to go into that. When you create a new structure just use the name of the type you’re defining and append a “_St” to it for the tag name. When you use messiah:develop to create your struct definitions this will all be done for you.
Following this first line we have, enclosed in curly braces, the definition of the members of the struct. The first line of the members defines a float called FrameNumber, the next line a double called KeyValue. The third line defines three floats for Tension, Cont and Bias, and so on. Finally the last line closes the curly braces and indicated the name of this new type, keyframe.
myKey.FrameNumber = 10.0;
pointers to structures
You start to run into a little trouble when you have a pointer to a structure and you want to access one of the members of the structure that the pointer points to. Take the following:
pkey = &real_key;
Now the question is “how do I access the members of real_key with the pointer pkey? Would the following work?
pkey.FrameNumber = 10;
Hopefully you realize that it won’t work. The reason is that pkey is a pointer to a keyframe, not a keyframe. Therefore pkey doesn’t have any members to access (remember that pointers only contain an address, nothing else). So by adding the .FrameNumber to the end of pkey I’m trying to access a member that isn’t there.
I can get around that be first dereferencing the pointer to the keyframe, once dereferenced we’re dealing with a keyframe and no longer just a pointer to a keyframe. So how ‘bout this:
*pkey.FrameNumber = 10;
Would that work? The answer is no, but I’ll let you off if you didn’t get that one, it’s a little tricky. The reason it won’t work has to do with the order of operations in C. Don’t worry about it just use parentheses as I mentioned earlier:
(*pkey).FrameNumber = 10;
Viola. That will work. The part in the parentheses evaluates to a keyframe, so the reference to the FrameNumber member will now be successful. That does look a little ugly though doesn’t it? Realizing that it would be kind of a pain to write that all the time, the “->” operator was created. This operator takes the place of the “.” operator when we are dealing with pointers to structures instead of actual structures:
pkey->FrameNumber = 10;
is equivalent to the previous line. Looks a lot cleaner huh? You will find that structures are used extensively in C programming and certainly in messiah. I’d highly recommend going through one of the reference books and learning structs inside and out.
|Converted from CHM to HTML with chm2web Pro 2.82 (unicode)|