Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Using Global.hps to use a script in every level
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#11
RE: Using Global.hps to use a script in every level

The global.hps file can call functions, though only the HPL2 functions and the ones defined in it (since HPS files cannot access each other), but most of the HPL2 functions aren't practical for the global.hps file since the file is compiled and loaded before any map is loaded. I use it mostly for blocking default hints and disabling darkness sanity loss.

Tutorials: From Noob to Pro
(This post was last modified: 05-15-2013, 01:43 AM by Your Computer.)
05-15-2013, 01:40 AM
Website Find
Tomato Cat Offline
Senior Member

Posts: 287
Threads: 2
Joined: Sep 2012
Reputation: 20
#12
RE: Using Global.hps to use a script in every level

(05-15-2013, 01:40 AM)Your Computer Wrote: The global.hps file can call functions, though only the HPL2 functions and the ones defined in it (since HPS files cannot access each other), but most of the HPL2 functions aren't practical for the global.hps file since the file is compiled and loaded before any map is loaded. I use it mostly for blocking default hints and disabling darkness sanity loss.

So that's why classes don't work across .hps files?
05-15-2013, 01:51 AM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#13
RE: Using Global.hps to use a script in every level

(05-15-2013, 01:51 AM)Tomato Cat Wrote: So that's why classes don't work across .hps files?

They can't be accessed across HPL2 scripts because they're not saved to be accessed across multiple scripts, unlike the map variable functions.

Tutorials: From Noob to Pro
05-15-2013, 02:52 AM
Website Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#14
RE: Using Global.hps to use a script in every level

(05-15-2013, 01:26 AM)JustAnotherPlayer Wrote: Ok, so it can't do so. Can it do the include command by Apjjm?
The only way to get a separate script file to run in every map is to use a bit of a hack like including a preprocessor to add stuff like #include. This is akin to just pasting the script at the top of all your script files though, except you have some tool do it all for you automatically, and you only have to make changes in one place if you mess something up.

To re-iterate what people have said about global.hps, i think it only runs once as soon as the custom story is started (or new game pressed). You can't add timers or any stuff like that using it. Even if it was run at the start of every map, classes and such in one script file are only in the scope of that script file in amnesia.
05-15-2013, 03:30 AM
Find
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#15
RE: Using Global.hps to use a script in every level

Shit, no global timers for me.

"Veni, vidi, vici."
"I came, I saw, I conquered."
05-15-2013, 03:37 AM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#16
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.)
05-15-2013, 04:18 AM
Find
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#17
RE: Using Global.hps to use a script in every level

The time unit is ms right?

"Veni, vidi, vici."
"I came, I saw, I conquered."
05-15-2013, 04:42 AM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#18
RE: Using Global.hps to use a script in every level

(05-15-2013, 04:42 AM)JustAnotherPlayer Wrote: The time unit is ms right?
The time unit for the global timers stuff should be the same as the time unit for normal timers, i.e. in seconds.
05-15-2013, 05:25 AM
Find




Users browsing this thread: 2 Guest(s)