Python code port to 2.6

Hi!

I am fairly new to python using Blender and basically I am trying to port the 3rd person template of cray (http://blenderartists.org/forum/showthread.php?t=153042) to make it work in 2.6.

Has anyone done it already?

There are 5 scripts: move the character, control the camera, avoid the collision of the camera using a ray and two more regarding counters of the game and special movements.

The first problems I am finding is just after the imports. Before you could import GameLogic or GameKeys, whereas now you should import bge.logic or bge.events.

from bge import logic
from bge import events

However, the following code still gives errors:


cont = logic.getCurrentController() //Before it was GameLogic.getCurrentController()
own = cont.getOwner()      <----- ERROR
objList = logic.getCurrentScene().getObjectList()  <----- ERROR
front_tget = objList["OBfront"]
back_tget = objList["OBback"]
left_tget = objList["OBleft"]
right_tget = objList["OBright"]
front_right_tget = objList["OBfront_right"]
front_left_tget = objList["OBfront_left"]
back_left_tget = objList["OBback_left"]
back_right_tget = objList["OBback_right"]

In all the scripts I get errors due to getObjectList() and getOwner(). What would be the way of doing this with the 2.6 API?

Any help is welcome.

own = cont.getOwner() <----- ERROR
objList = logic.getCurrentScene().getObjectList() <----- ERROR
to ->
own = cont.owner
objList = logic.getCurrentScene().objects

the next error I think is OBfront , no more “OBfront” but the right name of obj -> “front”

Thanks very much! The other problems seem similar, so I will try to solve all of them when I am free and hopefully come back to post the final scripts and blender file. :slight_smile:

Run them with 2.49
You will receive deprecation warnings in the console.

They will tell you how to solve them. Make sure you read them carefully.
Do that until all deprecation warnings are gone.

The modules GameLogic and GameTypes are still available. They got the aliases bge.logic and bge.types.
The only module that is not available is MathUtils. This was buggy and was removed completely.

After that you can run the modified code in 2.5+. If you still get errors it is most likely because of Python 3 syntax. The most obvious difference is print “x” is now print (“x”).

thats of <print> is the only “inverse improvement” in python all the rest seem better now !!

or not?

Thank you very much for your answers. I got back to port the code and I am following your advices. Really useful advice the one of running the file on 2.49, thanks!

Hi,

I solved many errors regarding how to access the objects and so on. However, I am still stuck getting this error in 2.6:

Python script error - object 'Main_character', controller 'character_moves':
Traceback (most recent call last):
  File "Character_Moves", line 109, in &lt;module&gt;
AttributeError: 'KX_GameObject' object has no attribute 'grounded'

The code that gives that error is the following:


cont = logic.getCurrentController()
own = cont.owner
if ground_test.positive:
	own.grounded=1
else:
	own.grounded=0

It complains about the lack of the attribute grounded. There are other attributes as well that are giving the same error. I attach a picture where grounded is defined:


Am I missing something obvious?

Object properties are now stored on a dictionary, not as a attribute;
use

object['property']

instead of

object.property

Be brave!..your task is a must!
Cray’s template its a wonder and unique…as far as I know!
A good reason to start to use these 2.6 thing ( I know I’m “heavy”, but I can’t get used to the interface)
Bye

Thank you agoose77, it was a simple thing that was in almost all the scripts!!
Thanks for the support as well ^^

I managed to change most of the parts of the scripts. However, in the following code, but in the setLinearVelocity the last 1 is defining whether it is local or global. Now, with the new code it seems that I lost that option. Is that right?


#Old
moves.setLinearVelocity(0, walk, 0, 1)
moves.setForceLimitY(ForceLimitYMin, ForceLimitYMax, True)
moves.setForceLimitZ(ForceLimitZMin, ForceLimitZMax, True)	

#New
moves.linV=[0,walk,0]
moves.forceLimitY=[ForceLimitYMin,ForceLimitYMax,True]
moves.forceLimitZ=[ForceLimitZMin,ForceLimitZMax,True]

I have been struggling with the actuators. For instance, when the character moves, the script gets the direction depending on the keys that the player has pressed. However, it seems that I have a problem substituting the setObject function.


moves = cont.actuators["moves"]
direction = cont.actuators["direction"]
...
if forward.positive and turn_left.positive:
	print("Moving forwardLeft")
	up_key = 1
	left_key = 1
	move_state = 1
	#direction.setObject(front_left_tget)
	direction=front_left_tget #I replaced the previous one by this one
...
cont.activate(moves)
cont.activate(direction) #ERROR

The error message is:

Traceback (most recent call last):
  File "Character_Moves", line 248, in &lt;module&gt;
ValueError: front_left_tget not in this python controllers actuator list
Python script error - object 'Main_character', controller 'character_moves':

Basically, the error message is correct, because the only actuator that I have for that is direction (which is an edit object with track to). I believe that setObject was not copying as I do now in the code. What would be the way to do setObject in 2.6?

Thanks in advance!

Ok, I answer myself for the second question:

direction.object , no direction (I was assigning the whole object instead of the object to track…)

Some links to bookmark:
BGE 2.49 API

and
BGE 2.62 API

The Motion Actuator is called KX_ObjectActuator in the Python API. This is a bit strange, but it is like that for ages.

the 2.49 API describes how to replace setLinearVelocity()

The local flag is a separate attribute now:
KX_ObjectActuator.useLocalLinV

Hi!

Thanks Monster! I manage to port all the scripts. However I am getting a slower response with the camera mouse look in 2.6 than in 2.49… Sometimes it slows down or even gets blocked, and sometimes it moves smoothly. I have no clue which the source of the problem…

Then I found some problems not related to the python scripts. Basically, there are 3 armatures: normal, under the water and when the character is hanging from an edge.
I found that when the character changes to any of these armatures, say that the character was hanging from an edge and jumps down, it still continues in with the previous armature. However, the state of the armature has changed: there are 2 variables to see if the armature is under water or in the edge.

If any of you have any clue, I would appreciate the help. I will clean a little the scripts and upload the file as well.

Yes, please…if you post a link to the file, I’ll be very glad to try to help!
Bye

I agree with OTO. This is a very specific issue. Without details we can’t help.

Hi!

Here is the blend file. I did not have time to clean the scripts, but I guess you can understand everything ^^

You can get the file from the following url (I could not manage to upload it here, is 6MB too much?)

http://dl.dropbox.com/u/978291/Third_pers_template_2_0_bsc2.blend

Hi!

I did not have time to clean the scripts but I upload the file so that you can dig into it if you like.

http://dl.dropbox.com/u/978291/Third_pers_template_2_0_bsc2.blend

Did you find out the problem with the armatures? I am still trying to find it… I hope I have some spare time this afternoon.

Still searching…it’s a weird problem…states change but not the corresponding animation?..

Yes, Cray ( I guess?), the author, have made a 2.5 compatible version!
But have the same problems as yours in 2.62!
Maybe a bug?
Bye