Game Development Community

Using echo to return a value?

by Infinitum3D · in Torque Game Engine · 04/08/2009 (2:43 pm) · 10 replies

I know it can be done I'm just messed up on syntax. (I hate syntax errors, and I make them constantly!)

%playerInTrigger = 0;

function triggerAreaOne::onEnterTrigger(%this, %obj)

If (%client)
{
%playerInTrigger = %playerInTrigger++;
echo("There are", @ %get.playerInTrigger, "players in the trigger area!");
}

What is the proper syntax for returning the value of %playerInTrigger in an echo statement?

Thanks!
Tony

#1
04/08/2009 (2:46 pm)
And I know there are probably things wrong with the rest of the script, but I'm texting while driving, so forgive me :)

Just igonre the other mistakes.

Tony
#2
04/08/2009 (2:53 pm)
heya,
i'm not sure what you mean, can you rephrase the question ?

i can tell you that your echo statement isn't going to do what you want.
try this:

echo("There are " @ %playerInTrigger @ " players in the trigger area!");

also, you probably want to change %playerInTrigger to $playerInTrigger so that it's a global variable instead of a local one.

also, instead of %client you probably want %obj.client.

also, the body of your function needs to be enclosed in braces,
eg:
function foo()
{
   // do stuff
}

#3
04/08/2009 (3:19 pm)
Thanks Orion!

Basically I'm trying to keep track of how many players enter an area. I want my echo statement to tell me how many are in a trigger area at a given time. I'm trying to use onEnter and onLeave to ++ increment and -- decrement the value of %playerInTrigger as players enter/leave.

I thought about using messageClient, but I like echoes.

Thanks again.

Tony
#4
04/08/2009 (3:43 pm)
Quote:
I was going to use $ for global variables, but I have different trigger areas and I need to keep them separate, so I tried to keep things local to each specific area.

good thinking, except that by definition, local variables are deleted when whatever function they're in returns, so when you increment it, the increment won't have any effect.

you could use an global array variable using the simID of the trigger as the index, eg:
$playersInTrigger[%this.getId()] += 1;
#5
04/08/2009 (3:51 pm)
Sweet!

I think that will work for me. Does TGE have limits to the size of an array? For that matter, does TGE limit the number of trigger areas? I've been "torque'ing" for years and still know so little about it.

Thanks again!

Tony
#6
04/08/2009 (3:55 pm)
there's no limits on the size of a torquescript array, or on the number of trigger areas. yeah, there's a lot to learn about the engine!
#7
04/09/2009 (1:05 am)
Are you running this script on the server? If so, couldn't you store the number of players as a property of the trigger object?
%trigger.numberOfPlayers++;
#8
04/09/2009 (5:23 am)
I was declaring my PlayersInTrigger = 0; in the trigger datablock like this:

datablock TriggerData(ZoneTrigger)
{
tickPeriodMS = 500;
PlayersInTrigger = 0;
};

function ZoneTrigger::onEnterTrigger(%this, %trigger, %obj)
{
if(%obj.client) //-Player enters trigger
{
echo("Player entered Zone");
%trigger.PlayersInTrigger = %trigger.PlayersInTrigger++;
echo("There are " @ %PlayersInTrigger @ "players in the trigger area");
}
}

//------------------------

I thought by declaring it in the datablock it could be accessed by all functions. I forgot that onEnterTrigger( and onLeaveTrigger( are separate functions so the local variable gets lost...

I'm going to try the array. I understand the concept of an array, I've just never been able to use one properly (kinda like golf. I understand the concept, but I still can't putt, chip, or even drive.)

I'll let you know how it works.

Thanks again!
Tony
#9
04/09/2009 (5:41 am)
BTW: I'm basing this on the BF2 Style CTF code by mb area_trigger.cs

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=1160...


UPDATE:

@Orion, its not

echo("There are " @ %playerInTrigger @ " players in the trigger area!");


its this

echo("There are " @ %trigger.playerInTrigger @ " players in the trigger area!"); //--forgot the trigger precursor!


Thanks again! The echo works. Now I just have to get my trigger code working.
Tony
#10
04/09/2009 (6:37 am)
> couldn't you store the number of players as a property of the trigger object?
great point!