Python

Hey guys, I’m trying to make a wooden maze game a bit like this:
http://t2.gstatic.com/images?q=tbn:QW5-ycBKwLA-XM:http://www.cs.utexas.edu/~quark/cs384g_p4/talk/wooden_marble_maze.jpg&t=1

I have written a Python script to take care of keyboard input. the problem is, when I run the game nothing happens. Attached is the .blend file, can you tell me what I’ve done wrong?

Attachments

Maze.blend (283 KB)

The console is a great debugging tool. It told me you had some syntax errors.

Python is a little different than C or Java in that it actually uses actual words (not, and, or) as opposed to symbols (!, &&, ||). Taking care of that got me half way to clearing out the errors.

What version of blender are you using? I had to go and update a bunch of api stuff you were using. Lots of gets/sets that are now properties.

After getting the console to shut up, it still wasn’t working. I don’t really know much about using actuators, so I rigged something up with the object’s worldOrienation, but it wasn’t working well.

So, first clean out that console spam, and then figure out why it isn’t working from there :wink:

I’m using 249.2

Ah I see. I use C++ so I thought it used symbols too :stuck_out_tongue:

Lets see whats appearing in the terminal…

Ah no such thing as ! for not. I’ll change that to isNegative() EDIT: No such thing as isNegative(). I’ll try not left.isNegative()…
And I’ll change this to and…

A lot more Syntax fixes later: Ok, no more Syntax errors.
Now I’m getting this error repeated infinite times:

Python script error from controller “cont#CONTR#1”:
Traceback (most recent call last):
File “Rotate.py”, line 17, in <module>
rotx = move.GetDRot()[0]
AttributeError: ‘KX_ObjectActuator’ object has no attribute ‘GetDRot’

so, what do I use instead of rotx = move.GetDRot()[0]?

EDIT: I Fixed most of it! now all I’m left with is:

Method getSensor(string) is deprecated, please use the sensors[string] property instead.
Rotate.py:8
Method getActuator(string) is deprecated, please use the actuators[string] property instead.
Rotate.py:14
Method getDRot() is deprecated, please use the dRot and the useLocalDRot properties instead.
Rotate.py:17
Method isPositive() is deprecated, please use the read-only positive property instead.
Rotate.py:21
Method setDRot() is deprecated, please use the dRot and the useLocalDRot properties instead.
Rotate.py:55

What does it mean that setDRot() and friends is deprecated? What am I supposed to use instead?

Ok fixed it somewhat. lines 32, 36 and 70 are the only problems.
getDRot, isPositive and setDRot are deprecated

Edit again (lol): I fixed every syntax error.
Now the only problem is that is doesn’t do anything. the board doesn’t move.

My new .blend file is attached

Attachments

Maze.blend (285 KB)

http://www.tutorialsforblender3d.com/GameModule/ClassKX_ObjectActuator.html

Instead of using getDRot() is dRot like the message says.


#Get
rotx = move.dRot[0]
roty = move.dRot[1]

#Set
move.dRot[0] = rotx
move.dRot[1] = roty

Python does encapsulation a little differently. It is much more common to directly access a property rather than accessing it through gets/sets. At first glance this seems to destroy encapsulation, but python has ways to change how that property access behaves without messing up the interface.

EDIT:
When I was doing my testing it was in 2.54. Where those deprecated functions where removed. If you are fine with deprecated functions, then you can leave them (though it is a good idea to update them).

Don’t forget to activate the controller so that the actuator actually does something (why your code probably isn’t doing much):
http://www.tutorialsforblender3d.com/GameModule/ClassSCA_PythonController_1.html

Yeah check my edit above. I changed the code to roughly what yours is like.
The controller isn’t set to do anything, the python script is supposed to change its settings so it does something

You are changing the controller settings, but you aren’t telling it to actually do something. That is what the last bit of my last post was about (after the edit).

The controller logic brick needs to send a signal to the actuator to tell it to do it’s thing.

Oh… Duh. I’ve got to go somewhere now so I’ll try that out when I get back.
Thanks!

Ok, I’ve edited my code so now it activates the controller but still no luck.

#Apply changes
dRot=[rotx,roty,0,0]
controller.activate(“move”)

Nothing happens.
I’m going to try and see if its a problem up earlier in the script.

In the code sample you gave the actuator name should appear without quotes as it is not a string but a variable name assigned earlier in the code when you get the attached actuators:


#Set rotation
move.dRot[0] = rotx
move.dRot[1] = roty

#Activate actuator
controller.activate(move)

If you’re still having difficulties post your latest blend and I’ll take a look when I get home.

Hope this helps!

I did something like that few months ago,

Maybe it can help you.

http://www.4shared.com/file/137379033/732e6e73/Nice_game.html

http://i559.photobucket.com/albums/ss36/Ninja_Goliath/das.jpg

Here’s how to play the demo:

Rotate the board using ASWD

While using ASWD keys, Hold “shift” to rotate the board with more precision,

Middle mouse button to rotate the camera
(It’s only a sensor you can set to “alway”)

and last but not least,
Holding right mouse button while moving the mouse will rotate the board.
(my personal favorite)

ho…
and “R key” to restart the game

Have fun & good luck with your game !

Ok, Battery I tried what you sent me and that didn’t fix it.
Ninja Goliath, I used the owner.applyRotation() like you did and it worked!
now I just have to fix up my maths.
I forgot how many times blender executes the script in one second :stuck_out_tongue: The board goes spastic.

Nice looking,

I’m happy this has been somehow useful to you.

Have a great day !