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
Andris Offline
Junior Member

Posts: 3
Threads: 2
Joined: May 2019
Reputation: 0
#1
How to detect when player uses or drinks potion

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?
06-01-2019, 03:09 PM
Find Reply
Romulator Offline
Not Tech Support ;-)

Posts: 3,623
Threads: 62
Joined: Jan 2013
Reputation: 195
#2
RE: How to detect when player uses or drinks potion

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: (Select All)
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.
    
}



Discord: Romulator#0001
[Image: 3f6f01a904.png]
(This post was last modified: 06-01-2019, 03:39 PM by Romulator.)
06-01-2019, 03:39 PM
Find Reply
Mudbill Offline
Muderator

Posts: 3,829
Threads: 59
Joined: Apr 2013
Reputation: 177
#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 Reply




Users browsing this thread: 1 Guest(s)