addObject and applyMovement question

def fire():
    cont = bge.logic.getCurrentController()
    owner = cont.owner
    scene = bge.logic.getCurrentScene()
    keyboard = bge.logic.keyboard
    num4Key_act = bge.logic.KX_INPUT_JUST_ACTIVATED ==[]

    if num4Key_act:
        a = scene.objects["Empty_player_hit_box_missile"]
        ob1 = scene.addObject("missile1",a,120)

Why isn’t my object moving when added to the scene?

It does if i replace the a with owner in the addobject line.

appyMovement is just an impulse that teleports the object by a certain amount. If the object is dynamic or rigidBody, use applyForce or applyImpulse

This should work:

scene.addObject("missle1",a,120).applymovement([0,2,0], True)

instead of:

ob1 = scene.addObject("missile1",a,120)        

Thanks Kevin, it works. But i just remembered i don’t want the object to be affected by gravity, or at least prevent it from moving along the Z axis.

I can do it with the logic bricks, using motion, liniar along my y axis with value of 20, and next to it there is an ‘L’ and an ‘A’ where i only checked the L.

When looking at
i only see setLinearVelocity(velocity, local=False) - I can’t see the attributes(?) for the L and A, so i can write it in code.

It’s not super important, just trying to learn this stuff and move some logic bricks into code :slight_smile:

Edit: ahh, its just setLinear… and addLinear…
But, it decents when using the code, and not when using bricks.

Okay, it’s because on logic bricks i have pulse mode, and in my code i have just activated on keyboard.

Looks like i have to use bricks with this one. oh well. Thanks for the replies.

if you want the python code to be activated with pulse mode you can just attach an always sensor that is on pulse mode.

Yep i know, but thanks :slight_smile:

Just gotta seperate functions that need to be in a true level triggering impulse script, and those that don’t.

My script for firing a missile don’t have to run all the time, but the setLinearVelocity does.

That reminds me… can i write in a script import, and use variables from it? Can they… speak to each other?
I sort of miss some kinds of global variables, instead of assigning game properties.

I recommend following:

  • For thumb projectiles (like cannon balls) use the addObject actuator it allows to set up an initial velocity via GUI.
  • For a rocket like behavior add the “engine” to the rocket rather than the barrel. This means the rocket moves itself forward (Motion actuator)
  • To cancel out gravity apply a constant force against the scene gravity (global Z). Take care of the projectiles mass. The motion actuator fits perfect in this situation.

Keep “firing projectile”, “running rocket” and “cancelling out gravity” separate. You will see it makes your design much simpler.

If you miss “global variables” you most-likely have a weak design.
No, you can’t use variables form other Python controllers. Why would you do that? Your code should not know how the other code works. You can access the properties of the game objects. So you can use them for communication. Also message are pretty good with such things.

I hope it helps.