Creating a path
by CodingChris · in Torque Game Engine · 10/04/2007 (10:59 am) · 25 replies
Hi,
I just want to create a path from script in starter.racing. My plan is to get the position of the current checkpoint, of all items inside this checkpoint and of the next checkpoint. Then I want to create a path from this values. This is my code:
I just want to create a path from script in starter.racing. My plan is to get the position of the current checkpoint, of all items inside this checkpoint and of the next checkpoint. Then I want to create a path from this values. This is my code:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
datablock TriggerData(CheckPointTrigger)
{
// 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 = 100;
};
//-----------------------------------------------------------------------------
function CheckPointTrigger::onEnterTrigger(%this,%trigger,%obj)
{
Parent::onEnterTrigger(%this,%trigger,%obj);
if(%obj.client.nextCheck == %trigger.checkpoint)
{
centerPrint(%obj.client, "Checkpoint!", 1, 1);
//Bot
if(%obj.isAIControlled()) {
%count2 = 1;
new Path (%trigger.nextCheck+"Path") {
new Marker("mycheck") {
position = %trigger.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = %count2
};
%groupName = %trigger.nextCheck;
%group = nameToID(%groupName);
if (%group != -1) {
%count = %group.getCount();
if (%count != 0) {
while (%count != 0)
{
%spawn = %group.getObject(%index);
new Marker (%spawn + "1")
{
position = %spawn.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = %count2
};
%count = %count - 1;
%count2 = %count2 + 1;
}
}
else
error("No spawn points found in " @ %groupName);
}
else
error("Missing spawn points group " @ %groupName);
new Marker("mycheck2") {
position = %trigger.nextCheck.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = %count2
};
};
}
//Bot
$pos = getWords( ServerConnection.getControlObject().getTransform(),0,2);
if(%trigger.isLast)
{
// Player has completed a lap.
%obj.client.lap++;
centerPrint(%obj.client, "Lap complete!", 1, 1);
if(%obj.client.lap >= $Game::Laps)
{
centerPrint(%obj.client, "Complete!", 1, 1);
// Increase his score by 1.
%obj.client.incScore(1);
// End the game
cycleGame();
}
else {
%obj.client.nextCheck = 1;
commandToClient(%obj.client, 'IncreaseLapCounter');
}
}
else {
// Continue to the next one.
clientCmdCheck();
%obj.client.nextCheck++;
}
}
else
centerPrint(%obj.client, "Wrong Checkpoint!", 1, 1);
}What's wrong here?About the author
#22
I think I might of found something... You query the group twice and the second time you use the wrong name.
Try this:
10/06/2007 (7:41 am)
Sorry for the double post.. when I hit refresh I didn't realize it had reposted.I think I might of found something... You query the group twice and the second time you use the wrong name.
Try this:
function CheckPointTrigger::onEnterTrigger(%this,%trigger,%obj)
{
Parent::onEnterTrigger(%this,%trigger,%obj);
if(%obj.client.nextCheck == %trigger.checkpoint)
{
centerPrint(%obj.client, "Checkpoint!", 1, 1);
//Bot
// Check AI
echo(%this);
echo(%trigger);
if (%obj.client.isAIControlled())
{
echo("Yes");
$count2 = 1;
$groupName = "MissionGroup/" + %trigger.nextCheck;
%group = nameToID($groupName);
%path = new Path ("roadtotrigger");
MissionCleanup.add(%path);
//create path
%marker = new Marker("mycheck")
{
canSaveDynamicFields = "1";
position = %trigger.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = $count2;
type = "Normal";
msToNext = "1000";
smoothingType = "Linear";
speed = "60";
};
%path.add(%marker);
MissionCleanup.add(%marker);
//created first marker = current checkpoint
//set group name
[b]//Excess/duplicate/wrong code removed here[/b]
//check if it is valid
if (%group != -1)
{
//get items
%count = %group.getCount();
if (%count != 0)
{
while (%count != 0)
{
//get selected object
%spawn = %group.getObject(%index);
//create next marker = next object inside of current checkpoint
%marker = new Marker (%spawn + "1")
{
canSaveDynamicFields = "1";
position = %spawn.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = $count2;
type = "Normal";
msToNext = "1000";
smoothingType = "Linear";
};
%path.add(%marker);
MissionCleanup.add(%marker);
//created marker
//numbers
%count = %count - 1;
$count2 = $count2 + 1;
}
// do it again
}
else
error("No items in " @ %groupName);
}
else
// checkpoint is not valid
error("Missing spawn points group " @ %groupName);
//create last marker = next checkpoint
%marker = new Marker("mycheck2")
{
canSaveDynamicFields = "1";
position = %trigger.next.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = $count2;
type = "Normal";
msToNext = "1000";
smoothingType = "Linear";
};
//close marker
%path.add(%marker);
MissionCleanup.add(%marker);
MissionCleanup.add(%path);
}
//close if
%obj.client.car.followPath(%trigger.getTransform(), -1);
//Bot
$pos = getWords( ServerConnection.getControlObject().getTransform(),0,2);
if(%trigger.isLast)
{
// Player has completed a lap.
%obj.client.lap++;
centerPrint(%obj.client, "Lap complete!", 1, 1);
if(%obj.client.lap >= $Game::Laps)
{
centerPrint(%obj.client, "Complete!", 1, 1);
// Increase his score by 1.
%obj.client.incScore(1);
// End the game
cycleGame();
}
else
{
%obj.client.nextCheck = 1;
commandToClient(%obj.client, 'IncreaseLapCounter');
}
}
else
{
// Continue to the next one.
clientCmdCheck();
%obj.client.nextCheck++;
}
}
else
centerPrint(%obj.client, "Wrong Checkpoint!", 1, 1);
}
#23
10/06/2007 (9:04 am)
Now I got:52 1695 Yes Missing spawn points group followPath() AIWheeledVehicle::followPath failed - Bad Path!
#24
If this doesnt work let me suggest that you try renaming your groups from something other than just a number. Try 'TriggerGroup1', 'TriggerGroup2', etc. or anything else. I think it might be getting confused and mistaking the number for an object id.
I cleaned up the code a little bit and modified the debug output a bit.
You were abusing the groupName bariable using both a global reference ($) and a local reference (%) I dont know if you want your variables to be locals or not so I left the ones that were global as such until you change them. Normally you want to use local variables unless you are going to reference them outside of your function (without being able to pass them down to another function).
I dont think this version really fixes any bugs with the code. The underlying problem will probably still exist but it will provide better debug output.
10/06/2007 (10:16 am)
Okay, this is the last one before I go to bed... Its REALLY late for me (but I work night shift so its actually 10am).If this doesnt work let me suggest that you try renaming your groups from something other than just a number. Try 'TriggerGroup1', 'TriggerGroup2', etc. or anything else. I think it might be getting confused and mistaking the number for an object id.
I cleaned up the code a little bit and modified the debug output a bit.
You were abusing the groupName bariable using both a global reference ($) and a local reference (%) I dont know if you want your variables to be locals or not so I left the ones that were global as such until you change them. Normally you want to use local variables unless you are going to reference them outside of your function (without being able to pass them down to another function).
I dont think this version really fixes any bugs with the code. The underlying problem will probably still exist but it will provide better debug output.
function CheckPointTrigger::onEnterTrigger(%this,%trigger,%obj)
{
Parent::onEnterTrigger(%this,%trigger,%obj);
if(%obj.client.nextCheck == %trigger.checkpoint)
{
centerPrint(%obj.client, "Checkpoint!", 1, 1);
//Bot
// Check AI
echo("CheckPointTrigger::onEnterTrigger("@%this@", "@%trigger@", "@%obj@")");
if (%obj.client.isAIControlled())
{
echo("Encroaching object is AI controlled");
$count2 = 1;
$groupName = "MissionGroup/" + %trigger.nextCheck;
%group = nameToID($groupName);
// report if group is missing
if (%group != -1) {
error("Missing spawn points group " @ $groupName);
echo("Objects under MissionGroup");
for (%ii = 0; %ii < MissionGroup.getCount(); %ii++) {
%oobj = MissionGroup.getObject(%ii);
echo("MissionGroup/"@%oobj.getName()@"("@%oobj.getId()@")");
echo(" -- test -- "@nameToId("MissionGroup/"@%oobj.getName())):
}
}
%path = new Path ("roadtotrigger");
MissionCleanup.add(%path);
//create path
%marker = new Marker("mycheck")
{
canSaveDynamicFields = "1";
position = %trigger.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = $count2;
type = "Normal";
msToNext = "1000";
smoothingType = "Linear";
speed = "60";
};
%path.add(%marker);
MissionCleanup.add(%marker);
//created first marker = current checkpoint
//set group name
//Excess/duplicate/wrong code removed here
//check if it is valid
if (%group != -1)
{
//get items
%count = %group.getCount();
if (%count != 0)
{
while (%count != 0)
{
//get selected object
%spawn = %group.getObject(%index);
//create next marker = next object inside of current checkpoint
%marker = new Marker (%spawn + "1")
{
canSaveDynamicFields = "1";
position = %spawn.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = $count2;
type = "Normal";
msToNext = "1000";
smoothingType = "Linear";
};
%path.add(%marker);
//created marker
//numbers
%count = %count - 1;
$count2 = $count2 + 1;
}
// do it again
}
else
error("No items in " @ $groupName);
}
//create last marker = next checkpoint
%marker = new Marker("mycheck2")
{
canSaveDynamicFields = "1";
position = %trigger.next.getTransform();
rotation = "1 0 0 0";
scale = "1 1 1";
seqnum = $count2;
type = "Normal";
msToNext = "1000";
smoothingType = "Linear";
};
//close marker
%path.add(%marker);
}
//close if
%obj.client.car.followPath(%trigger.getTransform(), -1);
//Bot
$pos = getWords( ServerConnection.getControlObject().getTransform(),0,2);
if(%trigger.isLast)
{
// Player has completed a lap.
%obj.client.lap++;
centerPrint(%obj.client, "Lap complete!", 1, 1);
if(%obj.client.lap >= $Game::Laps)
{
centerPrint(%obj.client, "Complete!", 1, 1);
// Increase his score by 1.
%obj.client.incScore(1);
// End the game
cycleGame();
}
else
{
%obj.client.nextCheck = 1;
commandToClient(%obj.client, 'IncreaseLapCounter');
}
}
else
{
// Continue to the next one.
clientCmdCheck();
%obj.client.nextCheck++;
}
}
else
centerPrint(%obj.client, "Wrong Checkpoint!", 1, 1);
}
#25
10/09/2007 (8:28 am)
Sorry Brian, but I can't test it at the moment. I'm ill. The doctor says maybe I'll be able to test it next week. I'm very sorry
Torque 3D Owner CodingChris