7
\$\begingroup\$

Say you have two events: Action1 and Action2. When you receive Action1, you want to store some arbitrary data to be used the next time Action2 rolls around. Optimally, Action1 is normally a command however it can also be other events. The idea is still the same.

The current way I am implementing this is by storing state and then simply checking when Action2 is called if that specific state is there. This is obviously a bit messy and leads to a lot of redundant code. Here is an example of how I am doing that, in pseudocode form (and broken down quite a bit, obviously):

void onAction1(event) {
    Player = event.getPlayer()
    Player.addState("my_action_to_do")
}

void onAction2(event) {
    Player = event.getPlayer()

    if not Player.hasState("my_action_to_do") {
        return
    }

    // Do something
}

When doing this for a lot of other actions it gets somewhat ugly and I wanted to know if there is something I can do to improve upon it.

I was thinking of something like this, which wouldn't require passing data around, but is this also not the right direction?

void onAction1(event) {
   Player = event.getPlayer()
   Player.onAction2(new Runnable() {

       public void run() {
           // Do something
       }

   })
}

If one wanted to take it even further, could you not simply do this?

void onPlayerEnter(event) { // When they join the server
    Player = event.getPlayer()
    Player.onAction1(new Runnable() {

       public void run() {

           // Now wait for action 2
           Player.onAction2(new Runnable() {
               // Do something
           })

       }

    }, true) // TRUE would be to repeat the event,
             // not remove it after it is called.
}

Any input would be wonderful.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Look up the Action List (also Command Queue) pattern. In general, though, expect some sloppiness to remain; game logic is not usually capable of being shoved into neat patterns. \$\endgroup\$ Mar 24, 2012 at 20:38

2 Answers 2

1
\$\begingroup\$

I think your initial example is the simplest. If I were to do anything I would have a central ActionManager. Actions could post data to it for other actions to pick up later. I would almost be like an Event system except that "Messages" aren't delivered to an entity, they simple sit, waiting for an Action to pick them up.

That would help decouple Action1 from Action2 so that if you wanted to connect Action3 to Action2 as well you could. Action2 doesn't know or care about 1 or 3, all it cares about is to look for an Action2Message.

\$\endgroup\$
0
\$\begingroup\$

It's hard to come up with a solution without knowing how your ActionListener/Handler/Manager.. looks like

the way i would do it (i'm a c# developer, but i think the same is possible in java):

instead of registering the state

Player.addState("my_action_to_do")

i would have a Stack of ActionListeners for Action2 events, to which the void onAction1() event would add a handler that will be called if Action2 is invoked:

(i'll try in java, might not be executable though):

Stack<ActionListener> onAction2Events = new ...;

void onAction1(event) {
    Player = event.getPlayer();
    onAction2Events.add(new ActionListener() {
      public void actionPerformed(ActionEvent e) { 
          // this code will be called if onAction2 is invoked
      }
    });
}

void onAction2(event) {
    Player = event.getPlayer()

    InvokeEvent(onAction2Events); // calls all ActionListeners / states that ware added

    // ...
}

void InvokeEvent(Stack<ActionListener> listenerStack) {

    while(listenerStack not empty)
    {
        listenerStack.pop().actionPerformed(...);
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .