Game Development Community

Trigger help needed

by Jesse P · in Torque Game Engine · 10/20/2007 (4:52 pm) · 11 replies

Just added a trigger to my mission, it activates each time the user steps into it, how do I make it so it only activates once and then never again? Thanks

#1
10/20/2007 (4:59 pm)
When it activates have it disable itself.. something this should do ya:
%myTrigger.setHidden(true);
#2
10/20/2007 (5:47 pm)
What would I change %myTrigger to?
#3
10/20/2007 (5:51 pm)
Also when I played around with it the console says "unknown command setHidden"
#4
10/20/2007 (6:55 pm)
What I am doing is having a triggered sound to play as soon as the player starts the mission, here is what I put in the trigger's onEnterTrigger:


function OpeningAudioTrigger::onEnterTrigger(%this,%trigger,%obj)
{

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

alxPlay(Mission1_entry);

}


then after that I need to find a way to hide/disable the trigger but the setHidden doesn't seem to be working when I place it inside the onEnter or onLeave, I'm not much of a coder so forgive me.

I would have used an audio emitter but it kept playing the sound while the mission was still loading
#5
10/20/2007 (11:00 pm)
Okay, I checked into it and Triggers inherit from GameBase and not ShapeBase (that makes sense) so they have no 'setHidden' method... anyhow this should get you going.

I threw in some extra functionality! This example counts the number of times it has been triggered. You can use this to do different things each time it is hit or just use it as-is for one-shots.

function OpeningAudioTrigger::onEnterTrigger(%this,%trigger,%obj)
{
    Parent::onEnterTrigger(%this,%trigger,%obj);

    // create (and initialize) field if it doesnt exist
    if (%trigger.timesEntered $= "")
       %trigger.timesEntered = 0;

    %trigger.timesEntered++;

    if (%trigger.timesEntered == 1) {
        alxPlay(Mission1_entry);
    }

    if (%trigger.timesEntered == 2) {
        // you can do something different.
    }
}
#6
10/21/2007 (10:07 am)
It played once and then not again which is good, but then when I quit the program and then launched it again it doesn't play at all which makes me think that maybe count is being stored somewhere. Shouldn't it be resetting as soon as I quit the program though? But then when I even restarted it doesn't play again either...Thanks for helping me out on this. What do you think could cause that?
#7
10/21/2007 (11:02 am)
It has something to do with a problem in the initialization if statement:

if (%trigger.timesEntered $= "")
%trigger.timesEntered = 0;

I'm not good enough at coding yet to figure out exactly what though, but I got it to work by tweaking the code to the following, but this is just for testing I set it to if it doesn't equal 1 then play it then set it to 1, but for some reason that initialization line is not setting it to zero if it hasn't been entered once. I don't quite know yet what the $ or % mean...I'm learning...what are your thoughts?

function OpeningAudioTrigger::onEnterTrigger(%this,%trigger,%obj)
{

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

    // create (and initialize) field if it doesnt exist
//(these two lines aren't needed for this particular test but I'd like to get it to work)
    if (%trigger.timesEntered $= "")
       %trigger.timesEntered = 0;
      

    if (%trigger.timesEntered != 1) {
        alxPlay(Mission1_entry);
        %trigger.timesEntered = 1;
       
    }

    if (%trigger.timesEntered = 1) {
        // you can do something different.
    }


}
#8
10/21/2007 (3:49 pm)
I have no idea why it wouldn't work a second time.

timeEntered is entirely script created and certainly not stored anywhere.

Anyways, maybe give this a shot:
function OpeningAudioTrigger::onAdd(%trigger)
{
    %trigger.timesEntered = 0;
}

function OpeningAudioTrigger::onEnterTrigger(%this,%trigger,%obj)
{
    Parent::onEnterTrigger(%this,%trigger,%obj);

    %trigger.timesEntered++;

    if (%trigger.timesEntered == 1) {
        alxPlay(Mission1_entry);
    }

    if (%trigger.timesEntered == 2) {
        // you can do something different.
    }
}
#9
10/22/2007 (8:18 am)
I am pretty sure I figured out what was happening. It was setting the variable to 1 and then it logs it as a Dynamic Variable and when I made changes in the mission and then saved it it was keeping it at that value. So I just need to remember to go into the World inspector and delete that variable in the trigger before saving it when I make changes. Yet another thing I learned about Torque...
Thanks so much for your help, the first script is great, havn't tested the last one you posted though but when I delete the variable before saving the mission the other script seems to work fine
#10
10/22/2007 (8:24 am)
Wow, I didn't know about that one either.

It makes sense though....

I'll have to keep this in mind.. it could have odd side effects.


Taking this into consideration the second script would be better as it will always initialize the counter to zero regardless if it exists or not.
#11
10/22/2007 (8:33 am)
Thanks