Game Development Community

Scripting

by Lisa Kelly · in Technical Issues · 04/08/2008 (1:47 pm) · 3 replies

Hi all,

I need to figure out how to set a global variable and compare it to a counter when the character enters a trigger. I am able to declare the global variable easily but am confused when needing to compare it to a counter.

About the author

Recent Threads

  • Databases
  • Odbc And Tge

  • #1
    04/08/2008 (8:00 pm)
    What do you mean by a "counter" exactly?
    #2
    04/08/2008 (9:24 pm)
    What kind of comparison are you trying to make? If you need to determine if your variable is greater than, less than, or equal to you counter then this might be of help: Script Operators, look toward the end of that page at the evaluation operators. If a more esoteric comparison then we may need more information.
    #3
    04/09/2008 (8:05 am)
    If you have a trigger area set up around the CashRegister static object, add the "dynamic field" (exists in script only, and only on the server) counter to the definition of the trigger.

    specify this global variable somewhere in the trigger script file:
    $CashRegisterTriggerAlarmValue = 1000000;

    In the script callback function that is executed when a player enters the trigger:
    %trigger.counter++;
    if (%trigger.counter == $CashRegisterTriggerAlarmValue) {
    // do your response one time only
    // and perhaps
    %trigger.counter = 0; // reset counter for the next lucky customer
    }

    In many cases, it would seem to make more sense to put the $CashRegisterTriggerAlarmValue into the trigger object instance also, instead of having one global variable, so you would be able to have different triggers each with their own alarm value.

    Or, if you wanted all your cash register triggers to have the same value, put it into the CashRegisterTrigger datablock definition. Then it would be something like:

    datablock TriggerData(CashRegisterTrigger)
    {
       // 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;
    
       // this is the number of the lucky customer who wins the prize
       alarmValue = 1000000;
    };
    
    function CashRegisterTrigger::onEnterTrigger(%this,%trigger,%obj)
    {
       %trigger.counter++;
       if (%trigger.counter == %this.alarmValue) {
          // do your response one time only
             // not yet implemented
    
          // and perhaps
          %trigger.counter = 0; // reset counter for the next lucky customer
       }
    }

    %trigger is passed in as the ID of the object instance of the trigger. %this is the object instance of the CashRegisterTrigger datablock.

    Sometimes you would want to check in the onEnterTrigger to make sure the %obj (object entering the trigger) is a player. You wouldn't want to give a prize to an AI robber, or the checker.