Array/Map not referenced correctly across multiple classes

Hello,
I am running into this problem where an array or map created in one class is not properly referenced in other classes except of one.

This is the main_manager (In the real world it would be the game_manager):

using { /Fortnite.com/Devices }

main_manager := class(creative_device):

    ArrayManager : array_manager = array_manager{}
    MiddleManager : middle_manager = middle_manager{}

    OnBegin<override>()<suspends>:void=
        ArrayManager.InitArr()
        MiddleManager.ActivateCallManager()
        Print("TestArr in main_manager has Length of {ArrayManager.TestArr.Length}")

This calls the initialisation of the test array_manager (Irl it would call a InitPlayer function in a player_manager after the player spawned or something). This is what the array_manager (Irl player_manager) looks like:

array_manager := class():
    
    var TestArr : []int = array{}

    InitArr() : void =
        set TestArr = {1, 2, 3}
        Print("TestArr in array_manager has Length of {TestArr.Length}")

The main_manager also calls the middle_manager which I just use for further debugging to check where the reference fails:

middle_manager := class():
    
    CallManager : call_manager = call_manager{}
    ArrayManager : array_manager = array_manager{}

    ActivateCallManager() : void =
        CallManager.CallArr()
        Print("TestArr in middle_manager has Length of {ArrayManager.TestArr.Length}")

The middle_manager invokes the call_manager (Irl a team_manager for example which references an array of all players from the player_manager):

call_manager := class():
    
    ArrayManager : array_manager = array_manager{}

    CallArr() : void =
        for(Number : ArrayManager.TestArr):
            Print("Current Iteration is: {Number}")
        Print("TestArr in call_manager has Length of {ArrayManager.TestArr.Length}")

The test array is referenced correctly in the array_manager ofc and the main_manager but as soon as it comes to the middle and call_manager the array is empty:
Screenshot (69)
If somebody could help me on this it would be greatly appreciated! Thank you for your time!

Hi.
This is curious to me.

I was able to duplicate this. I don’t have a complete solution yet, but it is telling that the For expression is failing, which also confirms the array is returning as empty. I was just wondering when you create multiple instances of the array_manager with the same name if that isn’t the problem. Wouldn’t that, in a sense, create null references? If an array is going to be referenced by multiple classes, I’m wondering if it wouldn’t be better to create a superclass and have the other classes inherit from the superclass, so there is no need to create so many instances with the same name.

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



main_manager := class(creative_device):

   

    OnBegin<override>()<suspends>:void=
        ArrayManager: array_manager= array_manager:
        MiddleManager: middle_manager = middle_manager:
        ArrayManager.InitArr()
        MiddleManager.ActivateCallManager()
        Print("TestArr in main_manager has Length of {ArrayManager.TestArr.Length}")



array_manager := class():
    
    var TestArr : []int = array{1,2,3}

    InitArr() : void =
        Print("TestArr in array_manager has Length of {TestArr.Length}")


middle_manager := class(array_manager):
    
    CallManager : call_manager = call_manager{}

    ActivateCallManager() : void =
        CallManager.CallArr()
        Print("TestArr in middle_manager has Length of {TestArr.Length}")


call_manager := class(array_manager):
    
    CallArr() : void =
        for(Number : TestArr):
            Print("Current Iteration is: {Number}")
        Print("TestArr in call_manager has Length of {TestArr.Length}")
    # Runs when the device is started in a running game

So this works except the set array is taken out. I will have to look at this more later.

1 Like

Thank you for your answer!
Your solution works for me aswell. This is strange because even when the array is initialized with the set method (which is mendetory for what I am trying to do) it gets referenced correctly only by the main_manager… I am curios what you can find out about this.

Did you manage to find something?

So I kinda solved it myself.
The problem I encountered here is because of my misunderstanding of object oriented programming. If I reference a var (TestArr) of a class (ArrayManager) from another (like MiddleManager) verse creates a copy of some kind of the whole class (ArrayManager) and also of the var (TestArr) and because the var hasn’t ben initialized (InitArr() hasn’t been called) through the calling class (MiddleManager) yet it pickes up the empty var (TestArr).
This is also the reason why the MainManager picked the TestArr up correctely because for this class the array got initialized. Bur ONLY for this class.
So the soluion is to Initialize the array once in something like a GameManager and pass the array to every other function which needs it as a Parameter.
This whole object oriented programming topic is hard to grasp for me but I am slowly getting the hang of it. The Problem is always in fornt of the screen…