# python questions

I’m sort of new to python and I was wondering how to do these things:

1. Make a “track to” python script

2. Change an objects name with python in the middle of a game

3. Assign a random value to a variable with python

Any help would be greatly appreciated.

1. Isn’t it easier to use the TrackTo subset of the EditObject actuator?

2. Object names cannot be changed mid-game as far as I know. Even if you could, I would highly recommend against it (confusion would happen).

``````rdm = int(GameLogic.getRandomFloat()*x)+y
``````

…where x is the range of numbers and y is the lower limit of choices. So if x is 5 and y is 3, then the number could be 3, 4, 5, 6, or 7.

Alternativelly, you can use the Random actuator.

with that particular code, shouldn’t 8 also be included in the set? Or does the getRandomFloat() not hit 1.0?

RagMan>

x determines the range, y is the starting point of the range and it’s actually a part of it from what I understand.

So it makes sense, because “including” 3 (the starting point), the other numbers (4,5,6,7) make 5.

I always just used the “random actuator” or the “random” python module with the: “ran = random.randint(3,7)”, but now that I think of it, I find the “getRandomFloat()” method to be much cleaner (don’t have to import anything).

Not only that, but using getRandomFloat() produces a better random number. Using the random actuator gives the same results every time unless the seed is changed.

Here is a little track to script that I pieced together from a bunch of sources and from reading about rotation matrices. I can’t claim any of it as mine as all this is pretty well already been done before. But it worked for me. Of course it might not work either and if anyone cares to make it better…well go ahead.

``````
# vector functions needed
#
# displacement of vectors
#
def vsub(end, begin):
return [(end-begin), (end-begin), (end-begin)]
#
# magnitude of the vector
#
def vmag(vec):
return math.sqrt(math.pow(vec,2)+math.pow(vec,2)+math.pow(vec,2))
#
# normalize a vector by dividing by vmag
#
def vnorm(vec):
mag = vmag(vec)
return [(vec/mag), (vec/mag), (vec/mag)]
#
# cross product is perpindicular to input vectors
# don't mix these up or normals could point wrong direction
#
def vcross(vec1, vec2):
cross = [(vec1*vec2 - vec1*vec2), \
(vec1*vec2 - vec1*vec2), \
(vec1*vec2 - vec1*vec2)]
#	print "cross:", cross
return cross
#
# Calculate Out vector
#
def vout(end, begin):
out = vsub(end, begin)
out = vnorm(out)
return out
#
# Calculate Up vector
#
def vrt(vec):
upw = [0.0, 0.0, 0.1]
up = vcross(vec, upw)
return vnorm(up)
#
# calculate right
#

import math
import GameLogic as gl

scene = gl.getCurrentScene()
cont = gl.getCurrentController()
own = cont.getOwner()

pointer = scene.getObjectList()["OBPointer"]
target = scene.getObjectList()["OBYTarget"]

tpos = target.getPosition()
ppos = pointer.getPosition()

out = vout(tpos, ppos)

right = vrt(out)

up = vcross(right, out)

#set Orientation with proper values

pointer.setOrientation([\
[right, out, up], \
[right, out, up], \
[right, out, up]\
])

``````

oh, nuts to that! orientation matrices are evil (my trig teacher sucked…)!! i’m still not understanding how the random number code provided doesn’t include the number 8 in the system. I’m reading it as random number from (0.0 to 1.0) * 5, which as intergers would be {0,1,2,3,4,5}, and then increment all of them by 3, which gives {3,4,5,6,7,8}. Why is this wrong? is there something i’m not getting? oh, and God, why would you want to change an object’s name in the middle of a game, anyway? even if it were possible, it’s just too messy, unless if you don’t mean the object itself, but more like a character name, which can be done by simply modifying a property (call it “name”).

The int command doesn’t round numbers. It truncates them. So both 1.2 and 1.7 would be int’d as 1.

duh! i know that!! jeeze, all you people think i’m stupid or something… i meant the random floating point number generator. does that go from 0.0 to 1.0, including both numbers? because, according to my logic, if you multiply the possible result by 5, you should receive one of 6 different numbers, when truncated that is. i listed the possible outcomes in my previous post, so just work off that.

Lol. Do some testing if your so confused:

``````a = GameLogic.setRandomFloat()
#a = int((a*5)+3) #&lt;--Uncomment that to add range functions
#and observe the results of the truncation.
print a
``````

Do a few tests, answer your own question.

I had sort of an idea that if a “track to” script was to complicated, I would just use logic bricks and have the object always track to and object called TRACK or something, and change the name of any object I wanted it to track to. Of course, now that I think back on it, it seems kind of stupid, because I could just have the object track to an empty and just change the position of the empty… anyways, thanks for the advice.

ok, took me a while to get around to doing this, but i finally tested the getRandomFloat() thing.

``````list = [0,0,0,0,0,0]
pos = 0
attempts = 0
print list

while pos &lt; len(list) and attempts &lt; 100:
num = int(GameLogic.getRandomFloat() * 5) + 3
count = 0
isIn = 0

while count &lt; len(list) and isIn != 1:
if num == list[count]:
isIn = 1
count += 1

if isIn == 0:
list[pos] = num
pos += 1

attempts += 1

print list
`````` Probably could have done this more efficiently, but this is, like, 5 min of work, without really caring. I anticipated the possibility that the random number generator doesn’t hit 0.0 or 1.0, and added a variable to limit the amount of loops. Apparently, as the test shows, it doesn’t hit those numbers. I probably could have saved myself, and everyone else, the annoyance of this post if i had just read the GameLogic.pdf file that i downloaded, but this was much more fun.