Results 1 to 3 of 3

Thread: Dynamic Arrays

  1. #1

    Default Dynamic Arrays

    I was readying the new Unreal Development Kit Game Programming with Unrealscript: Beginner's Guide. I believe I found a mistake when it comes to compiling this array:
    //AwesomeActor Sprite
    class AwesomeActor extends Actor
    placeable; //Allows to be placed into editor
    var int TestArray[3];
    var array<int> CopyArray;
    function PostBeginPlay()
    {
    TestArray [0]= 9;
    TestArray [1]= 5;
    TestArray [2]= 6;
    CopyArray[CopyArray.length] = TestArray[CopyArray.length 1];
    CopyArray[CopyArray.length] = TestArray[CopyArray.length 1];
    CopyArray[CopyArray.length] = TestArray[CopyArray.length 1];
    `log("Copy array length:" @CopyArray.length);
    }
    defaultproperties
    {
    Begin Object Class=SpriteComponent Name=Sprite
    Sprite=Texture2D'EditorResources.S_NavP'
    HiddenGame=True
    End Object
    Components.Add(Sprite)
    }

    The intention was to have CopyArray be a dynamic array but it doesn't compile unless I add a plus or minus symbol. Is there a reason for this? Was their a mistake from the author? or did I miss something?

  2. #2

    Default

    TestArray is a Array. CopyArray is a dynamic array.
    To add items to a dynamic array, you use Add or AddItem (or the method you were trying to do).

    http://udn.epicgames.com/Three/Unrea...es.html#Arrays
    http://udn.epicgames.com/Three/Unrea....html#DynArray

    I don't get the intention behind your code. You are changing the CopyArray dynamically and using the length to access a static array. It would result into a "out of bound" error.

    Code:
        TestArray [0]= 9;
        TestArray [1]= 5;
        TestArray [2]= 6;
        CopyArray[CopyArray.length] = TestArray[CopyArray.length - 1]; // CopyArray[0] = TestArray[0 - 1] // out of bound
        CopyArray[CopyArray.length] = TestArray[CopyArray.length - 1]; // CopyArray[1] = TestArray[1 - 1]
        CopyArray[CopyArray.length] = TestArray[CopyArray.length - 1]; // CopyArray[1] = TestArray[2 - 1]
        `log("Copy array length:" @CopyArray.length);
    If you want to copy an array (not a dynamic array) use a for loop and the ArrayCount function.
    Code:
    local int i;
    //CopyArray.Empty
    CopyArray.Length = 0;
    for (i = 0; i<ArrayCount(TestArray); i++) {
       CopyArray.Length[i] = TestArray[i];
    }

  3. #3


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.