Game Development Community

Trouble with Triggers

by Adam McDonald · in Torque Game Builder · 01/21/2012 (11:23 pm) · 4 replies

I have been having a bit of trouble getting a trigger to work. Here is a breakdown of what I'd like it to do:

- Player touches the trigger.
- Player changes colour.

I have a couple of functions that already handle the colour change, which currently works by pressing number keys (just for debugging/testing purposes). I also had the colour change working for when the player collided with an object, but I want the player to go THROUGH the object and in another thread it was recommended that I use triggers instead of collision. So far, I haven't been able to get the trigger to work.

Here is my script for a trigger that should be making the player turn red:

function redGate::onEnter(%this, %object)
{
		%object.red();
}

Here is what I have in the player's code that is related to this:

function playerClass::onLevelLoaded(%this, %scenegraph)
{
	$player = %this; 
	$colour = 0;

      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "a", "playerJump();", "playerFall();");
      moveMap.bindCmd(keyboard, "1", "red();", "");
      moveMap.bindCmd(keyboard, "2", "orange();", "");
      moveMap.bindCmd(keyboard, "3", "yellow();", "");
      moveMap.bindCmd(keyboard, "4", "green();", "");
      moveMap.bindCmd(keyboard, "5", "blue();", "");
      moveMap.bindCmd(keyboard, "6", "violet();", "");
      moveMap.bindCmd(keyboard, "0", "grey();", "");
      
      %this.enableUpdateCallback();
}
function red()
{
	$colour = 1;
}
function orange()
{
	$colour = 2;
}
function yellow()
{
	$colour = 3;
}
function green()
{
	$colour = 4;
}
function blue()
{
	$colour = 5;
}
function violet()
{
	$colour = 6;
}
function grey()
{
	$colour = 0;
}
function playerClass::onUpdate(%this)
{
   %this.updateHorizontal();
   %this.updateVertical();
   %this.setCurrentAnimation();
}
function playerClass::setCurrentAnimation(%this)
{	

	if ($colour == 1)
	{
		%this.playAnimation(player1);
	}

	if ($colour == 2)
	{
		%this.playAnimation(player2);
	}

	if ($colour == 3)
	{
		%this.playAnimation(player3);
	}

	if ($colour == 4)
	{
		%this.playAnimation(player4);
	}

	if ($colour == 5)
	{
		%this.playAnimation(player5);
	}

	if ($colour == 6)
	{
		%this.playAnimation(player6);
	}
	if ($colour == 0)
	{
		%this.playAnimation(player0);
	}
}

Each animation, as you may have guessed, is the player's sprite but in a different colour. Is there something in my script that stands out as something that may be causing this issue? I don't think I am doing anything wrong in the editor, but if the scripting looks solid then I can start troubleshooting there.

#1
01/22/2012 (6:25 am)
Put an echo in your onEnter() to see if it's getting called at all. If it's not, check the collision settings on your trigger. It has to collide like a regular object for the trigger callback to work.
#2
01/22/2012 (12:10 pm)
I wasn't sure if I needed to actually collide with it or not, so I didn't have it in a group that my player could collide with. I changed that and it works now! Thanks!

The only problem now is that the player seems to speed up when jumping up through it, slow down when falling through it, and stop when hitting it from the side. Do you have any idea how to fix that? Currently the only box checked for the trigger's collision is "callback". There is some collision stuff coded directly into the player if that makes a difference (based on the platformer tutorial).
#3
01/22/2012 (10:01 pm)
If there's an onCollision callback for the player you'll need to check in there if you're hitting a trigger, and not call the usual collision scripts if that's the case.
#4
01/23/2012 (9:51 pm)
Yeah, my design for the game is actually very heavily reliant on different types of collision so I will likely need to revamp all of the collision to be less universal and more specific to what type of object the player is colliding with. Good to know that's probably what's causing this and not just general weirdness from triggers. Thanks for the help.