Game Development Community

Strange trigger issue

by Lee Latham · in Torque Game Engine · 01/13/2007 (12:44 am) · 6 replies

I've got a strange trigger issue that I'd be grateful if someone could explain what I am clearly not understanding. I've set up a simple trigger to do a jump if an AI or player runs over it. The problem is that if I use the servercommand I've set up, the jump happens to the _player_, and not the AI. Meaning, the AI runs over the trigger, and then I am thrust upwards! here is the relevant script:
function jumptrigger::onEnterTrigger( %this, %trigger, %obj )
{

commandToServer('jumpboost', %obj);
//%centroid = %obj.getWorldBoxCenter(); 
//%obj.applyimpulse(%centroid, "0 0 3333");

	Parent::onEnterTrigger( %this, %trigger, %obj );
}

This is the code which acts as described. However, if I comment out the commandToServer, and uncomment the two lines below it, it works just fine. The thing is, the "jumpboost" servercommand uses very similar code as you see commented out, there. Here is the code for that from my commands.cs:
function serverCmdJumpBoost(%client)
{
      %vehicle = %client.getControlObject(); //get the vehicle id

      %centroid = %vehicle.getWorldBoxCenter(); //get the centroid of the vehicle so we can apply impulse to it.
      %vehicle.applyImpulse(%centroid,"0 0 3333"); //kablamo!
}

I'm kinda feeling like I'm not understanding something in the client/server relationship, but it would be swell if someone could explain my foolishness, as I don't think my hack will work for some of the other things I would like to do.

Edit: Have I answered my own question? In the server command I get the ControlObject, where in the trigger I do not. The reason I did this was to let my AI vehicles use the same "hopping" that I let my players use. But the trigger behavoir I describe happens even with players not mounted on vehicles? I figure the %obj is the same...? I'm confused!

Lee

#1
01/13/2007 (1:08 am)
Have you tried using the physic zone ?

Aun.
#2
01/13/2007 (1:54 am)
function jumptrigger::onEnterTrigger( %this, %trigger, %obj )
{
   commandToServer('jumpboost', %obj);
   Parent::onEnterTrigger( %this, %trigger, %obj );
}

function serverCmdJumpBoost[b]([i]%client[/i], %obj)[/b]
{
      %vehicle = %obj.getControlObject(); //get the vehicle id

      %centroid = %vehicle.getWorldBoxCenter(); //get the centroid of the vehicle so we can apply impulse to it.
      %vehicle.applyImpulse(%centroid,"0 0 3333"); //kablamo!
}

The %client in the server cmd represents the actual client and not the object that entered the trigger (%obj).

This is how server commands work, the first variable in the function is the client.

So when you pass on the object that entered the trigger in commandToServer('jumpboost', %obj); it will actually be the second variable in the commandToServer.
#3
01/13/2007 (3:34 am)
I think the reason your player is the one whose jumping instead of the ai is because %client.getControlObject() is going to retrieve what the client is controlling which is the player, not the AI.

Another point is that the onEnterTrigger command is called by the server, not the client and so there's no need for the commandToServer.

What I would do is this:

function jumptrigger::onEnterTrigger( %this, %trigger, %obj )
{
%centroid = %obj.getWorldBoxCenter(); 
%obj.applyimpulse(%centroid, "0 0 3333");
}

As soon as an object enters the trigger, you apply an impulse onto the object.

--Amr
#4
01/13/2007 (3:50 pm)
Tim: I think I tried what you suggested, but after echoing out the variables in the servercommand, I got nothing for the value of %obj. I just changed the parameters in the server command function, adding %obj. Is that correct? I must confess to being a bit confused sometimes (I'm no coder) about how Torque passes these things around. I interpreted what you said to mean that Torque always sends the client as the first parameter of a servercommand, no matter what one intends to send when one calls the function with some other parameter...? I would like to understand this...

Amr: I see, and in fact I've taken your advice to heart and solved the problem with my more complicated triggers as well. I had thought that it would be nice/essential to unify some of the functions by using the server commands, since, for example, I have a "forward boost" which needs to first find the eye vector and then boost along it. But no matter. I simply ported the code into the trigger context and sure enough it works fine. But it simply hadn't occurred to me that the server was calling the trigger command! Duh!

Thanks all!
#5
01/13/2007 (3:59 pm)
Glad you got it working Lee!

I'll try and answer your question about the parameters.

When using commandToServer, you specify the command you want to run and also any parameters you want to send. If I run this command:

commandToServer('someFunc',%param1,%param2,%param3);

the corresponding function that would respond to that would be:

serverCmdsomeFunc(%client,%param1,%param2,%param3)
{
..
}

The first parameter in such function is always going to point to the client that called the function. Your other parameters come after that.

I think it is a good idea to unify your functions, as it will make the code cleaner and easier to modify. You could just use a regular function, it doesn't have to be a server command.

--Amr
#6
01/13/2007 (6:12 pm)
Amr, thanks for that! Indeed, I would not have guessed that's how it works.

Basically, what I'm doing is simulating a vehicle "boost" and "jump" servercommand I use for my players in order to get the AI's in vehicles to navigate my course for a demo. So it does seem like I'll need to have them as separate functions, even if they use (more or less) the same code.

Unless, of course, I'm wrong about that.

Thanks!
Lee