Creating ScoreCounter in Starter.fps
by Mike Duggan · in Technical Issues · 08/08/2007 (1:17 pm) · 4 replies
I am using the Getting Started PDF guide that ships with Torque to create what -- to me -- seems like it would be a very simple script. I want to create a ScoreCounter GUI, place objects on the map, and attach the ScoreItem dataBlock to it. I seem to have no problems doing that. I had to use some ingenuity, because I'm attempting to do it using Starter.fps as a template instead of Tutorial.base.
I made a GUI widget for ScoreCounter, and I placed my code snippets as follow:
// I put this in at the bottom of starter.fps\server\scripts\crossbow.cs:
datablock ItemData(ScoreItem)
{
// Mission editor category
category = "ScoreItem";
// Hook into Item Weapon class hierarchy.
className = "ScoreItem";
// Basic Item properties
shapeFile = "~/data/shapes/coins/coin.dts";
mass = 1;
elasticity = 0.2;
friction = 0.6;
emap = true;
// Dynamic properties defined by the scripts
pickUpName = "a coin";
}
function ScoreItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
%client.score++;
commandToClient(%client, 'SetScoreCounter', %client.score);
%obj.delete();
%coinCount = coins.getCount();
if(%coinCount > 0)
return;
// otherwise display victory screen
commandToClient(%client, 'ShowVictory', %client.score);
}
}
// Then I created a new script file called starter.fps\client\scripts\clientMsg.cs and I coded this in it:
function clientCmdSetScoreCounter(%score)
{
ScoreCounter.setText("Score:" SPC %score);
}
function clientCmdShowVictory(%score)
{
MessageBoxYesNo("You Win!",
"Would you like to restart the game ?",
"loadMyMission();",
"quit();");
}
// Then I opened starter.fps\client\init.cs and where there are all the client includes I called:
exec("./client/clientMsg.cs");
I double checked all my code, I deleted old cs.dso files, and then I launched the game mission, opened the editor, placed my coin objects around my map, and made sure they all share the ScoreItem dataBlock and are collidable. I also put them into a single SimGroup called coins. My character on-screen froze at that point, so I saved the mission, exited and then relaunched it.
My character can move around again, and I can find all the coins. Unfortunately, whenever I walk over a coin I just go right through it and nothing happens. I also noticed that I can't look around with the mouse anymore, although I can still right-click + Tab key to switch to 3rd person. Something serious must have gone wrong.
I'm in serious straits here! Can someone help me? I just need the player character to be able to pick up objects and have them add to his overall score. If there's an easier way to do it, please let me know!
I made a GUI widget for ScoreCounter, and I placed my code snippets as follow:
// I put this in at the bottom of starter.fps\server\scripts\crossbow.cs:
datablock ItemData(ScoreItem)
{
// Mission editor category
category = "ScoreItem";
// Hook into Item Weapon class hierarchy.
className = "ScoreItem";
// Basic Item properties
shapeFile = "~/data/shapes/coins/coin.dts";
mass = 1;
elasticity = 0.2;
friction = 0.6;
emap = true;
// Dynamic properties defined by the scripts
pickUpName = "a coin";
}
function ScoreItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
%client.score++;
commandToClient(%client, 'SetScoreCounter', %client.score);
%obj.delete();
%coinCount = coins.getCount();
if(%coinCount > 0)
return;
// otherwise display victory screen
commandToClient(%client, 'ShowVictory', %client.score);
}
}
// Then I created a new script file called starter.fps\client\scripts\clientMsg.cs and I coded this in it:
function clientCmdSetScoreCounter(%score)
{
ScoreCounter.setText("Score:" SPC %score);
}
function clientCmdShowVictory(%score)
{
MessageBoxYesNo("You Win!",
"Would you like to restart the game ?",
"loadMyMission();",
"quit();");
}
// Then I opened starter.fps\client\init.cs and where there are all the client includes I called:
exec("./client/clientMsg.cs");
I double checked all my code, I deleted old cs.dso files, and then I launched the game mission, opened the editor, placed my coin objects around my map, and made sure they all share the ScoreItem dataBlock and are collidable. I also put them into a single SimGroup called coins. My character on-screen froze at that point, so I saved the mission, exited and then relaunched it.
My character can move around again, and I can find all the coins. Unfortunately, whenever I walk over a coin I just go right through it and nothing happens. I also noticed that I can't look around with the mouse anymore, although I can still right-click + Tab key to switch to 3rd person. Something serious must have gone wrong.
I'm in serious straits here! Can someone help me? I just need the player character to be able to pick up objects and have them add to his overall score. If there's an easier way to do it, please let me know!
#2
im not sure this solves everything, but you use a SimGroup, though I dont see you adding the coins to that simgroup. 2 posible solutions:
add something like: coins.add(%obj); to a ScoreItem::onAdd method
or you could choose not to use a simGroup like this, but spawn them in a specific group in your mission editor:
in mission editor add a simgroup (missionObjects->system->simgroup) and then alt-click it to select it, then place your coins and they will be a part of this group (or just do this text-editting the .MIS file). for example call this simgroup Coins.
then instead of your onCollision method add this one:
function ScoreItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
%client.score++;
commandToClient(%client, 'SetScoreCounter', %client.score);
%obj.delete();
%coinCount = nameToID(MissionGroup/Coins).getCount();
if(%coinCount > 0)
return;
// otherwise display victory screen
commandToClient(%client, 'ShowVictory', %client.score);
}
}
I think this should work. if not please post again ill try to be of help
edit: maybe there should be "" around MissionGroup/Coins, but i think this is correct
08/12/2007 (2:13 am)
Mike, im not sure this solves everything, but you use a SimGroup, though I dont see you adding the coins to that simgroup. 2 posible solutions:
add something like: coins.add(%obj); to a ScoreItem::onAdd method
or you could choose not to use a simGroup like this, but spawn them in a specific group in your mission editor:
in mission editor add a simgroup (missionObjects->system->simgroup) and then alt-click it to select it, then place your coins and they will be a part of this group (or just do this text-editting the .MIS file). for example call this simgroup Coins.
then instead of your onCollision method add this one:
function ScoreItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
%client.score++;
commandToClient(%client, 'SetScoreCounter', %client.score);
%obj.delete();
%coinCount = nameToID(MissionGroup/Coins).getCount();
if(%coinCount > 0)
return;
// otherwise display victory screen
commandToClient(%client, 'ShowVictory', %client.score);
}
}
I think this should work. if not please post again ill try to be of help
edit: maybe there should be "" around MissionGroup/Coins, but i think this is correct
#3
There's that and a minor spelling error on my side which I had fixed above and hadn't even noticed. I have the code fixed and working, and the editors are pleased. The book is done and is at the printers now.
Thanks for your suggestion, though!
08/12/2007 (9:41 am)
Thanks, Tijs. I meant to post earlier. A couple of days ago I found my problem and fixed it. I was using a SimGroup; that wasn't my problem. If you looked above, you'll notice the [exec("./client/clientMsg.cs");] was calling the clientMsg.cs file from the client folder, when in fact I had put that CS file in the client/scripts folder.There's that and a minor spelling error on my side which I had fixed above and hadn't even noticed. I have the code fixed and working, and the editors are pleased. The book is done and is at the printers now.
Thanks for your suggestion, though!
#4
08/12/2007 (11:23 am)
Hmm then you havent posted the entire code though, you in no place use the add method to add the coins in the simgroup, or am I mistaken? anyways, good luck with your book :)
Mike Duggan