智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...
随笔 - 991, 文章 - 0, 评论 - 27, 阅读 - 341万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Replicating a 2D dynamic array

Posted on   Bill Yuan  阅读(486)  评论(0编辑  收藏  举报

转自: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

 

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2013-12-28 cocos2d-x 手电筒效果
2013-12-28 cocos2d-x 二进制文件的读写
2013-12-28 cocos2d-x 获取系统时间
2013-12-28 cocos2d-x如何截屏并保存图片
2009-12-28 程序英语
点击右上角即可分享
微信分享提示