A little help to my code please.

I wrote a code that a character will track another character if the character has the name of a property that will trigger the character to chase him. Here is what I do (I’m just beginning to use python):

[/cont = GameLogic.getCurrentController()
own = cont.getOwner

#sensor

near = cont.getSensor("near")

#Actuator

track = cont.getActuator("track")
motion = cont.getActuator("motion")

#process

if near.distance():
    if ['Unit'] == ['Zerg']:
        own.getHitPosition() 		]

Moderation:
Please use the

 tags when entering Python code in a post. You find that in advanced mode. You can even change your own posts by pressing the edit button to correct that. This makes your post much more readable.

Help:

Your description is a bit unclear. 
We need to know: <i>What </i>should happen <i>when</i>.

Example:

> ...a character will track another character...

Sounds like a job for the trackTo actuator


> ...if the character has the name of a property...

What do you mean with that?  The character object's name and the property name should be equal?
Or do you mean, the character object has a property (of whatever name) containg the name of the target object?
Or do you mean, the character will track an object with a specific property (of whatever name)?
Or do you mean, the  the character will track an object with a specific property (of whatever name) containing a specific value?

When should that happen, when the target object gets the property setup? Or when it is near the character?


> ...the character to chase him...

Just add a forward motion actuator to the character object when starting tracking.

What I’m trying to make is, make the enemies fight each other without putting many track to actuators to the characters

cont = GameLogic.getCurrentController()
own = cont.getOwner

#sensor

near = cont.getSensor("near")

#Actuator

track = cont.getActuator("track")
motion = cont.getActuator("motion")

#process

if near.distance():
    if ['Unit'] == ['Zerg']:
        own.getHitPosition()

Alright, just a few simple mistakes there.

When ever you are debugging code, you want to start by looking at the console (the black DOS window) and seeing if you have errors.

In this case I would guess that you would have an error that says something on the lines of “List object is not callable”

When you are calling something, you are basically putting brackets after it.

cont.sensors()

If you look at the link in this sentence, you will see that cont.sensors is actually a property of cont. The sensors attribute is simply a list of sensors, so what your code really translates to is:

[sens1, sens2]("sens1")

Which doesn’t really make sense to the interpreter.

To correct that mistake is quite easy, all you have to do is change it to:

cont.sensors["sens1"]

Also, you made the same mistake when you said:

 if near.distance()

distance is a property of near.

In order to fix that, you just have to take away the brackets.

[EDIT:]Just one last thing I didn’t see, you are trying to access own.getHitObject() which is also an error because KX_GameObject doesn’t have that method. I think you were trying to access near.hitObject. You can find the docs for that at the second link that I gave you (the distance one.).


A great way to tell if something is a function or not is, functions normally have words like “set” or “get” in them, while properties are just the words them selves.

Hope that helps,
-Sunjay03

Some thing that might help you, maybe when you go out and learn some more is this:

#Tracks to object, no actuators needed.
# target: object that will track
#axis: (default=1)#0=x #1=y #2=z | Note: use negitive numbers for minus axis.
#factor (float) - Only rotate a fraction of the distance to the target vector (0.0 - 1.0)
#ob can be object or list.
#use3D is the same as the 3D button on the actuator.
def track(target,ob,axis=1,time=0,use3D=1):
	# calculate the factor
	if not bool(time):
		factor = 1.0
	else:
		factor = 1.0/time
	
	vec = target.getVectTo(ob)[1]
	if axis &lt; 0:
		vec = -Vector(vec)
		axis = axis*-1
	
	final = [0,0,0]
	if not use3D:
		final[0],final[1] = vec[0],vec[1]
	
	else:
		final = vec
	
	target.alignAxisToVect(final, axis, factor)

That is a function that does exactly the same thing as the track actuator. It even has the time value built in.

That function will allow you to track to objects and even coordinates with out the actuator.

Kinda dont get that thing as I told I’m a beginner in python yet. I want the characters on my game to kill each other. So its cont.sensor[“name”] not cont.getSensor[“Name”]?

Exactly. Maybe go study the basics of Python for a bit.

Here is a great book:
http://www.amazon.ca/Mastering-Blender-Tony-Mullen/dp/0470407417/ref=sr_1_1?ie=UTF8&s=books&qid=1272226596&sr=8-1

It doesn’t cover “All” the basics, but it does get the most important ones and its structured so that you can learn properly.

-Sunjay03

Maybe you find this demousefull. It does not use that much scripting. The most logic is in the brícks.

I just want the character to track another character if the that character has the property of the name I want. For example Character 1 will chase character 2 because it has a string property = enemy. Should I use rayTargetposition then?