Display a score in TGB using particles.
by RollerJesus · 05/23/2010 (7:22 am) · 16 comments
I know you've seen games where you get an item or something a a nice little score floats up above the spot where you picked up said item. It looks nice but who wants to make a ton of images for all the different possible scores???
I created this SIMPLE particle system that accepts a score (numeric only) and a position (can be fired from a mouse event, collision, etc) that does simply that. It floats up a bitmap based score of any length:
1, 15, 200, 14032235.... Whatever.
You can download the effect and base images here and the code to use it is very simple. Essentially, we just create an instance of the particle effect, copy the emitter for the number of digits in the score and shift them over a bit for each one, then fire the effect.
Extract the number directory to your /data/images folder and add each of the images as a material.
Create a linked image map (using the button in the editor) and add all the number images in order. Name the image map numbersLinkImage.
Add the following code to a new cs file:
Make sure that you have the effect (Score.eff) file in your data/particles directory and execute the new *.cs file with the code above.
Then issue a script command like so and you'll see a nice looking score pop up. Cool!
I have a plan to extend to support all numbers and characters but it needs quite a bit of leg work - which I'm partially through - and will post an update when I can.
Patrick
I created this SIMPLE particle system that accepts a score (numeric only) and a position (can be fired from a mouse event, collision, etc) that does simply that. It floats up a bitmap based score of any length:
1, 15, 200, 14032235.... Whatever.
You can download the effect and base images here and the code to use it is very simple. Essentially, we just create an instance of the particle effect, copy the emitter for the number of digits in the score and shift them over a bit for each one, then fire the effect.
Extract the number directory to your /data/images folder and add each of the images as a material.
Create a linked image map (using the button in the editor) and add all the number images in order. Name the image map numbersLinkImage.
Add the following code to a new cs file:
//score should be the numbers only!!!
function showScore(%score, %startPos)
{
echo("Showing score: " @ strlen(%score));
for(%i = 0; %i < strlen(%score); %i++)
{
%xPos = getWord(%startPos, 0) + (%i * 5);
%yPos = getWord(%startPos, 1);
%newPos = %xPos SPC %yPos;
playScoreEffect(getSubStr(%score,%i,1), %newPos);
}
}
function playScoreEffect(%score, %pos)
{
%scoreEffect = new t2dParticleEffect()
{
scenegraph = sceneWindow2D.getSceneGraph();
};
%particlePath = "~/data/particles/Score.eff";
%scoreEffect.loadEffect(%particlePath);
%scoreEffect.setEffectLifeMode("KILL", 2);
%scoreEffect.setPosition(%pos);
%emitter = %scoreEffect.getEmitterObject(0);
%emitter.setImageMap("numbersLinkImage", %score);
%scoreEffect.playEffect();
}
function t2dParticleEffect::onStopEffect(%this)
{
echo("deleting");
%this.safeDelete();
}Make sure that you have the effect (Score.eff) file in your data/particles directory and execute the new *.cs file with the code above.
Then issue a script command like so and you'll see a nice looking score pop up. Cool!
showScore(1234, "0 25");Let me know if you have any questions, improvements, etc.
I have a plan to extend to support all numbers and characters but it needs quite a bit of leg work - which I'm partially through - and will post an update when I can.
Patrick
About the author
#2
Thanks Kevin!
Patrick
05/23/2010 (4:13 pm)
Ahhh yes, quite the oversight on my part. I updated the instructions above to reflect the change.Thanks Kevin!
Patrick
#3
very smart and pretty resource, thanks for sharing.
I suggest to modify the scenegraph assignment
with
I think this could be more easy to use for novice because sceneWindow2D is always present into default new projects.
Also, there's a typo in the resource description: the linked imagemap name should be numbersLinkImage, and not numbersLinkedImage.
05/23/2010 (5:18 pm)
Hi Patrick,very smart and pretty resource, thanks for sharing.
I suggest to modify the scenegraph assignment
scenegraph = $SG;
with
scenegraph = sceneWindow2D.getSceneGraph();
I think this could be more easy to use for novice because sceneWindow2D is always present into default new projects.
Also, there's a typo in the resource description: the linked imagemap name should be numbersLinkImage, and not numbersLinkedImage.
#4
Thanks for the feedback; very much appreciated!
I incorporated your change/correction into the main resource.
`Patrick
05/23/2010 (7:26 pm)
Giuseppe,Thanks for the feedback; very much appreciated!
I incorporated your change/correction into the main resource.
`Patrick
#5
It would be nice to have the option to extend the life, but increasing the 2 here:
%scoreEffect.setEffectLifeMode("KILL", 2);
has no affect.
05/24/2010 (5:43 am)
Here are a couple of changes I made to this in my project:function showScore(%score, %startPos)
{
%L=strlen(%score);
for(%i = 0; %i < %L; %i++)
{
//((%L/-2)*%L) = keep it centered
%xPos = getWord(%startPos, 0) + ((%L/-2)*%L) + (%i * 5);
%yPos = getWord(%startPos, 1);
%newPos = %xPos SPC %yPos;
playScoreEffect(getSubStr(%score,%i,1), %newPos);
}
}
function playScoreEffect(%score, %pos)
{
%scoreEffect = new t2dParticleEffect()
{
scenegraph = glMain;
};
%particlePath = "~/data/particles/Score.eff";
%scoreEffect.loadEffect(%particlePath);
%scoreEffect.setEffectLifeMode("KILL", 2);
%scoreEffect.setPosition(%pos);
%emitter = %scoreEffect.getEmitterObject(0);
%emitter.setImageMap("numbersLinkImage", %score);
%scoreEffect.playEffect();
%scoreEffect.kill=true; //do not kill my other stopped effects
}
function t2dParticleEffect::onStopEffect(%this)
{
if (%this.kill){
%this.safeDelete();
}
}It would be nice to have the option to extend the life, but increasing the 2 here:
%scoreEffect.setEffectLifeMode("KILL", 2);
has no affect.
#6
I actually had thought of Fangorodrim (sp?) when I was working on this; I hope it makes it in there!
When I get home tonight I can tell which emitter graph to edit to make them a little longer.
Patrick
05/24/2010 (6:44 am)
@Kevin,Quote:Nice!
getWord(%startPos, 0) + ((%L/-2)*%L) + (%i * 5);
I actually had thought of Fangorodrim (sp?) when I was working on this; I hope it makes it in there!
Quote:I have to say that I am growing to both love and HATE the particle system in TGB. :) That is most likely not working because I'm setting the particle life/quantity/speed at the emitter but I don't see a way to modify the emitter graphs via script. Hopefully someone else who knows them well could weigh in...
It would be nice to have the option to extend the life, but increasing the 2 here:
%scoreEffect.setEffectLifeMode("KILL", 2);
When I get home tonight I can tell which emitter graph to edit to make them a little longer.
Patrick
#7
You will see this in v1.25 of Fangorodrim in the home sector of the aliens. It gives you an update every 5 seconds of how much health is left on the nest.
05/24/2010 (7:33 am)
I looked at the emitter in the designer, thinking I would set it to infinite which should give me the ability to change its life via code, but the life mode can not be changed in the designer: it shows up blank and will not change.You will see this in v1.25 of Fangorodrim in the home sector of the aliens. It gives you an update every 5 seconds of how much health is left on the nest.
#8
To extend the 'life' of the particle, the easiest way I found was to drag the 3rd and 4th nodes in the "Visibility Life" graph to the right until it extends to your desired tastes.
Looking forward to seeing it in the game!
Patrick
05/24/2010 (7:03 pm)
@Kevin,To extend the 'life' of the particle, the easiest way I found was to drag the 3rd and 4th nodes in the "Visibility Life" graph to the right until it extends to your desired tastes.
Looking forward to seeing it in the game!
Patrick
#9
06/25/2010 (8:05 am)
I took your advice by extending the life of the particle. You'll see your affect in the Alien Nest sector of Fangorodrim (v1.55)
#10
07/01/2010 (9:53 pm)
Nice. I was using t2dtextobject and attaching it to a spawned path. This is much better, gonna use it in my current project.
#11
07/02/2010 (6:19 am)
Happy to hear you're getting some use out of it! Post back with suggestions/improvements/etc please.
#12
daaaaa. Bump. Retard here, no coffee yet. Data is already there!!
10/26/2010 (3:21 pm)
Hey Patrick, Is there anyway i could get the actual effects code from you. I am planing to make a Behavior out of it with more control options so you can just attack it to objects that will take damage and it handles the reset and then put it up on the behaviors section.daaaaa. Bump. Retard here, no coffee yet. Data is already there!!
#13
The effect file is in a link at the top. Do you need something else?
10/26/2010 (3:31 pm)
@Andrew,The effect file is in a link at the top. Do you need something else?
#14
03/01/2011 (7:17 pm)
hi patrick!!! the effect is blank in my project? is it only able to see the effect into the game project?
#15
That's correct; the effect is created and run when the game is running by executing something like where 1234 is the score and "0 25" is the position you want it to appear:
03/02/2011 (5:50 am)
@Selin,That's correct; the effect is created and run when the game is running by executing something like where 1234 is the score and "0 25" is the position you want it to appear:
showScore(1234, "0 25");Give it a try and let me know if you have any problems.
#16
01/19/2013 (3:27 pm)
This is a pretty rad effect. Thanks for sharing! 
Torque Owner Kevin James
t2dParticleEmitter::setImageMap() - t2dImageMapDatablock Datablock is invalid! (numbersLinkImage)
I assume there is an image map that needs added in the level designer?