problem with .setState()

Hello

the .setState() is not working as I want it to.

GameLogic.getCurrentController().getOwner().setState(3)

That should set the object’s state to number 3, right? (top row, 3rd from the left) And it works like copy state?

It don’t.

I tried with a scene with the default cube and made five states. Every state had an always actuator - and - movement. Every state had a movement or rotation on different axis so there’s no mistake which state is active. Then a script in the first state with the above code and tested it with different numbers in setState(). State 2 and 3 looks to both be number 2. 4 looks like 3 should. 5 like state 2 again. Then nothing until state 10 which again acts state 2. No warning in the console.

am I doing something completely wrong? if this looks correct I can post a blend if you don’t want to make this simple scene yourself.

Do you know if states start counting from 0? or 1?

if I enter 0 it says it’s not between 1 and 29 I think.

you need to set the state bitmask, not the number

GameLogic.getCurrentController().getOwner().setState(1<<2)

should work

I suspected I was doing something fundamentally wrong. Thank you I think I get it.

edit: I don’t, how do I use it?

The state of an object is a bit mask. The state of a controller is a single bit, numbered from 1 to 30 while the actual mathematical bit number is 0 to 29.
Graphically, the state is represented as collection of 30 button, each corresponding to a bit. The top row correspond to bits 0-14 and the bottom row to bits 15-31.

So if you want to set state 3, you have to set the state mask to 1<<(3-1) = 1<<2 = 4.

This complicated and it would be better to define constant values for each state bit: KX_STATE1 -> KX_STATE30 so that you could use setState(KX_STATE3), If you want to have more than one state bit set, you could use the | operator: setState(KX_STATE3|KX_STATE4).

I’ll this in SVN soon.

edit: this should go in 2.47 sticky thread.

what does “<<” mean? I don’t understand how to think. The constants sounds nice.

shift to the left

the number 1 is: 00000000000000000000000000000001
shifted two times: 00000000000000000000000000000100 (4)

i made a supah elegant function for that, you just pass the states you want to activate.


def getStateBitmask(*ac_states):
    """
    Return a state bitmask from a list of integers
    By cyborg_ar
    """
    mask = 0
    for state in ac_states:
        state = int(state)
        if state &gt; 0 and state &lt; 31:
            mask = mask|(1&lt;&lt;(state-1))
    return mask
    
#test it!
print getStateBitmask(1,2,3,4)

clean and simple