Can we get data from a class into another class?

How can we get a value from another class?

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

Test := class<unique>(creative_device):

    var PlayerIntMap : [player]int = map{}
    TestClass2 : Test2 = Test2{}

    OnBegin<override>()<suspends>:void=
        if:
            Player := GetPlayspace().GetPlayers()[0]
            set PlayerIntMap[Player] = 777
        then:
            #sucess set
            TestClass2.A(Player) #success call

    ReturnMap():[player]int=
        return PlayerIntMap
    
    ReturnInt(Player : player):int=
        var IntValue : int = 0
        if:
            PlayerInt := PlayerIntMap[Player]
            set IntValue = PlayerInt
        return IntValue

Test2 := class(): #not creative_device

    @editable TestClass : Test = Test{}

    A(Player : player):void=
        if(PlayerInt := TestClass.PlayerIntMap[Player]):
            #nothing

        if(PlayerInt := Test{}.PlayerIntMap[Player]):
            #nothing

        TestInstance := Test{}
        if(PlayerInt := TestInstance.PlayerIntMap[Player]):
            #nothing
        
        NewInstanceMap := TestClass.PlayerIntMap
        if(PlayerInt := NewInstanceMap[Player]):
            #nothing

        NewInstanceMap2 := TestClass.ReturnMap()
        if(PlayerInt := NewInstanceMap2[Player]):
            #nothing

        PlayerIntF := TestClass.ReturnInt(Player)
        Print("{PlayerIntF}")
        #nothing
        

The problem with your attempt is that the Test2 object doesn’t access the good Test object.

@editable TestClass : Test = Test{} # Here you're creating a new Test object using its constructor, so the functions are usable but
# they won't return any data you've set in your code above

Ok so basically I think of 3 ways to do this :

  • You give a reference to your TestClass (device) when initializing your Test2 class, like this :
Test2 := class():
    MyDevice:Test

Test := class<unique>(creative_device):
    var TestClass2 : ?Test2 = false

    OnBegin<override>()<suspends> : void =
        set TestClass2 = option{Test2{MyDevice := Self}}

Now your Test2 class has a link to your Test device and your functions are working, you just need to unwrap your TestClass2 everytime (surely there’s a bit cleaner way to do it)

  • You try to implement singletons as described in I came up with a way to make singletons in Verse
    This solution is cleaner if you’re gonna have lots of classes each connected to eachother.

  • You make your Test2 a device too, and with @editable attribute you assign both the Test and Test2 to eachother inside UEFN.

1 Like

Hello how is the situation right now? When i try to connect two verse devices to each other with @editable, uefn crashes

You have an infinite loop issue, you should try linking them using optionals : @editable MyDeviceMaybe : ?my_device = false

THANK YOU, YOU ARE THE GOAT.

Really though, as a beginner this was something I was seriously struggling with!

Thank you so much! This example should be put directly in the docs for how to handle passing of data between classes.

1 Like