Adding some effects
The easiest way to make the game POP is to add some particle effects when we pick up the coins. Let's add the basic skeleton of a particle effect to the data/CoinCollection/datablocks/coin.tscript
file:
As you can see we define two new datablocks in this file, a ParticleEmitterDatablock
for creating a new particle emitter. Particle emitters does not have a place in the world by themselves tho, they only emit particles they need a node to know where to emit particles from. Therefore we need a datablock for a particle emitter node aswell. What you probably will notice here is when we define the name of the datablocks, we write a colon and then another name. What does this mean?
This means that our new datablock, inherits values from another datablock. It makes a copy of the other datablock and lets you edit the values which will only affect the new datablock.
Another thing that is important to note, is that CoinEmitter
references CoinParticle
. Which is why it is important that CoinParticle
is defined before CoinEmitter
.
On-the-fly instancing
Lets put these new datablocks to good use. We want some visual feedback to tell us that we have picked up a coin.
To spawn a new Emitter we will use the new
operator. It works like this:
Remember it is the node not emitter we want to spawn, then we set the emitter inside the “initialiser”. (What i call the initialiser is the variable definitions inside the two brackets { and }.)
This is where we define what datablock to use, the emitter and anything else we want to do with the newly created object.
We need to give this new object a position in the world. To get the position of an object you would call
And to set the position of an object you would assign it, like this
So now, let's utilise this to spawn emitters when we pickup coins, in data/CoinCollection/server/coin.tscript
add the instancing to the onCollision
callback:
Schedules and cleanup
If you run into a couple of Coins, and it is all working properly, then if you open the world editor you will notice that the emitters is still there even tho they stopped emitting particles (given that you gave the ParticleEmitter
a lifetime) if you didn’t you will see that they keep emitting particles.
We want to fix that! So I will introduce you to a very important feature in TorqueScript: schedules.
You can use a schedule to delete the emitter after some time.
The schedule syntax is:
Or if you are not calling it on an object:
We can use this to delete the emitter after we spawn it:
Customising the effect
Let's start by filling out the ParticleData
datablock
The particles is the billboards we are emitting, these are all cosmetic values, I won't dive into them here.
I set the softnessDistance
to 1. Softness distance refers to the concept of soft particles. It defaults to 1000
, so it is important to set this down to something reasonable, or else your particles will look transparent when the background is not kilometres away.
Last updated