1
\$\begingroup\$

Say I have object A, which has children B, C and D.

In OOP I would have A be an interface containing method action(), then I would have B, C and D implement A and, in their class definition, I would define the method in each of them in extremely different ways (as intended).

Is there a way to do something similar in GML? So far I can only think of making a script with an chain of if statements for every kind of action based on object_is_ancestor() which feels messy and unorganized to say the least.

Thank you for the help.

\$\endgroup\$
1
\$\begingroup\$

Yes, you can by using function pointers (or script indexes as game maker calls them).

You already know that you can have an object be a child object type of some other parent. You want parent A to have a general method "action()" that can be called in B, C, and D and have varying behavior. This can be done via function pointers, and is precisely the same way that the internals of most OOP languages do so. The reason you have to do this yourself is that game maker is an event oriented language (or at least that's how I have heard it phrased). Hence, objects don't have methods they have events and those are predefined or addable with triggers.

So how do you do function pointers in game maker? Suppose you have a few versions of the method written as scripts:

action()

actionB()

actionC()

etc.

Then just give the parent A and each of the children a variable called action_pointer. For A set action_pointer = action, and do similarly for each of the children. Then use the function script_execute to call the stored index.

You will have to make the methods separate from the objects and written as scripts (unless you wish to use the forbidden execute_string function which I highly recommend not using), but that can be done with simple organization and folders for different objects. The nice thing here is that if you do all this in the create event and call the parent's event before doing the sub-objects initialization within a create event then you get the effect of hierarchical behavior as an interface. I haven't had a particular use for it in game maker but it sounds like what you are wanting.

See here for more details on the script_execute function: http://gamemaker.info/en/manual/409_06_scripts

\$\endgroup\$
0
\$\begingroup\$

Game Maker doesn't support anything resembling OOP interfaces. On the other hand, you can use inheritance to override/overload actions defined in parent objects' events.

Also, you don't need to use object_is_ancestor() continuously, as long as you design your game object in a good way. You may create generic or ad-hoc scripts for different objects, or make a reference to a common user-defined event, and place your custom code in there as if you were implementing an interface method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.