Problems that vexed you for a long time, and then you solved.

What I mean is that you had a problem with your game that challenged you and annoyed you for a long time and then fixed it later on (maybe because you’ve gotton more experienced in the BGE or something).

I had one of those and recently fixed. I have been developing a game I started a long time ago initially based off of Social’s ancient FPS template, see this code


cont = GameLogic.getCurrentController()
own = cont.getOwner()
camown = GameLogic.getCurrentScene().getObjectList()["OBcamera0"]
 
lmb = cont.getSensor("fire")
ray_all = cont.getSensor("ray_all")
add_force = cont.getActuator("add_force")
 
forceobj = add_force.getLastCreatedObject()
 
if own.force and str(ray_all.getHitPosition()) != 'None': # Here we convert the result of ray to a string
# and then check to make sure the ray result isn't 'None'
 
    hit_pos = ray_all.getHitPosition()
    forceobj.setOrientation(camown.getOrientation())
    forceobj.setPosition(hit_pos)
    own.force = 0
 
apply_force = ray_all.isPositive() and lmb.isPositive()
 
if apply_force:
    GameLogic.addActiveActuator(add_force, 1)
    own.force = 1

A straight port of this code to the 2.49 API will work for a little while, and you get this error message

ERROR: ‘nonetype’ object has no attribute named ‘orientation’

it doesn’t have the position attribute either.

After shuffling the ported code a few times only for it to fail I trimmed off some of the fat to make a more slim script, but functions the same that checks the mouse before executing commands instead


cont = GameLogic.getCurrentController()
own = cont.owner
scene = GameLogic.getCurrentScene()
 
Lmb2 = cont.sensors["fire"]
Ray_all = cont.sensors["ray_all"]
Rbullet = cont.sensors["ray_bullet"]
Add_force = cont.actuators["add_force"]
 
if Rbullet.positive==True:
    CAob=scene.objects["OBcamera0"]
    CAorient=CAob.orientation
    hit_pos = Ray_all.hitPosition
 
    if Ray_all.positive==True and Ray_all.hitPosition != 'none':
        cont.activate(Add_force)
        forceobj = Add_force.objectLastCreated
        forceobj.orientation=CAorient
        forceobj.position=hit_pos

After 5 minutes or so it still worked beautifully, I was still able to shoot crates and make them move and tumble, the reason I found why it worked is because while you’ll still have the same error printed in the console, the fact that the script is forced to re-initialize the ‘forceobj’ property to be the last created object every time you shoot makes it work, as in if you tap, tap, tap, slowly, if it doesn’t work you tap again and it works.

It’s not an error-free way but at least I can test the game and shoot stuff after 3 minutes.

Now what about your examples of what I asked?

shouldn’t this work better?


cont = GameLogic.getCurrentController()
own = cont.owner
scene = GameLogic.getCurrentScene()

if cont.sensors["fire"].positive:
    ray_all = cont.sensors["ray_all"]
    # XXX Isnt the hitPosition check trivial? i have a very strong feeling it is, 
    # but since i didn't run the code even once, i'll leave it there, corrected 
    # for your viewing pleasure.
    if ray_all.positive and ray_all.hitPosition: 
        camera = scene.objects["OBcamera0"]
        add_force = cont.actuators["add_force"]
        add_force.instantAddObject() # make sure we did add an object this frame :D
        forceobj = add_force.objectLastCreated
        # if for some really obscure reason the object wasn't created, 
        # or was deleted before this point, we'll be safe
        if forceobj and not forceobj.invalid: 
            forceobj.orientation = camera.orientation
            forceobj.position = ray_all.hitPosition

note: didn’t test since i don’t have the .blend

NEVER compare to None True or False using == / !=, much less using strings