1
\$\begingroup\$

I am trying to create a game similar to a board game on a hexagonal grid (also similar to Civilization game).

I want the tiles to be able to display various textures and of course be able to detect objects on there, and add improvements to the tile etc. So I wrote a C++ script to spawn each cell in a grid format. It looks nice and seems to work but even a small grid really slows down my computer in the Unreal Editor.

I am not sure if this is down to my code, or my model (which was just a simple hexagonal plane made in Maya using 6-sided cylinder). I have a fairly decent system (6700k, 1660GTX, 32GB Ram) so I didn't expect the bad performance from what I have created.

#include "Grid.h"

#include "GridCell.h"

// Sets default values
AGrid::AGrid()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AGrid::BeginPlay()
{
    Super::BeginPlay();
    
    for(int y=0; y<Grid_Height; y++)
    {
        for(int x=0; x<Grid_Width; x++)
        {
            //Cells[x+(y*x)]=GetWorld()->SpawnActor<AGridCell>(Grid_Cell_BP->StaticClass());
            FVector location=FVector(x*100.f, y*75.f, 0);
            if(y%2!=0)
                location.X+=50.f;
            Cells.Add(GetWorld()->SpawnActor<AGridCell>(Grid_Cell_BP, location, FRotator::ZeroRotator));

        }
    }
}

// Called every frame
void AGrid::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}


#include "GridCell.h"

// Sets default values
AGridCell::AGridCell()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    Mesh=CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
    RootComponent=Mesh;


}

// Called when the game starts or when spawned
void AGridCell::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called every frame
void AGridCell::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

My project so far, I just have a "Grid" blueprint dragged into the scene. I set the values at 20x20 size grid.

Everything does spawn and looks great, but it is very slow. I had hoped to have much larger maps.

enter image description here

Is there another approach I could use, perhaps that has one large rectangle plane and then lay the hexagons over that maybe?

Also, I feel it's probably a bad idea to make each Cell an "AActor" . Does it need to be, how could I do it without it being an AActor?

Any ideas would be great. Thank you.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ It's possible to use a shader to draw your grid of hex cells using just a single mesh for the whole world, and a small index texture with 1 pixel per tile storing tile information, which the shader uses to select appropriate textures/etc. I show an example of this in Unity here. You could use something like this for rendering, then have a very lightweight data structure in an array storing the gameplay state of each tile, rather than spawning complex objects for every one of them. \$\endgroup\$
    – DMGregory
    Commented Nov 16, 2022 at 23:40
  • \$\begingroup\$ Thank you very much I will read through the link and give it a try. \$\endgroup\$ Commented Nov 17, 2022 at 21:13

0

You must log in to answer this question.

Browse other questions tagged .