Is it wrong to not use states in logic?

I know that other people have a hard time with large globs of logic…

But I love logic, and python and have found that logic is best for many things for me.

So can they/we make a tool where I can color different logic groups different colors, and then hide by color?

Like I setup logic for walking around and color that blue,

picking up and dropping = green

jumping = (255,0,255)

etc.

this would allow me to cluster and name groups by color, and could allow for people to organize their own logic.

I would use states, but most of the time I am using the same property set over and over

ie is it on the ground? what frame is his walk cycle? what frame is jump on (or at all?)

having the capacity to hide clusters would be slick.

also activate(run) and deactivate(don’t run) by color would be a bit like mixing states…

States are a way of sorting them, just like ‘by color’
Put all your walking ones in one state, all your powerup ones in another, and then use the brick to have both states active.

Ahh… I have never really used more then one state at once…

To be honest, neither have I.

These days my logic bricks seem to consist mainly of:

I have never got to the point where my logic is cluttered to me,

It feels like how a brain is wired… :smiley:

I wish I had more python skill, but Gil and python threading issues makes me want to learn C … :expressionless:

Learn Python first, it’ll be way easier to sink your teeths in to C after that.

I know basic python, just have not gotten into classes or modules that much,

Check out some of my stuff :smiley: project Wrectified, and some stuff in resources.

General
As usual it is not wrong to avoid things. You have to decide for single solution. It would be the wrong one if it does not what you expect it to do. In all other situations it is right.

As the world is usually not black and white only there are shades of gray - this means there are other solutions with individual benefits and drawbacks. You can only judge this solutions if you know them (which is the first criteria for judging ;)).

Example
As you asked for states I will give you an example that you can decide by yourself if you want to give it a try. I refer to the file you posted in thread vehicles #11.

We know it works, so it is a right solution. I checked the file and my main critic is that you didn’t used any meaningful names. This makes it very hard to understand what the logic is supposed to do. So I took the freedom to distribute some names where I hope they explain a bit (the logic did not change).

Single State Logic Block



I hope you can identify the bricks you designed.
What is nice:

  • it is one block of logic. The reader can see all of them at once.
    What is not so nice:
  • it is one block of logic. With the pure amount of blocks it is hard to get the single concerns.

You see an argument can be seen positive and negative at the same time.

States and Transitions
So I took these blocks and placed them into different states. This time I did a change to the logic. I introduced high-level states not just to group logic, but to act as states. There are two states:
1 - waiting (this happens when the player is in the car)
2 - user controlling (this happens outside of the car)

Switching between states is called “Transition”.
You can switch between this to states by setting/resetting the property “On”.
So you get the transition:
state 1 - waiting: if is On -> switch to state 2 - user controlling
Implemented it looks like this:


What happens: when state 1 is active and the sensor “is On” evaluates positive, the state machine will switch to state 2
(I will explain the additional states in the next post. Logically they can be seen as one state)

A similar behavior can be found in
state 2 - user controlling: is is not On -> switch to state 1 - waiting



What happens: when state 2 is active and the sensor “is On” evaluates not positive (which will be negative by the NOR controller), the state machine will switch to state 1.

You see the logic is not that much. You did that with a property sensor before. This is a fine solution too. But it creates some additional connections to the controllers. That can be confusing when the logic grows. Nevertheless in this situation it still would be a good solution.

Whit these two states you build a small but fine FSM (finite state machine). It starts with state 1 and toggles between state 1 and 2.

In the next post I explain how to group logic (without state machine).

Initial states
Be aware the state machine will start with the states enabled in the second panel.
If there is no state enabled, it will use the enabled states of the first panel (visible states). This can be dangerous as the logic might depend on states executed already (imagine an init state). Therefore I recommend to carefully check the initial states before starting the BGE.

State Names
Numbering states is not really helpful. States have no order. They are entities of themselves. Unfortunately Blender removed the option to name the states (2.49) without introducing a new (and better) method. To add at least a bit of a meaning I suggest to use the 2.49 naming style: the name of the state is the name first controller of this state. This way you can always check what the state is supposed to do.

A good naming practice is to use the -ing form. This way it describes the ongoing behavior:
Walking, Running, Waiting …
in difference to finished or requested behavior:
Play Walkcycle, Move forward …

I recommend to use the state names on transitions (name of the state actuator = name of the first controller).

(in Python there are numbers only, but you could create a mapping ;).)

In the post before we learned how to create a finite state machine (FSM). States can be used to group logic too. This means there is no additional logic (such as state changes). But you can group logic with certain level of abstraction. This way it is much easier to read and to manage. I call this states grouping states.

Polling player input
This state deals with the input from the player. As the player does not push changes to this object, this object polls for the current player data.



We need this data all the time, so this state is active when waiting and when user controlling (see post above).

Walking
This state deals with the (forward) walking behavior.



The object should only walk forward when on user controlling, so it is excluded from waiting. You see it does the motion, but it is more than one logic brick sequence. So it is worth an own (grouping) state.

Turning
This state deals with the turning behavior.



The object should only walk forward when on user controlling, so it is excluded from waiting.
This state encapsulates left turning and right turning. As this is not that much logic and they belong together, they are encapsulated into one (grouping) state. An alternative is to create a left turning and a right turning state.

I hope you can identify the logic bricks from the image in the previous post.

I hope you see what you can do with states:

  • You can group logic by concerns
  • You can create a finite state machine to add an higher level of abstraction to the logic

Now imagine what you can get with the player logic:


or even the car logic:


I hope it helps
Monster