User Tools

Site Tools


en:tutorials:objects:lifetime

This is an old revision of the document!


Object Lifetime

Objects have the concept of a lifetime. This is where you can specify a value in seconds, and the object will live only for that amount of time. This is perfect for spawned objects like bullets, or temporary objects.

But you can also specify one or more literals to create lifetime situations that are a little richer.

Before getting into that, let's create a blank project using the init script.

Numeric Lifetime

Let's begin will the most straight forward example, a numeric lifetime. Add a lifetime of 5.0 to the Object section:

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = 5.0

Compile and run. The logo object will turn for 5 seconds and then disappear. It's dead. Completely gone.

FX Lifetime

The fx literal means that the object's life will only end after any fx that are attached end first.

Change the object's lifetime to fx:

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = fx

The fx currently attached to the object is in a loop, so this condition will never be reached.

Change the loop property on the ColorCycle fx to false:

[ColorCycle]
Loop            = false
...

Then run that. The logo will cycle its colour once and then its life will end. Nice.

Sound Lifetime

In this case, if there is a sound attached to the object which plays on object creation, the object will die once the sound finishes playing. This is a nice enhancement over time based lifetimes because if you decide to change the sound your object uses, you no longer need to be mindful of how long or short the sound goes for.

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = sound 

Run that and as soon as the appearance sound is finished, the object's life is over.

Child Lifetime

For the last three lifetime literal types, I won't go into any code examples. I'll leave that as an exercise for the reader, as they simply follow the same principle.

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = child
ChildList       = Child1 # Child2 # Child3

A child lifetime is one where the object's lifetime does not end unless the lifetime of its children ends first. For example, you might have a boss alien made up of many parts. Once all the parts are destroyed, the final parent object will finally be removed.

Spawn Lifetime

In a similar manner to the child lifetime literal, a spawn lifetime is one where the object's life does not end until it finishes spawning.

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = spawn
Spawner         = MySpawner

This operates on the TotalObjects property of the spawner. Once the spawned objects are exhausted, the object will die. Clearly if the spawner is set to infinite spawning, the lifetime will not work.

Track Lifetime (in development)

Using this literal, the object's life will end when the attached track has finished it's job.

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = track
Track           = MyTrack

Multiple Lifetime literals

You can combine two or more literals, or a value if you wish. If you do this, the object's lifetime will not end until all of the conditions are exhausted. For example:

[Object]
Graphic         = @
Texture         = logo.png
SoundList       = @
Sound           = appear.ogg
Pivot           = center
AngularVelocity = 18
FXList          = FadeIn # ColorCycle
LifeTime        = 15 # fx

In this case, the fx will complete it's single cycle, and then the object will continue to live for a total of 15 seconds. Once both conditions are complete, it's curtains for the object.

Conclusion

There's a lot of combinations and scenarios that be covered with multiple lifetimes. I can see sound, fx and track being extremely useful. You can get more info on usage at: orxOBJECT structure

en/tutorials/objects/lifetime.1559136315.txt.gz · Last modified: 2019/05/29 09:25 (5 years ago) (external edit)