Python: Object's Life

Hi!

The API has a ‘life’ attribute where it can only be used by Add Object Actuators. Now, is there any way to get an object’s life attribute where the object is created through scene.addObjects()?

Thanks!

I think this option was added to make the AddObjectActuator supportive towards (game) particles to limit the number of simultaneous particles. I do not think there is much support on this.

Alternatives:
Use the KX_SCA_AddObjectActuator.instantAddObject() so it uses the life parameter of the actuator. But I have no idea if it is used at all.

Let the object end itself.

Track the added objects and its life time. End them when time is up (via a managing object).

Super fast response!

OK! I’ll track it using a timer.

Thank you Monster Sifu!

From the API:

The number of seconds until the object ends, assumes 50fps. (when added with an add object actuator), (read-only).
Type: float

I think it has something to do with Bullet. Read somewhere that bullet assumes 50fps for simulations. Maybe it represents how long Bullet should calculate physics for it? I assume that addObject()'s “time” does this then ends the object.

@MrPutuLips
Good to know that. I’ve always been wondering why the 50fps instead of 60fps. Thanks!

Have a look at this bug report: T40402 addObject() ‘time’ parameter inconsistent. It was recently reported by myself, after investigating the ‘life’ attribute in relation to the ‘time’ parameter of addObject().

So when adding objects with addObject(), the life parameter is updated, but not consistent with the logic tic rate. As mentioned it assumes one second is 50 logic tics, so if the current logic tic rate is 60 and you would set the ‘time’ parameter to 50, then the added object will end after 1 second (60 tics).

Well, after testing with a new blend file, the ‘life’ attribute of a newly created object through scene.addObject() DOES work!

new_obj = scene.addObject(obj, launcher, 0)
print(new_obj.life)

The misleading API’s fault. Just need to live with it.

@Raco
Glad that you’ve submitted the report.

According to Moguri, this probably isn’t worth fixing, because it would break existing games. He also pointed out that by using the time module and KX_GameObject.endObject() you can get accurate results. This would require to run a Python script on the added object which renders the ‘time’ parameter and ‘life’ attribute useless with frame rates other then 50fps.

It’s unfortunate, but maybe sometime in the future this could be changed. Unless it could be fixed without breaking existing games.