Sign in to follow this  
Telcontar

Strategy Game: Communication between code layers

Recommended Posts

Telcontar    1554
What is a good, standard way for the game to respond to actions taken by the player through any particular class?

For instance, in my current project a city can create an army with a few variables set by the player. Right now I have it set up as the City class actually creates the Army object, but passes it to the Game Controller for all the other work (placing it in the army lists for instance). In order to do this I have to have an appropriate function in the Game Controller and pass it to the City object when created (coding in Python). Similarly, I would have to make and pass functions for armies attacking, for trade caravans arriving... Is this overly clunky?

I'm doing my best to enforce seperation and modularity in my code (it being a hobby/learning project, I anticipate ripping it up and rewriting often), and thus I want to keep communication between code layers and objects (game map, playing objects, game controller, etc) as standardized as possible, so I'm looking for a paradigm that can be generalized across the entire project.

Share this post


Link to post
Share on other sites
greenvertex    510
I have no experience with Python so this may or may not be useful:

If you're creating an object in class A and immediately passing it to class B, why not simply have some interface from B to A to create said object? For example, in your situation, Game Controller clearly takes responsibility for the managing of your Army immediately after creation. It seems reasonable that Game Controller should be able to create armies (or traders, or whatever else it controls) either directly or indirectly via some game object factory.

This doesn't really break a dependency between City and Game Controller. The dependency does get inverted, however, which seems more natural to me. After all, is a city not a game object to be controlled more or less like an army?

Share this post


Link to post
Share on other sites
Telcontar    1554
[quote name='greenvertex' timestamp='1349808061' post='4988428'] After all, is a city not a game object to be controlled more or less like an army?[/quote]

Yep, and because of the way Python treats objects and functions it is relatively simple to do the "work" from anywhere in the code. This may be a case of me thinking too much like an OO C++ programmer and not enough like a Python Programmer (or perhaps overthinking the thing entirely). I suppose it is just a case of "pick a method and stick with it."

I'm now considering binding the create_Army() function in the city (triggered by the player actions) to a create_Army() function in the Game Controller, which just uses the calling object's (city's) variables to act appropriately.

I love Python, but it is capable of such different things than C++ that it can be hard to adapt.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this