Skip to main content
gamedev.net
🔒 Locked

Class interaction

Started by HinDRAncE Sep 24, 2010 at 2:47 AM 1 replies 1k views
Original Post
HinDRAncE
HinDRAncE
Hello forum, i'm trying to figure out the best way of having all my rts-like units to interact with eachother, but i'm not sure how to do it.

My idea is as follows:

Unit class: with all the AI built into it.
UnitHandler class(drawable game component): which holds all units and retrieves data from Unit "X" for unit "Y" to use.

The problem is, i'm not sure if this is the right approach, or if the Unit Class should be a nested class of UnitHandler (in order to get/set the data in a more elegant way)

On top of that, i already have AI code Built into Unit, so i dont want to migrate all the code to UnitHandler if i dont have to.

AbydosOne
AbydosOne
Depending on how you plan to create new units, it might be better to keep the Unit class outside of the handler. The UnitHander (Unit Manager) is a good way, to keep track of them. You can use it as a central hub then.

I would also suggest building in an event system into the handler, and also event reactions into the units. That way you can use the handler to dispatch events from other units and have the others react to that event (maybe there are units following a single unit and that single unit says "I am moving to X/Y/Z position").

I am fairly new to this type of development, but I can still have ideas. :)
Captain P
Captain P
Quote:
Original post by HinDRAncE
Unit class: with all the AI built into it.

Define 'AI'? Are you talking about pathfinding, obstacle avoidance, or about which enemy unit to attack and what attacks to use, when to withdraw... ? Not all of these aspects may fit equally well inside your Unit class - it may be better to split them up into various 'levels'. Especially if you plan on writing AI opponents - that's a kind of high-level AI that has to go somewhere 'higher-up'.

Quote:
UnitHandler class(drawable game component): which holds all units and retrieves data from Unit "X" for unit "Y" to use.

A sort of tell-me-what's-happening-around-me class, eh? Sounds fine - but why is it a drawable game component? Sounds like that's mixing responsibilities...

If you're doing a lot with groups, it may be useful to write a separate class for that, to ease communication between grouped units (for example, if all units of a group are to move at the same pace, it helps if they can ask their UnitGroup reference what movement speed they should adapt). Either way, the best approach depends on what kind of things you're making (a.k.a. your requirements).

Quote:
The problem is, i'm not sure if this is the right approach, or if the Unit Class should be a nested class of UnitHandler (in order to get/set the data in a more elegant way)

No, you probably don't want that. It may seem easier, but it's not more elegant. Why not just give your Unit class a proper interface?

Quote:
On top of that, i already have AI code Built into Unit, so i dont want to migrate all the code to UnitHandler if i dont have to.

If that AI code fits better somewhere else, you'd better refactor it as soon as possible. Don't let code grow into an intangible mess just because you were unwilling to change what you had already written. ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.