Multiple addObject - setLinV not working?

Hi,

I have the following little script that adds an object (bullet) when the mouse left button is pressed.

  • addobject
  • setlinearvelocity

The icosphere is the bullet.
The cube is the gun.
It’s working well, the bullet is sent (see image below, yellow direction).

But
* Can’t set linearVelocity if first added object is still alive?
If I press left click before the first bullet has disappeared, the next added object (bullet) doesn’t have his linear velocity set (see image below, in red).

* Script played twice?
Also, the script is played twice even if I use the Tap option on the Mouse sensor.

Here is the script :

import GameLogic as g 
 
scene = g.getCurrentScene() 
objList = scene.objects 
cont = g.getCurrentController() 
own = cont.owner 
 
# sensor     
mouse_attack = cont.sensors["mouse_attack"] 
  
shoot_obj = objList.get("OBGun") 
bullet_obj = "Bullet1" 
bullet_life = 20 
bullet_speed = 10 
 

# Add Bullet at Gun position 
add_obj = scene.addObject(bullet_obj, shoot_obj, bullet_life) 
 
obj = scene.objects.get(add_obj.name, "OBBullet_1") 
obj.setLinearVelocity([0.0, bullet_speed, 0.0], True) 
print "done"

and an example .blend

If somebody can help? :slight_smile:

Attachments


example.blend (137 KB)

thou must use the “positive” variable to check if the click was just pressed or just released, check it

Attachments

example.blend (137 KB)

I thought that the Tap sensor would filter the second pulse.
But in fact, i understand that it’s sending two pulses : a positive and a negative one.
Thank you.

Now, my main problem is still :
Can’t set linearVelocity if first added object is still alive?

hi Cray,

You’re trying to use the get method to return a KX_GameObject and it doesn’t do that. It returns the value of a property on a KX_GameObject. That’s what is causing your problem with setLinearVelocity.

Take a look at the blend I attached. Notice that I replaced the first get function:

shoot_obj = objList.get(“OBGun”)
with
shoot_obj = objList[“OBGun”]

and deleted the second get function. It didn’t need to be replaced. You already had the bullet game object. (addObject returns the KX_GameObject for the object being added.)

Clark

Attachments

example_Bullet_Cray.blend (138 KB)

Hi Clark,

thank you very much for the .blend.
Now that I think about it, I don’t know why I tried to get that object twice…
Sometimes when working on big projects, we just get stucked with very easy things.
Or maybe it’s just me :slight_smile:

About objList.get(“OBGun”)
This returns no error if the other areas of the script are corrected (.addObject)
I got that syntax from that thread : New methods in 2.49 ob.get(), ob.has_key() by ideasman42


These 2 functions work for GameObjects and GameLists
ob = sce.objects.get("OBfoo") # OBfoo or None
ob = sce.objects.get("OBfoo", ob_default) # OBfoo or ob_default

I believe that this is the new way to do?

Thank you again.

I do not think the problem is the get() funktion the problem is that added objects all have the same name. If you request an object with an specific name from the scene and there are more than one, you will get ANY object with that name. In your case the first bullet. So you applied the velocity to the first bullet twice and nothing tho the second.

The example from cthames correctly uses the reference to the newly created object returned by addObject().