Scripting problem

Update: Please see post #4

Hi I am trying to make a character walk, run and reverse. I have defined an integer property call Action. When action == 0, the armature does nothing, when action == 1, the character walks etc.

0 = rest
1 = walking
2 = running
3 = reverse

http://img37.imageshack.us/img37/3572/45700260.png

As shown in the screen-shot, I have made 4 property sensors to detect what value has been assigned to the Action property.

Now I want to get rid the sensors, and let the python script do the properties checking. Here is my final script:

get current controller

controller = GameLogic.getCurrentController()

get object script attached to

obj = controller.owner

get controller

actAction = controller.actuators[“action”]

get sensors

senAct0 = controller.sensors[“if action = 0”]
senAct1 = controller.sensors[“if action = 1”]
senAct2 = controller.sensors[“if action = 2”]
senAct3 = controller.sensors[“if action = 3”]

StartFrame = actAction.frameStart
EndFrame = actAction.frameEnd
ActionName = actAction.action

Actions

Character standing

if obj[“Action”] == 0:

ActionName = “Walking Speedx2”
StartFrame = 1
EndFrame = 1

Character Walking

if obj[“Action”] == 1:

ActionName = “Walking Speedx2”
StartFrame = 13
EndFrame = 40

Character Running

if obj[“Action”] == 2:

ActionName = “Running (finalized)”
StartFrame = 1
EndFrame = 16

Character Reversing

if obj[“Action”] == 3:

ActionName = “Walking Speedx2”
StartFrame = 40
EndFrame = 13

Play Action

actAction.action = ActionName
actAction.frameStart = StartFrame
actAction.frameEnd = EndFrame
GameLogic.addActiveActuator(actAction,1)

For some reason, if I delete all the property sensors and remove the “get sensors lines”, the script will stop working. (I have also tried to link the script to an “always” sensor, but that does no good neither)

The script as it is now, functions properly, but the if statements (if obj[“Action”] == 0 etc.) seem to rely on the property sensors for some reason…

Anyone can suggest a solution??

Thanks in advance! :slight_smile:

you should better use LOGIC BRICK for this.

no it relies on the property Action.

What does the console say?

Ok I have sorted the sensors problem, I have now got the script to run without it attached to any property sensors although it now connects with an always sensor, otherwise it wouldn’t run.

But I can’t get the IF Statements to work, and the animations wouldn’t play (They were working before I removed the property sensors!)

http://img40.imageshack.us/img40/1280/25645662.png

This is my updated script:

get current controller

controller = GameLogic.getCurrentController()

get object script attached to

obj = controller.owner

get actuator

actAction = controller.actuators[“action”]

actuators parameters

StartFrame = actAction.frameStart
EndFrame = actAction.frameEnd
ActionName = actAction.action

Test if the script runs at all

print (“I run”)

Actions

Character standing

if obj[“Action”] == 0:

ActionName = “Walking Speedx2”
StartFrame = 1
EndFrame = 1

Character Walking

if obj[“Action”] == 1:

ActionName = “Walking Speedx2”
StartFrame = 13
EndFrame = 40

Character Running

if obj[“Action”] == 2:

ActionName = “Running (finalized)”
StartFrame = 1
EndFrame = 16

Character Reversing

if obj[“Action”] == 3:

ActionName = “Walking Speedx2”
StartFrame = 40
EndFrame = 13

Play Action

actAction.action = ActionName
actAction.frameStart = StartFrame
actAction.frameEnd = EndFrame
GameLogic.addActiveActuator(actAction,1)

You should read this documentation regarding sensors. I think that will help you to understand what happens.

Better use

 rather than [QUOTE] tags.

Thanks for the tutorial. I have read it and have fixed the problem by enabling the [ ’ ’ ’ ] of the Always sensor.
I think the reason why it didn’t work is because the sensor is constantly sending TRUE pulses and the actuator didn’t have time to play the action…I don’t know…something like that :stuck_out_tongue: