zoukankan      html  css  js  c++  java
  • Replicating a 2D dynamic array

    转自:https://forums.unrealengine.com/community/community-content-tools-and-tutorials/105-saxonrahs-tutorial-thread-random-maze-generation-solving/page2?47-SaxonRahs-Tutorial-Thread-Random-Maze-Generation-amp-Solving=&viewfull=1

    I made my own USTRUCT system for having a multi-dimensional UPROPERTY() friendly dynamic array version of your maze data structure.

    Here's that code for you and others to enjoy!

    USTRUCT()
    struct FMazeGridRow
    {
        GENERATED_USTRUCT_BODY()
    
        UPROPERTY()
        TArray<AStaticMeshActor*> Columns;
        
        void AddNewColumn()
        {
            Columns.Add(NULL);
        }
        //default properties
        FMazeGridRow()
        {
            
        }
    };
    USTRUCT()
    struct FMazeGrid
    {
        GENERATED_USTRUCT_BODY()
    
        UPROPERTY()
        TArray<FMazeGridRow> Rows;
        
        void AddNewRow()
        {
            Rows.Add(FMazeGridRow());
        }
        
        void AddUninitialized(const int32 RowCount, const int32 ColCount)
        {
            //Add Rows
            for(int32 v = 0; v < RowCount; v++)
            {
                AddNewRow();
            }
            
            //Add Columns
            for(int32 v = 0; v < RowCount; v++)
            {
                for(int32 b = 0; b < ColCount; b++)
                {
                    Rows[v].AddNewColumn();
                }
            }
        }
        
        void Clear()
        {
            if(Rows.Num() <= 0) return;
            //~~~~~~~~~~~~~~~
            
            //Destroy any walls
            const int32 RowTotal = Rows.Num();
            const int32 ColTotal = Rows[0].Columns.Num();
            
            for(int32 v = 0; v < RowTotal; v++)
            {
                for(int32 b = 0; b < ColTotal; b++)
                {
                    if(Rows[v].Columns[b] && Rows[v].Columns[b]->IsValidLowLevel() )
                    {
                        Rows[v].Columns[b]->Destroy();
                    }
                }
            }
            
            //Empty
            for(int32 v = 0; v < Rows.Num(); v++)
            {
                Rows[v].Columns.Empty();
            }
            Rows.Empty();
        }
        //default properties
        FMazeGrid()
        {
            
        }
    };
    //Now you have dynamic array benefits and also UPROPERTY()!
    UPROPERTY()
    FMazeGrid JoyMazeGrid;
    //Init Maze
    JoyMazeGrid.Clear();
    JoyMazeGrid.AddUninitialized(tileX, tileY);
        
    //Sample usage
    //JoyMazeGrid.Rows[x].Columns[y] = SpawnBP<AStaticMeshActor>(GetWorld(), ...

    Summary

    The final result of all of this is that you have

    1. dynamic array benefits (easily change the maze size and regenerate during runtime)

    2. global access to the MazeGrid Actors, even from other classes

    3. GC protection from the UPROPERTY() (if not using actors/objects for some reason)

    4. And yet the syntax for 2D array using UPROPERTY() and TArray is still clean and clear and precise

    JoyMazeGrid.Rows[x].Columns[y]

    Enjoy!

    Rama

  • 相关阅读:
    [2013腾讯马拉松 3月23日]HDU 4517 小小明系列故事——游戏的烦恼
    金山西山居初赛第三场 HDU 4551~HDU 4553
    Redis安装
    前端常用网址汇总
    Html5移动端页面布局通用模板暨移动端问题总结
    js数组去重,并统计最多项算法
    纯css实现下拉菜单
    js实现求平均数功能
    Html5+css3实现3D转动效果
    移动设备分辨率及响应式断点汇总
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/8134511.html
Copyright © 2011-2022 走看看