how to setup a scoring/points system
by hbomega · in Torque 2D Beginner · 08/09/2013 (10:16 pm) · 17 replies
i wanna implement a scoring system as simple as adding a specified number of points to totalpoints each time a certain collision happens and have totalpoints reflect the changes immediately, example like what happens each time mario collects a coin,
#2
08/10/2013 (8:45 am)
Thanks Simon i got it first i created the ScriptObject with all of my global stats then i passed them to individual Imagefont objects works great so far thanks again
#3
Also, if you want to save these to a file, simply use
All the fields you've added will be saved to file with their current value.
Then read it using TamlWrite.
08/10/2013 (9:53 am)
No prob!Also, if you want to save these to a file, simply use
TamlWrite(GlobalStats, "/modules/MyModule/Stats/Stats.taml");
All the fields you've added will be saved to file with their current value.
Then read it using TamlWrite.
#4
08/10/2013 (8:22 pm)
thanks for the info i'm pretty sure i'll be using it soon, and it will probably tie in to what i'm about to ask, i want to start putting some sense/ logic to my project, example, having multiple scenes, logical switching of scenes etc, i believe what i want to do is learn how to add multiple scenes to my project, would i just create a new scene example i only have "myscene" would i create "myscene1" from scratch, and about the game logic or loop how do i go about that do i create a new script file example gameloop.cs please help any form of guidance is greatly appreciated
#5
There is no 'main' loop as T2D is event-driven. This means that everything is driven by Collisions, input events and behaviors.
If you want to execute a function which resembles a main loop, you could run a function every few seconds by scheduling it.
In this example, the schedule function will execute in 1000 milliseconds (1 second = 1000 milliseconds).
--------------
Concerning Scene Switching :
If you need to keep track of the current scene, you might add a $CurrentScene global variable and set it to the currently loaded Scene.
You can always call SceneObject.getScene(); to know if the object is in the current scene as well.
In all cases, you must load or create the new scene before displaying it. You can read it from .taml, create it from scratch, however you choose to do it.
Here are a few ways to handle multiple scenes :
- Load new scenes into the current one
First, create your new Scene, then, call MyScene.merge(nextScene);. This will clone the objects of nextScene into MyScene. Make sure to delete the current contents of MyScene beforehand else you might have objects from both in MyScene.
- Load a new Scene and switch to it.
To do so, first, pause the current scene with MyScene.setScenePause(true) and then simply call SceneWindow.setScene(nextScene);
You can either keep your previous Scene paused in the background or delete it, whatever your game requires.
- Fade out from one Scene to the next
This is tricky and I've never tried it fully but if you create a Sprite in MyScene that is bigger than the camera size, set it to layer 0.
Simply run a schedule which gradually makes the alpha of the object to be more opaque as time goes on. Once it completely hides the Scene, switch Scenes and do the opposite thing, making the Sprite more and more transparent until the new scene is fully revealed.
There are many techniques to make cool Scene Transitions, play around with the engine and you might find new ones!
08/10/2013 (9:33 pm)
Concerning the gameloop.cs question : I'm not sure exactly what you want. Creating a new file (gameloop.cs), just call exec("./scripts/gameloop.cs";) in your main.cs file to load it and be able to access the functions defined within in.There is no 'main' loop as T2D is event-driven. This means that everything is driven by Collisions, input events and behaviors.
If you want to execute a function which resembles a main loop, you could run a function every few seconds by scheduling it.
In this example, the schedule function will execute in 1000 milliseconds (1 second = 1000 milliseconds).
function MainGameLoop()
{
...
//Main loop stuff
...
schedule(1000,0,MainGameLoop);
}--------------
Concerning Scene Switching :
If you need to keep track of the current scene, you might add a $CurrentScene global variable and set it to the currently loaded Scene.
You can always call SceneObject.getScene(); to know if the object is in the current scene as well.
In all cases, you must load or create the new scene before displaying it. You can read it from .taml, create it from scratch, however you choose to do it.
Here are a few ways to handle multiple scenes :
- Load new scenes into the current one
First, create your new Scene, then, call MyScene.merge(nextScene);. This will clone the objects of nextScene into MyScene. Make sure to delete the current contents of MyScene beforehand else you might have objects from both in MyScene.
- Load a new Scene and switch to it.
To do so, first, pause the current scene with MyScene.setScenePause(true) and then simply call SceneWindow.setScene(nextScene);
You can either keep your previous Scene paused in the background or delete it, whatever your game requires.
- Fade out from one Scene to the next
This is tricky and I've never tried it fully but if you create a Sprite in MyScene that is bigger than the camera size, set it to layer 0.
Simply run a schedule which gradually makes the alpha of the object to be more opaque as time goes on. Once it completely hides the Scene, switch Scenes and do the opposite thing, making the Sprite more and more transparent until the new scene is fully revealed.
There are many techniques to make cool Scene Transitions, play around with the engine and you might find new ones!
#6
08/11/2013 (4:26 am)
@Simon: lots of useful info, thanks.
#7
08/11/2013 (8:25 am)
THanks again Simon, and i have the feeling i'm gonna be thanking you for some time to come i can now switch scenes thanks again, but they switch too fast i tried setting timers but they don't work as expected maybe i'm going about it wrong, i'm trying to create splash screens that last whatever time i wish.
#8
Since it saves it nicely by adding the 'GlobalStats' when you TamlWrite, is there something similar to load all that information back into GlobalStats using a TamlRead?
08/14/2013 (4:42 am)
When you use TamlRead, how do you get that information back into the variable (in your example, GlobalStats).Since it saves it nicely by adding the 'GlobalStats' when you TamlWrite, is there something similar to load all that information back into GlobalStats using a TamlRead?
#9
08/14/2013 (7:16 am)
I think you are mixing things up Christian. GlobalStats in Simon's example is not a variable, it is a ScriptObject. Since it is an object, you can assign custom fields to it - which is what Simon did for keeping track of coins and the score. When you write an object out in TAML, you are saving the names of those fields and their values. When you read them back in via TamlRead, the engine recreates the ScriptObject with all the fields and values that you had when you wrote it to disk.
#10
Thank you.
08/14/2013 (2:19 pm)
Mike your right, I was using a $global_variable instead of a script object. It was writing fine, but now that it's a script object it can read it too.Thank you.
#11
08/21/2013 (2:43 am)
Does each script object you create need to be saved in a separate TAML file? Or is there a way to combine them into one file?
#12
Originally I wanted to take my human_player object and make a 'list of lists' to contain all the characters the player had collected, like so:
human_player.crew[1].level = 5;
human_player.crew[2].level = 1;
etc....
That didn't work so I switched to just adding a new script object for a 'crew' class.
new ScriptObject(crew);
crew[1].level = 5;
crew[2].level = 5;
But this doesn't work either.
Is there a way to make that script object into something that can hold an array?
If not, how would you handle saving new 'crew' as the player acquires them?
08/21/2013 (2:53 am)
Before that question, is it possible to create a script object array?Originally I wanted to take my human_player object and make a 'list of lists' to contain all the characters the player had collected, like so:
human_player.crew[1].level = 5;
human_player.crew[2].level = 1;
etc....
That didn't work so I switched to just adding a new script object for a 'crew' class.
new ScriptObject(crew);
crew[1].level = 5;
crew[2].level = 5;
But this doesn't work either.
Is there a way to make that script object into something that can hold an array?
If not, how would you handle saving new 'crew' as the player acquires them?
#13
You can group any type of objects into a Simset and save the Simset to .taml.
------------------------------------------------
Question 2 - Again, you can use a Simset to group objects together and then do something like this :
to find objects within the simset.
08/21/2013 (3:35 am)
Quote:Does each script object you create need to be saved in a separate TAML file? Or is there a way to combine them into one file?
You can group any type of objects into a Simset and save the Simset to .taml.
%MySet = new Simset(); %Script1 = new ScriptObject(); %Script2 = new ScriptObject(); %Script3 = new ScriptObject(); %MySet.add(%Script1); %MySet.add(%Script2); %MySet.add(%Script3); TamlWrite(%Myset, "Myset.taml");
------------------------------------------------
Question 2 - Again, you can use a Simset to group objects together and then do something like this :
for(%i=0;%i<%mySimset.count;%i++)
{
%object = %mySimset.getObject(%i);
echo(%object.level);
}to find objects within the simset.
#14
The %array[x].sweet variable can be anything you want, level, hp, experience, etc. TorqueScript may be slow but this type of weirdness allows for really complex concepts to be prototyped quickly.
08/21/2013 (3:42 am)
Or, use a %local or $Global variable as follows :%array[0] = new ScriptObject(); %array[0].sweet = "Boo"; %array[1] = new ScriptObject(); %array[1].sweet = "Ya"; %array[2] = new ScriptObject(); %array[2].sweet = "Ka"; echo(%array[0].sweet); echo(%array[1].sweet); echo(%array[2].sweet);
The %array[x].sweet variable can be anything you want, level, hp, experience, etc. TorqueScript may be slow but this type of weirdness allows for really complex concepts to be prototyped quickly.
#15
It writes into the TAML file great, but when I use TamlRead() all the script objects aren't part of the global $crew_owned anymore.
Can they be fed into it?
08/21/2013 (5:01 am)
That was the issue a couple posts up, when using a $global variable$crew_owned = new Simset(); %crew_owned1 = new ScriptObject(); %crew_owned2 = new ScriptObject(); %crew_owned3 = new ScriptObject();
It writes into the TAML file great, but when I use TamlRead() all the script objects aren't part of the global $crew_owned anymore.
Can they be fed into it?
#16
One easy way to change this is to name the object
When read from .taml, it should recreate the Simset with the name Crew_owned.
Otherwise, I think you can try $crew_owned = TamlRead("myfile") and the top-most item (The Simset in this case) should be assigned to $crew_owned.
This solution brought to you by years of trial and error. :)
08/21/2013 (5:21 am)
That happens (I think) because the Global Variable is not an object name, as such, in your taml file, the Simset should not have any name.One easy way to change this is to name the object
%crew_owned = new Simset(Crew_owned);
When read from .taml, it should recreate the Simset with the name Crew_owned.
Otherwise, I think you can try $crew_owned = TamlRead("myfile") and the top-most item (The Simset in this case) should be assigned to $crew_owned.
This solution brought to you by years of trial and error. :)
#17
08/21/2013 (6:39 am)
$crew_owned = TamlRead() did the trick. You've once again been awesome Simon.
Associate Simon Love
For example $TotalPoints= 0;, simply add the $ symbol before the variable name to define it as a global variable.
Whenever your player object collides with another, $TotalPoints++; will add 1 to your points.
You can then either use a GuiText object or an ImageFont and set its text value to $TotalPoints.
If you want to be a bit more thorough, you can create a new ScriptObject for all your 'global stats'.
And then update this from wherever in your code using the following code. In this example, we add to the variable CoinsCollected and then modify the TotalScore (5 points for each Coin!) :