Need help optimizing a logic system.

From my target shooting game demo thread (in this link)

ALSO: Look at this screen

If you click on the crosshair you’ll see this logic system, in the top right quarter you’ll see part of the 2 python scripts it uses.

I had a bit of trouble trying to optimize the logic system (by states or ray sensor filtering), without getting an error “Nonetype object has no attribute getOrientation()” or something similar either right away or after a minute. It breaks the shooting so I can’t do much to optimize it as much as I can or how optimized I think it can be.

The newest version is in the top post, see if you can look at what you see on the above screen and see what can be done to optimize it a bit, using states or something else, thanks.

As I said in the quote I had a bit of trouble trying to optimize this as far as it can go, and I do believe I’ll get a nice speedup once that is done. If anyone can help me by downloading the demo and optimizing the system I showed, telling me what was done, that would be great. If anyone finds the python can be noticably optimized as well that would be even better.


  • checks it out *
if str(ray.getHitObject()) != 'None': # Here we convert the result of ray to a string
# and then check to make sure the ray result isn't 'None'

As for any other problems you may have, you’ll have to wait for this slow steam to finish the file download.

Okay, I was able to apply this part to the bulletforce script and not break the shooting

if str(ray.getHitObject()) != 'None':

But I couldn’t really apply the second part of the snippet without getting the nonetype object has no attribute error again. Did I not apply it right or something, I put it to extend the ‘if’ statement with an ‘and’ instead of an ‘if’ in front of it as that was the only way I got that part to work in the spot I thought it this would fit.

if ray.getHitObject():

the str step is not needed and error prone

Cyborg_ar, I did what you said and it seemed to slow things down slightly compared to if I did the str. step.

Plus, I found if I replaced

if str(ray.getHitObject()) != 'None':


if str(ray.getHitPosition()) != 'None':

I could use an X-ray filter on the ray_all sensor to speed things up and not get any ‘nonetype’ object errors, I tested for 5 minutes and the shooting of these objects didn’t stop working. GetHitObject() was causing problems.

Must have been hitting an Object class it didn’t like. Anywho, glad to have been of help.

I was able to use getHitObject() on the bullet hole script though (it doesn’t create visible hole objects but it lets off an effect)

Now the next thing on the list, making the following script more compact and efficient, this was origionally Social’s script as I don’t understand the math at the top.

from math import sin, cos, sqrt
# vector functions
def VEC_length(x):
 return sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2])
def VEC_normalize(x): 
 length = VEC_length(x)
 return [x[0]/length,x[1]/length,x[2]/length]
def VEC_cross(x, y):
 return  [x[1]*y[2] - x[2]*y[1],
    x[2]*y[0] - x[0]*y[2],
    x[0]*y[1] - x[1]*y[0]]
def VEC_min(x, y):
 return [x[0] - y[0], x[1] - y[1], x[2] - y[2]]
def MAT_trackvector(fw, y):
 if abs(abs(fw[2]) - abs(y[2])) < .001: #prevent gimbol lock
  del y[0]
 right = VEC_normalize(VEC_cross(y, fw))
 up = VEC_cross(fw, right)
 return [[right[0], up[0], fw[0]],
  [right[1], up[1], fw[1]],
  [right[2], up[2], fw[2]]]
cont = GameLogic.getCurrentController()
obj = cont.getOwner()
ray_sensor = cont.getSensor("ray_bullet")
lmb_sensor = cont.getSensor("fire")
add_bullet_act = cont.getActuator("add_bullet")
# position the last added bullet hole
newobj = add_bullet_act.getLastCreatedObject()
if obj.ebhole and str(ray_sensor.getHitObject()) != 'None':
 hit_pos = ray_sensor.getHitPosition()
 hit_norm = ray_sensor.getHitNormal()
 newobj.setOrientation(MAT_trackvector(hit_norm, [0.0,0.0,1.0]))
 obj.ebhole = 0
# determine wether or not to create a new bullet hole
make_bullet = ray_sensor.isPositive() and lmb_sensor.isPositive()
if make_bullet:
 GameLogic.addActiveActuator(add_bullet_act, 1)
 obj.ebhole = 1

I need to know these things.
-Whether a new function in 2.47 or later can replace all the math stuff at the top of the script.
-How much more efficient can this script be made without breaking the bullethole placement and what is done to do that.

Here’s the Native Math functions; but I doubt there’s much you’ll beable to trim from the script, Social’s normally very thorough.

The new distance constraint actuator can add bullet hole without any math, check this demo

I checked and ported over the bullethole logic. The FPS didn’t change too much, but I don’t need python for the bullet holes.

you may try with alignAxistoVect()

I’m finding the distance constraint actuator is working quite nice for both the force object and the bullet holes. In come cases it even slightly decreases logic usage.

Now with that done I was able to put the logic in the force and bullethole objects themselves and be done with the long ray sensors.