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
How to detect when player uses or drinks potion
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#3
RE: How to detect when player uses or drinks potion

I don't think there are any reliable ways of detecting when a potion is drank in the inventory. The closest would be to use a looping timer to continuously check if HasItem is false after they pick it up, which would tell you when it is no longer in the inventory of the player. But looping a timer over a long time is risky business, as it may cause the game to crash if they loop over 65 000 times. But I suppose looping it only once a second should be fine, since it would take you like 20 hours to hit that crash limit.

Starts similarly to Rom's code:
PHP Code: (Select All)
void OnStart()
{
    
SetEntityCallbackFunc("potion_health_1""PickUpEvent");
}

void PickUpEvent(string &in asEntitystring &in Type)
{
    
// Start the timer loop
    
TimerLoopCheckItem("my_timer_name");
}

void TimerLoopCheckItem(string &in asTimer)
{
    if(
HasItem("potion_health_1") == false)
    {
        
// Item no longer exists in the player's inventory
    
}
    else 
    {
        
AddTimer(asTimer1.0f"TimerLoopCheckItem");
    }


This checks every 1 second if the item exists, which means it may take up to a second before the game detects it after you close the inventory. Unfortunately it won't automatically close the inventory or anything, but wait until it's closed manually.

(This post was last modified: 06-02-2019, 01:33 PM by Mudbill.)
06-02-2019, 01:24 PM
Find


Messages In This Thread
RE: How to detect when player uses or drinks potion - by Mudbill - 06-02-2019, 01:24 PM



Users browsing this thread: 1 Guest(s)