I need help. How to unsubscribe the players when they don't have the item?

I’m working in a system where if the player has the item in inventory, he will become invisible while crouching. But i can’t make the “power” disable after dropping the item!

Here is the code. Someone please help me ;´(


using { /Fortnite.com/Devices }
using { /Fortnite.com/Game }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Verse.org/Verse }

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.

# A Verse-authored creative device that can be placed in a level
invisibility_manager := class(creative_device):

    @editable
    ItemDetector: conditional_button_device = conditional_button_device{}
    
    OnBegin<override>()<suspends>:void=
        
        spawn:
            CheckForItem()
        
        
            
        
    GoInvisibleOnCrouche(FortniteCharacter:fort_character, IsCrouched : logic) : void =
        if(IsCrouched?):
            FortniteCharacter.Hide()
        else:
            FortniteCharacter.Show()

    CheckForItem()<suspends>:void=
        loop:
            Sleep(1.0)
            AllPlayers:=GetPlayspace().GetPlayers()
            for(Player:AllPlayers):
                if(IsAgent:agent = agent[Player]):
                    if(ItemDetector.HasAllItems[IsAgent]):
                        Print("has item")
                        if(FortCharacter := IsAgent.GetFortCharacter[]):
                            FortCharacter.CrouchedEvent().Subscribe(GoInvisibleOnCrouche)
                    else:
                        
                            

                        Print("dont have items")
    

I’m new at Verse and programming! How to make it work?

In the method you’re using you check to see if the players holding the card, and then you start a subscription. A subscription will go until cancelled, so it’s no longer checking that the players holding the card, its just checking to see if they’re crouching.
Also because you’re running it in a loop you’re building up a bunch of these subscriptions so it’s going to trigger each additional subscription per second of gameplay that you have going (this is a rather large problem, the longer this goes on for the worse the memory issues will get).

Take a look at this aproach:

invisibility_manager := class(creative_device):

    @editable
    ItemDetector: conditional_button_device = conditional_button_device{}
    
    OnBegin<override>()<suspends>:void=
        AllPlayers:=GetPlayspace().GetPlayers()
        for(Player:AllPlayers):
            if(FortCharacter := Player.GetFortCharacter[]):
                FortCharacter.CrouchedEvent().Subscribe(GoInvisibleOnCrouche)
        
        
    GoInvisibleOnCrouche(FortniteCharacter:fort_character, IsCrouched : logic) : void =
        if( IsCrouched=true , Agent:=Fort.GetAgent[] , ItemDetector.HasAllItems[Agent] ):
            FortniteCharacter.Hide()
        else:
            FortniteCharacter.Show()
     

So now instead of creating a loop that subscribes, we’re just doing the single subscribe per player. You really did fundamentally have the right idea of how it should work logically, every second you would check to see if the player was crouching, and if they were holding the card they’d go invisible. That is different than subscribe actually works though.
Its a tool built to do the “crouch checking” for you, and will signal the subscribed function every time you crouch. We set up a “listener” for the trigger so it does it automatically for us once we subscribe to it.
So in the method above we don’t have to run any loop, instead we simply get the trigger for crouching subscribed to first, then whenever that event triggers we check if the players holding the card.

2 Likes

It worked! Thank you very much! I’ll try to learn with your example and turn better my codding.

1 Like