Announcement

Collapse
No announcement yet.

Working With Dynamic Arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Working With Dynamic Arrays

    Dear Everyone,

    If you are using code to spawn objects, you more than likely want to keep track of those objects!

    You also are probably wanting to spawn objects that are instances of your own Classes.

    Doing this is can seem a bit syntactically convoluted at first, but once you get used to it it's easy!


    Here are the basics:

    Simple Dynamic Array

    Code:
    var array<int> intArray;
    Note that you do not use new, or try to make an instance of this var in anyway.

    ~~~

    Deleting Dynamic Arrays

    The .length of the array can be adjusted to 0 to delete entries:

    Code:
    yourArray.length = 0; 
    //deletes all entries from array, but does not delete actual objects in the array
    ~~~

    Memory Allocation

    The memory for the array is automatically allocated when you use the array function AddItem.

    ~~~

    .AddItem()

    //continuing above example
    Code:
    intArray.AddItem(5);
    //adds a single item to intArray whose int value is 5
    The additional memory is allocated by Unreal engine for you.


    So

    let me repeat:

    You NEVER need to use the new command with dynamic arrays, in regards to the array itself.

    ~~~

    Making Instances of Your Class Via Code

    To make new instances of a class you can say:

    Code:
    local MyHappyClass happyObj;
    
    happyObj = new class'MyHappyClass';
    ~~~

    Local vs Global Vars With Dynamic Arrays
    How to Save a Local var To a Global Dynamic Array

    You use "local" for all variables when inside a function, "var" for global variables that are part of class itself


    local variables get deleted at the end of the function execution, so a dynamic array you want to repeatedly refer to needs to be GLOBAL and made with "var" not local.

    Locally created variables, made inside a function, persist after the end of the function if stored inside a global dynamic array variable.

    Code:
    class Joy extends Happiness;
    
    //Global Variable
    var array<MyHappyClass> happyArray;
    
    function newHappyObj(){
       local MyHappyClass v;
       local int willGetLost;  //note the name
    
       v = new class'MyHappyClass';
    
       happyArray.AddItem(v);
    
       willGetLost = someVeryImportantValueButAlasNoOneWillKnow;
       return; //end of local context
    }
    After this function runs willGetLost is invalid, but v will live on, referenced by the global array happyArray.

    In this way you can create elements of your global dynamic variable locally, inside a function, but then access them again later in your game's running time via the dynamic array.

    ~~~

    Making Array of Instances of Your Class


    But how to make a DynamicArray of your custom class?


    You might find info as I did on the net saying things like

    Code:
    var array<class<Your class> >; 
    
    //this code is NOT an array of variables of your custom class type


    with everyone discussing how you have to space out the two >> so it is not read as an operator.

    But this will make an array of CLASSES of your type of class, not objects that are the instances of your class.

    ~~

    The Actual Correct Code

    to make an array of the actual INSTANCES of your class, you must use this code


    THIS IS THE KEY SYNTAX!


    THE BIG THING which I had to figure out after hours of research



    Code:
    var array <MyHappyClass> happyOBJArray;


    That's it!


    Surprisingly simple.

    ~~~

    Summary:

    Making dynamic arrays and handling their memory allocation is very simple using Unreal Script using the above bits of code.

    Now you can dynamically generate Actors / Objects / Level Geometry and keep track of it easily!


    ~~~

    Dynamic World Making In Game

    I've used dynamic arrays understanding to make a game with UDK where you can build the level yourself inside of the actual game, and save it to file, and load it after closing the Unreal Game Engine and even restarting your computer.

    ~~~

    Enjoooy!





    EverNewJoy



    PS: See http://wiki.beyondunreal.com/Dynamic_array

    for dynamic array functions

    PSS:

    No Internet Replication

    Dynamic Arrays cannot be easily used for multiplayer games, as dynamic arrays are never replicated to the client from the server.

    So when planning your game structure keep in mind you will have to send data from the server to the client some other way

    Kismet

    Use kismet to handle dynamic objects via the Actor Factory in multiplayer levels.

    I've tested this and it works wonderfully.

    #2
    good info, will be usefull for someone no doubt.

    just one thing to note,
    var array<class<Your class> >;
    this is not wrong,its for a different circumstance.
    cant think of any off the top of my head sorry, but i have used it before for something.

    edit;
    i used it for having a list of projectiles on a vehicle weapon, but here is an example in the udk script classes.
    in the kismet node (script) SeqAct_GiveInventory

    var() array<class<Inventory> > InventoryList;

    fairly self explanatory if you have ever used that kismet node.

    Comment


      #3
      Yes sorry, I will update my comment to clarify what I mean

      Thanks for the example Tegleg!

      Great to hear from you!



      Rama

      Comment


        #4
        Hi everyone!

        I trying to handle my network players in dymamic array, but I can't modify players's data.

        That's how I adding new player to the game.


        Code:
        var Array<AlbertaPawn> Players;
        
        function AddNewPlayer(string pLogin)
        {
            local AlbertaPawn CurrentPlayer;
            CurrentPlayer = Spawn(class'AlbertaPawn');
            CurrentPlayer.Stats.Login = pLogin;
            Players.AddItem(CurrentPlayer);
        
            NetLog.AddItem("CurrentPlayer.Stats.Login =" @ CurrentPlayer.Stats.Login);
            NetLog.AddItem("pLogin =" @ pLogin);
        }
        So in log there is two strings. So the CurrentPlayer.Stats.Login is none...
        CurrentPlayer.Stats.Login =
        pLogin = angel


        How can it be? CurrentPlayer.Stats.Login is a string variable.

        Comment


          #5
          Awesome tutorial. Im sure many people can get a decent lesson out of this, either for a beginner or for someone coming back to the engine to review the basics.

          Comment


            #6
            @Tilk

            If you will read what Evernewjoy said in his post above:

            Dynamic Arrays cannot be used for multiplayer games, as dynamic arrays are not replicated to the client

            This is what I've been told by reasonably official documented sources anyways.

            Comment


              #7
              If you will read what Evernewjoy said in his post above
              Year, I've read it. But looks like you read my post not so carefully.

              This block of code... There is nothing about network. This is just function to spawn Pawn Actors and add its to array.

              Code:
              var Array<AlbertaPawn> Players;
              
              function AddNewPlayer(string pLogin)
              {
                  local AlbertaPawn CurrentPlayer;
                  CurrentPlayer = Spawn(class'AlbertaPawn');
                  CurrentPlayer.Stats.Login = pLogin;
                  Players.AddItem(CurrentPlayer);
              
                  NetLog.AddItem("CurrentPlayer.Stats.Login =" @ CurrentPlayer.Stats.Login);
                  NetLog.AddItem("pLogin =" @ pLogin);
              }
              I'm using my own network code, based on TCPLink class and I just get string "newplayer,somelogin;" from server and call AddNewPlayer(somelogin). I can use same code without network just by Left Mouse Click, and result will be same.


              ==UPD==


              Guys sorry. I just fogot to spawn Stats Actor in CurrentPlayer (CurrentPlayer.Stats == none). No any problems with arrays.

              evernewjoy, much thanks for good tutorial =)

              Comment


                #8
                yay! I am glad the issue got solved

                Rama

                Comment


                  #9
                  I updated the content of the OP with some of my latest understandings / clarifications



                  Rama

                  Comment


                    #10
                    Oh, is it the this time of the year again when someone posts a dynarray tutorial?

                    Comment


                      #11
                      Originally posted by Wormbo View Post
                      Oh, is it the this time of the year again when someone posts a dynarray tutorial?


                      Lovely to hear from you Wormbo!

                      I love your Replication Tutorial (see his signature)

                      and your other community offerings!



                      Rama

                      Comment

                      Working...
                      X