Results 1 to 4 of 4
  1. #1
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Question State management system

    I am creating a game that I plan to release open source. So far everything has been going smoothly, thanks to an amazing book I found on design patterns, but I'm running into some problems with states. Currently, I have a rule based system, that works like this:

    -Each state defines a way for an object to behave. There are different state categories, such as the Life states:Alive/Dead, Movement states: Strafe/Follow/Freeze/Straight, and and the Aiming states: AimAtTarget/AimAtMouse/DoNotAim.

    -Each state also has a rule defining when the state should be active. For example, the Movement: Strafe state's rule says it should activate when the mouse is right clicked.

    -When an object with states is instantiated, it sends an instance of each rule to the global rule manager class.

    -The rule manager then loops through each rule, and if a rule is met, it calls the enforce() method on the rule, which then activates the state the rule is for.



    The problem with my method is it would be very difficult to add new abilities to anything. I need a more abstract and dynamic approach to managing the states of objects, or maybe even ditch the state pattern for something better. Any ideas?


    The entire source code for the project is HERE. The built in attachment system for this forum wasn't working.

  2. #2
    starbuck is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: State management system

    Hiya,

    I remember reading a few good alternatives to the state pattern here:

    Angry Architect: The Problems with the State Pattern

    Good one is an onEvent() method with an enumeration of states.

    The other thing is that the rules seem to be tightly coupled with the states. An alternative to that would be to employ something like a JBoss Drools engine with conditions that represent states (maybe even config driven so as to autogenerate the drools), which would use rete to optimally select rules based on passed state. Then return action to an action processor.

  3. #3
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: State management system

    Thanks for the reply starbuck. I threw out my old state code and made a much better system, based on your suggestions. One thing that is bugging me is instance variables. Should all instance variables be stored in the concrete class that has all the states? Or should each state manage it's own variables?

    I would opt for the centralized variable approach, but with so many variables, especially one involved in calculating movement (velocity, distance, x/y, ect...) it would lead to a large number of getters and setters, being called all the time.
    Of course I wouldn't have local variables (ones created and used solely by methods) in the main concrete class, that would be useless.

  4. #4
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: State management system

    I can't find the edit option, so I'll just add this as a reply.

    Here's a rough graphic representation of a state tree. It's from the Player class.
    State tree

Similar Threads

  1. Leave management System
    By anji4u_19 in forum Suggestions & Feedback
    Replies: 24
    Last Post: 04-05-2010, 07:03 PM
  2. Window management system
    By MDUK in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2008, 07:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •