Frictional Games Forum (read-only)

Full Version: How to detect when player uses or drinks potion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Scenario in my custom story:  the player finds a health potion (laudanum) and when they drink it a specific event happens. I just don't know how to detect the moment when player drinks that potion. AddUseItemCallback() function (where asItem is health potion and asEntity is Player) is doesn't work even if player's health isn't full. Or it is not possible to do?
Create a new item in the Model Editor so that it is just an item that you can pickup in the World, then code it so that Daniel automatically drinks it and triggers the event.

Spoiler below!
PHP Code:
void OnStart()
{
    
SetEntityCallbackFunc("potion_health_1""Drink_Event");
}

void Drink_Event(string &in asEntitystring &in Type)
{
    
RemoveItem(asEntity);
    if(
Type == "OnPickup")
    {
        
AddPlayerHealth(25.0f);
        
//Rest of the event goes here. This includes any effects you want to use.
    
}


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:
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.