RE: Using Global.hps to use a script in every level
(05-15-2013, 03:37 AM)JustAnotherPlayer Wrote: Shit, no global timers for me.
Global timers can still be done but it is a huge hack.
Spoiler below!
You will need to paste the following in every map (or stick it in a script file and #include it in with a pre-processor). Call the OnEnter bit in the OnEnter bit of the map (i can't remember if timers resume or not, so this might need to be changed to onstart - you will need to test this).
//Call in OnEnter
void OnEnter_GLOBALTIMER()
{
AddTimer("_UpdateGlobalTimers",1.0f / 60.0f,"_UpdateGlobalTimers");
}
//Adds a global timer
void AddGlobalTimer(string &in asTimer, float afTime, string &in asCallbackName)
{
//Generate a unique timer ID for this timer
AddGlobalVarInt("GLOBALTIMER_COUNT",1);
int timerID = GetGlobalVarInt("GLOBALTIMER_COUNT");
//Setup Reverse lookup from name -> ID
SetGlobalVatInt("GLOBALTIMER_ID_" + asTimer,timerID);
//Setup properties of the timer associated with ID
SetGlobalVarString("GLOBALTIMER_NAME_" + timerID,asTimer);
SetGlobalVarString("GLOBALTIMER_CALLBACK_" + timerID,asCallbackName);
SetGlobalVarFloat("GLOBALTIMER_TIME_" + timerID,afTime);
}
//Removes a global timer
void RemoveGlobalTimer(string &in asTimer)
{
//Get id
int timerID = GetGlobalVatInt("GLOBALTIMER_ID_" + asTimer);
//Mark as removed
SetGlobalVarString("GLOBALTIMER_CALLBACK" + timerID,"");
SetGlobalVarFloat("GLOBALTIMER_TIME" + timerID,0);
SetGlobalVarString("GLOBALTIMER_NAME_" + timerID,"");
SetGlobalVatInt("GLOBALTIMER_ID_" + asTimer,0);
}
//Get time left on a timer (returns <=0 if expired or non-existant)
float GetGlobalTimerTimeLeft(string &in asTimer)
{
int timerID = GetGlobalVatInt("GLOBALTIMER_ID_" + asTimer);
return GetGlobalVarFloat("GLOBALTIMER_TIME" + timerID);
}
//Set time left on a timer (set time to negative -GetTime if you want to pause the timer).
void SetGlobalTimerTimeLeft(string &in asTimer, float afTime)
{
int timerID = GetGlobalVatInt("GLOBALTIMER_ID_" + asTimer);
SetGlobalVarFloat("GLOBALTIMER_TIME" + timerID,afTime);
}
//Private routine called to update the timers
void _UpdateGlobalTimers(string &in asTimer)
{
float tick = 1.0f / 60.0f
//Update every timer (Existing timers indexed from 1..Count INCLUSIVE)
for(int i=1; i<=GetGlobalVarInt("GLOBALTIMER_COUNT"); i++)
{
//Tick the timer forward
float t = GetGlobalVarFloat("GLOBALTIMER_TIME" + i) - tick;
SetGlobalVarFloat("GLOBALTIMER_TIME" + i,t);
//Will Timer expire in about 1 tick?
if(t<2.0f*tick)
{
//Hit the timer.
string timerName = GetGlobalVarString("GLOBALTIMER_NAME_" + timerID);
string timerCallback = GetGlobalVarString("GLOBALTIMER_CALLBACK_" + timerID);
//Timer expired, hit the callback in 1 tick
AddTimer(timerName,t,timerCallback);
//Remove global timer via AddTimer so if the map changes before the callback is triggered the timer isn't destroyed!
AddTimer(timerName,t,"RemoveGlobalTimer");
}
}
//Call again in one tick.
AddTimer(asTimer,tick,"_UpdateGlobalTimers");
}
Warning: I'm on my laptop so this code is untested. It is the same approach I've used for global times in the past though, so minus any syntax errors and the onenter/onstart thing it should work.
(This post was last modified: 05-15-2013, 04:19 AM by Apjjm.)