Search code examples
c++unreal-engine4

Function not getting called in Unreal


I'm new to Unreal. I'm part of a research project and I'm trying to learn the engine better as I proceed but there is something which has got me stuck for quite some time. I have a private function GetGrids declared in the .h file. I'm trying to call it in .cpp file but the control never enters that function. I have put a breakpoint in that function which which is not hit at all. I tried restarting visual studio, rebuilding a solution but nothing seems to work.

#include "RoboRev_Win.h"
#include "MapsManager.h"

AMapsManager::AMapsManager(const FObjectInitializer& ObjectInitializer)
    :Super(ObjectInitializer)
{
    m_row = MAP_SIZE;
    m_column = MAP_SIZE;
    m_startPoint = FVector(0, 0, 90);
    m_IsMapCreated = false;
    PrimaryActorTick.bStartWithTickEnabled = true;
}

void AMapsManager::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
    if (!m_IsMapCreated)
        CreateMap();
}

AGrid* AMapsManager::GetGridWithID(int _ID)
{
    for (int i = 1; i <= GRID_SIZE*GRID_SIZE; i++)
    {
        AGrid *_Grid = m_Grids[i];
        if (_Grid->ID == _ID)
        {
            return _Grid;
        }
    }
    return NULL;
}

void AMapsManager::GetGrids()
{
    int a, b, c;
    a = 1;
    b = 2;
    c = 3;
    int sum;
    sum = a + b + c;
    printf("");
    printf("");
}

void AMapsManager::CreateMap()
{
    UWorld* const _World = GetWorld();
    if (_World)
    {
        m_IsMapCreated = true;

        FActorSpawnParameters _Parameters;
        _Parameters.Owner = this;
        _Parameters.Instigator = Instigator;

        FVector _SpawnPosition;

        FRotator _Rotator(0, 0, 0);

        _SpawnPosition = FVector(0, 0, 0);

        originalX = _SpawnPosition.X - ((MAP_SIZE / 2)*GRID_SIZE);
        _SpawnPosition.X = originalX;
        _SpawnPosition.Y = originalX;

        _CurrentID = 1;
        for (int i = 1; i <= m_row; i++)
        {
            for (int j = 1; j <= m_column; j++)
            {
                AGrid *_Grid = static_cast<AGrid *>(_World->SpawnActor(m_GridPawn, &_SpawnPosition, &_Rotator, _Parameters));
                _Grid->ID = _CurrentID;
                m_Grids.push_back(_Grid);
                _SpawnPosition.X += GRID_SIZE;
                _CurrentID++;
            }
            _SpawnPosition.Y += GRID_SIZE;
            _SpawnPosition.X = originalX;
        }
    }
    int _MapIDs[] = { 1, 2, 3, 4, 5, 10, 15, 20, 25 };
    int k = 9;
    for (int l = 0; l < k; l++)
    {
        //AGrid *_Grid = GetGridWithID(_MapIDs[l]);
        //_Grid->ChangeMaterial();
        this->GetGrids();
    }
}

Don't mind what the function is doing. It is just a test function.

This is the declaration in .h file

private:
       void GetGrids();

please help!


Solution

  • Your Visual Studio compiler is configured to pretty aggressively optimize the code and calls of your functions. Since your GetGrids() method does not that has any influence on the running program, the compiler just drops the call altogether or inlines stuff like print. Even when debugging with breakpoints you can not jump into a method that the compiler optimized away.

    Try adding something like this to your function, so you can see it in the running engine.

    UE_LOG(LogExec, Warning, TEXT("GetGrids() was called."));