Obj-C Component Gaming Architecture and Message Forwarding

I am trying to implement a simple component-based architecture for a game object based on components using Objective-C, largely along the lines of the article “Develop Your Hierarchy” by Mick West. To this end, I have successfully used some of the ideas presented in the article "Objective-C Message Forwarding" by Mike Ash, that is, to say using the method -(id)forwardingTargetForSelector:.

Basic setup: I have a GameObject class for a container that contains three instances of component classes as instance variables: GCPositioning, GCRigidBody, and GCRendering. The method -(id)forwardingTargetForSelector:returns the component that will respond to the corresponding selector defined using the method -(BOOL)respondsToSelector:.

All this, in a sense, works like a charm: I can call a method on an instance of GameObject, an implementation of which is found in one of the components, and it works. Of course, the problem is that the compiler gives warnings "do not respond to ..." for each call. Now, my question is: how can I avoid this? And specifically regarding the fact that the fact is that each instance of GameObject will have a different set of components? Maybe a way to register methods with container objects, on an object by object? For example, can I create some kind of method -(void)registerMethodWithGameObject:and how would I do it?

Now it may or may not be obvious that I'm pretty new to Cocoa and Objective-C, and just stick around basically, and all of this can be very alien here. Of course, although I would really like to know about the solution to my specific problem, anyone who would like to explain a more elegant way to do this would also be very happy.

Very much appreciated, -Bastiyan

+3
source share
5 answers

I don’t think that sending a container object over all messages of its components is what Mick West suggested - it does not help to remove the idea of ​​an “entity object of a monolithic game”.

, , - -. - , -.

, , id , , . (, , , !)

+2

id

, , , , , , .

0

GameObject -respondsToSelector:. , , respondsToSelector: YES, - YES.

0

id - performSelector NSInvocation, . . , , , , .

0

, , - . Java, :

id anObjectRef

, , , anObjectRef, .

There are also methods that can tell you if a particular object conforms to a particular protocol before applying it or assigning it.

0
source

Source: https://habr.com/ru/post/1741157/


All Articles