Using a Trigger to Pop-Up a message
by Infinitum3D · in Torque Game Engine · 11/29/2006 (7:18 pm) · 8 replies
I'm trying to use a trigger near a street sign that will pop-up a message showing what the street sign says. I don't want to have to make custom street sign .DTS for every street name in a town (i.e Main St., 1st Ave., etc.)
here's my code...
datablock TriggerData(signYorkville)
{
tickPeriodMS = 1000;
}
//-- Should I use the first, second or third function? Or is there a better way?
//-- First Function
//-- Second Function
//-- Third Function
Are any of these acceptible/correct? What is the best way?
Thanks!
Tony
here's my code...
datablock TriggerData(signYorkville)
{
tickPeriodMS = 1000;
}
//-- Should I use the first, second or third function? Or is there a better way?
//-- First Function
Quote:
function signYorkvilleTrigger::onEnterTrigger(%this,%trigger,%obj)
{
echo("This road leads to Yorkville.");
//print message to client
%message="Yorkville";
%time=1;
%lines=1;
centerPrint(%obj.client,%message,%time,%lines);
Parent::onTriggerEnter
}
//-- Second Function
Quote:
function signYorkvilleTrigger::onEnterTrigger(%this,%trigger,%obj)
{
echo("This road leads to Yorkville.");
//-- or print message to client
commandToClient(%client,Yorkville);
Parent::onTriggerEnter
}
//-- Third Function
Quote:
function signYorkvilleTrigger::onEnterTrigger(%this,%trigger,%obj)
{
echo("This road leads to Yorkville.");
//print message to client
centerPrint(%obj.client, "This road leads to Yorkville.", 1, 1);
Parent::onTriggerEnter
}
Are any of these acceptible/correct? What is the best way?
Thanks!
Tony
#2
Thanks mb!
Tony
11/30/2006 (5:23 am)
I thought the third seems most likely also, but I'm just copy/pasting code. I'm not a programmer. And I thought I'd ask before trying it in my game. I've crashed Torque so many times with trial_and_error coding...Thanks mb!
Tony
#3
11/30/2006 (6:22 am)
The second one is the right way if you're in a multiplayer environment. Triggers are fired server-side, so if you have a (network) connected client, you need to send him the message that shows the actual sign. On the client-side you need then a function that receives this event and pops up a dialog or whatever.
#5
I've created a new file called signTriggers.cs with this code:
I've saved it to the Sever\Scripts folder AND the Client\Scripts folder. I've placed this line in the game.cs file of the Server\Scripts folder AND in the init.cs file in the client folder.
For some reason, signTriggers.cs does not get compiled. It NEVER gets a .dso and the datablock never comes up in the Mission Editor.
What am I missing???
Thanks!
Tony
11/30/2006 (4:58 pm)
Well, I'm still stuck.I've created a new file called signTriggers.cs with this code:
Quote:
datablock TriggerData(signYorkvilleTrigger)
{
tickPeriodMS = 1000;
}
function signYorkvilleTrigger::onEnterTrigger(%this,%trigger,%obj)
{
echo("This road leads to Yorkville.");
//-- and print message to client
commandToClient(%client, This road leads to Yorkville);
Parent::onTriggerEnter
}
function signYorkvilleTrigger::onLeaveTrigger(%this,%trigger,%obj)
{
Parent::onLeaveTrigger(%this,%trigger,%obj);
}
function signYorkvilleTrigger::onTickTrigger(%this,%trigger)
{
Parent::onTickTrigger(%this,%trigger);
}
I've saved it to the Sever\Scripts folder AND the Client\Scripts folder. I've placed this line in the game.cs file of the Server\Scripts folder AND in the init.cs file in the client folder.
Quote:
exec("./signTriggers.cs");
For some reason, signTriggers.cs does not get compiled. It NEVER gets a .dso and the datablock never comes up in the Mission Editor.
What am I missing???
Thanks!
Tony
#6
11/30/2006 (5:15 pm)
You should check your console log. You are missing a semicolon.datablock TriggerData(signYorkvilleTrigger)
{
tickPeriodMS = 1000;
}Should be:datablock TriggerData(signYorkvilleTrigger)
{
tickPeriodMS = 1000;
}; //Missing semicolon hereThat's all I see. Also, you can use [ code] [/code ] (without spaces) to make your code more readable.
#7
@Martin:
So my exec command should be in the server, right?
but my signTriggers.cs file needs to be in the client?
I'm sorry. I know I must be frustrating, but I'm a total newbie. Could someone explain where to place the signTriggers.cs file and where to place the exec code to load it?
Thanks!
Tony
ps. I hope it's just the missing semi-colon... :)
11/30/2006 (5:33 pm)
@Mike: Thanks!@Martin:
Quote:Triggers are fired server-side
So my exec command should be in the server, right?
Quote:
On the client-side you need then a function that receives this event and pops up a dialog or whatever
but my signTriggers.cs file needs to be in the client?
I'm sorry. I know I must be frustrating, but I'm a total newbie. Could someone explain where to place the signTriggers.cs file and where to place the exec code to load it?
Thanks!
Tony
ps. I hope it's just the missing semi-colon... :)
#8
I load it from my game.cs file using the exec line.
This works for me.
Tony
12/05/2006 (5:27 pm)
OK, I've placed this code in my signTriggers.cs file and placed the .cs file in my server\scripts directory. I load it from my game.cs file using the exec line.
Quote:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// DefaultTrigger is used by the mission editor. This is also an example
// of trigger methods and callbacks.
datablock TriggerData(YorkvilleTrigger)
{
// The period is value is used to control how often the console
// onTriggerTick callback is called while there are any objects
// in the trigger. The default value is 100 MS.
tickPeriodMS = 5000;
};
datablock TriggerData(TowneTrigger)
{
// The period is value is used to control how often the console
// onTriggerTick callback is called while there are any objects
// in the trigger. The default value is 100 MS.
tickPeriodMS = 5000;
};
datablock TriggerData(MountPleasantTrigger)
{
// The period is value is used to control how often the console
// onTriggerTick callback is called while there are any objects
// in the trigger. The default value is 100 MS.
tickPeriodMS = 5000;
};
//-----------------------------------------------------------------------------
function YorkvilleTrigger::onEnterTrigger(%this,%trigger,%obj)
{
// This method is called whenever an object enters the %trigger
// area, the object is passed as %obj. The default onEnterTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on
// every object (whatever it's type) in the same group as the trigger.
echo("This road leads to Yorkville.");
//print message to client
centerPrint(%obj.client, "This road leads to Yorkville.", 1, 1);
Parent::onEnterTrigger(%this,%trigger,%obj);
}
function YorkvilleTrigger::onLeaveTrigger(%this,%trigger,%obj)
{
// This method is called whenever an object leaves the %trigger
// area, the object is passed as %obj. The default onLeaveTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,0) method on
// every object (whatever it's type) in the same group as the trigger.
Parent::onLeaveTrigger(%this,%trigger,%obj);
}
function YorkvilleTrigger::onTickTrigger(%this,%trigger)
{
// This method is called every tickPerioMS, as long as any
// objects intersect the trigger. The default onTriggerTick
// method (in the C++ code) invokes the ::onTriggerTick(%trigger) method on
// every object (whatever it's type) in the same group as the trigger.
echo("This road leads to Yorkville.");
//print message to client
centerPrint(%obj.client, "This road leads to Yorkville.", 1, 1);
// You can iterate through the objects in the list by using these
// methods:
// %this.getNumObjects();
// %this.getObject(n);
Parent::onTickTrigger(%this,%trigger);
}
//-----------------------------------------------------------------------------
function TowneTrigger::onEnterTrigger(%this,%trigger,%obj)
{
// This method is called whenever an object enters the %trigger
// area, the object is passed as %obj. The default onEnterTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on
// every object (whatever it's type) in the same group as the trigger.
echo("This road leads to Towne.");
//print message to client
centerPrint(%obj.client, "This road leads to Towne.", 1, 1);
Parent::onEnterTrigger(%this,%trigger,%obj);
}
function TowneTrigger::onLeaveTrigger(%this,%trigger,%obj)
{
// This method is called whenever an object leaves the %trigger
// area, the object is passed as %obj. The default onLeaveTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,0) method on
// every object (whatever it's type) in the same group as the trigger.
Parent::onLeaveTrigger(%this,%trigger,%obj);
}
function TowneTrigger::onTickTrigger(%this,%trigger)
{
// This method is called every tickPerioMS, as long as any
// objects intersect the trigger. The default onTriggerTick
// method (in the C++ code) invokes the ::onTriggerTick(%trigger) method on
// every object (whatever it's type) in the same group as the trigger.
echo("This road leads to Towne.");
//print message to client
centerPrint(%obj.client, "This road leads to Towne.", 1, 1);
// You can iterate through the objects in the list by using these
// methods:
// %this.getNumObjects();
// %this.getObject(n);
Parent::onTickTrigger(%this,%trigger);
}
//-----------------------------------------------------------------------------
function MountPleasantTrigger::onEnterTrigger(%this,%trigger,%obj)
{
// This method is called whenever an object enters the %trigger
// area, the object is passed as %obj. The default onEnterTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on
// every object (whatever it's type) in the same group as the trigger.
echo("This road leads to Mount Pleasant.");
//print message to client
centerPrint(%obj.client, "This road leads to Mount Pleasant.", 1, 1);
Parent::onEnterTrigger(%this,%trigger,%obj);
}
function MountPleasantTrigger::onLeaveTrigger(%this,%trigger,%obj)
{
// This method is called whenever an object leaves the %trigger
// area, the object is passed as %obj. The default onLeaveTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,0) method on
// every object (whatever it's type) in the same group as the trigger.
Parent::onLeaveTrigger(%this,%trigger,%obj);
}
function MountPleasantTrigger::onTickTrigger(%this,%trigger)
{
// This method is called every tickPerioMS, as long as any
// objects intersect the trigger. The default onTriggerTick
// method (in the C++ code) invokes the ::onTriggerTick(%trigger) method on
// every object (whatever it's type) in the same group as the trigger.
echo("This road leads to Mount Pleasant.");
//print message to client
centerPrint(%obj.client, "This road leads to Mount Pleasant.", 1, 1);
// You can iterate through the objects in the list by using these
// methods:
// %this.getNumObjects();
// %this.getObject(n);
Parent::onTickTrigger(%this,%trigger);
}
This works for me.
Tony
Torque 3D Owner mb
did I win? :D
You could push/pop a custom gui on enter & leave.