Struct by Index - Part 3
<< Back
Check out
Part 1 and
Part 2
if you haven't done so.
Let's create one more command to do some basic arithmetics against 2 struct instances.
Use UI
#COMMAND StructMemberMath "ADD""SUB""MUL""DIV" R R R "TO" R
#PUSH !g
#SET G$ !4
#IFSAME !1 ADD
Move (!2SI!g + !3SI!g) To !6SI!g
#ELSE
#IFSAME !1 SUB
Move (!2SI!g - !3SI!g) To !6SI!g
#ELSE
#IFSAME !1 MUL
Move (!2SI!g * !3SI!g) To !6SI!g
#ELSE
#IFSAME !1 DIV
Move (!2SI!g / !3SI!g) To !6SI!g
#ENDIF
#ENDIF
#ENDIF
#ENDIF
#POP G$
#ENDCOMMAND
Struct BucketOfNumbers
Number n1
Number n2
Number n3
Number n4
End_Struct
Function AddBucketOfNumbers BucketOfNumbers b1 BucketOfNumbers b2 Returns BucketOfNumbers
BucketOfNumbers result
Integer memberIndex
For memberIndex From 0 to 3
StructMemberMath ADD b1 b2 memberIndex to result
Loop
Function_Return result
End_Function
Function SubtractBucketOfNumbers BucketOfNumbers b1 BucketOfNumbers b2 Returns BucketOfNumbers
BucketOfNumbers result
Integer memberIndex
For memberIndex From 0 to 3
StructMemberMath SUB b1 b2 memberIndex to result
Loop
Function_Return result
End_Function
Notice that on line 34 and 43, where I hardcoded the number of members within the struct "BucketOfNumbers". Currently I don't know of a way
to find out how many members are in a struct. There is an excellent post
here to find out that through modifying existing commands (Struct/End_Struct/STRUCT_ARRAY_MEMBER/STRUCT_SCARLAR_MEMBER). However if you
are to use the approach above, please remember to check FMAC whenever you are updating to a new version of VDF/DF to see if any of those commands
are changed between versions.