Hi Codecall regulars,
I'm not really new to OOP but I haven't really got my head around it fully. I have a problem that is probably best illustrated with a simple example, I'm sure this is a very common issue.
Let's say I have a Car() class, which instantiates other classes such as Wheels(), Engine() and so forth.
In the Car()'s mainloop, let's say it calls Accelerator.GoFaster(). The accelerator object updates itself but it has to go and tell the engine to open up its throttle. It would be inefficient to have Car()'s mainloop checking whether this needs to be done every cycle, but by my understanding it would be poor OOP practice for the accelerator to directly address the engine class through something like parent.myEngine.openThrottle().
How can the engine instance be informed of the increased acceleration without checking every cycle, and while avoiding the messy solution of having the Accelerator() go back up through parent and down to the engine instance?
Something event-based perhaps?
Any ideas?
I'm sure this problem is dead obvious to many of you, I just can't seem to Google up any good material on it!
Thanks for any insight you may have.
Well, If you have more than one car, I'd say event basing is wrong way to do it, as it would only slow down the whole project. In my humble opinion the best thing to do would be either:
- Have functions in Car, so you have to call Parent.OpenThrottle() - The worse solution
- Have direct link to myEngine in Accelerator, so in Accelerator you only have to call Engine.openThrottle() - the better one
If you are planning to have really lots of cars I'd consider keeping all the stuff in one object, ie Car, to make it most efficient. But it depends largely of what the final outcome should look.
I'm but a young games developer. If I were in your place, I'd just try all three ways to see which is fastest.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks