The prelaunch Task "Build Debug" terminated with exit code 2

I am trying to make this work but whenever I use class outside the file it crashes…
Here is my class:

#include"raylib.h"

#include"raymath.h"

int winWidth{382};

int winHeight{382};

class Character

{

public:

Vector2 GetWorldPos() { return worldPos; }

void SetScreenPos(int winwidth, int winheight);

void Tick(float deltaTime);

private:

Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D idle{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture run{LoadTexture("characters/knight_run_spritesheet.png")};

Vector2 screenPos{};

Vector2 worldPos{};



// 1.f facing right, -1.f facing left

float rightLeft{1.f};

// animation variables

float runningTime{};

int frame{};

int maxFrames{6};

float updateTime{1.f / 12.f};

float speed{4.f};

};


#include"Character.h"

void Character::SetScreenPos(int winwidth, int winheight)
{
    screenPos = {
        (float)winwidth / 2.0f - 4.0f * (0.5f * (float)texture.width),
        (float)winheight / 2.0f - 4.0f * (0.5f * (float)texture.height)};
};
void Character::Tick(float deltaTime)
{
    Vector2 direction{};
    if (IsKeyDown(KEY_A))
        direction.x -= 1;
    if (IsKeyDown(KEY_D))
        direction.x += 1;
    if (IsKeyDown(KEY_W))
        direction.y -= 1;
    if (IsKeyDown(KEY_S))
        direction.y += 1;
    if (Vector2Length(direction) != 0.0)
    {
        // set worldPos = worldPos + direction
        worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
        direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
        texture = run;
    }
    else
    {
        texture = idle;
    } 
    // update animation frame
    runningTime += deltaTime; // update with frame time (delta time)
    if (runningTime >= updateTime)
    {
        runningTime = 0.f;
        frame++;
        if (frame > maxFrames)
            frame = 0;
    }
    //DrawTextureEx(texture, worldPos, 0.0, 4.0, WHITE);  //all sprites rendered from the sheet..
    // Draw the character
    Rectangle source{frame * (float)texture.width / 6.f, 0.f,
                     rightLeft * (float)texture.width / 6.f, (float)texture.height};
    Rectangle dest{screenPos.x + screenPos.y, 
                   screenPos.y, 4.0f * (float)texture.width / 6.0f,
                    4.0f * (float)texture.height};
    DrawTexturePro(texture, source, dest, Vector2Zero() , 0, WHITE);
};

Can you show me the exact error code you’re getting when you’re trying to compile? The exit code on its own does not tell me much about the potential issue.

I got around this by putting all my code in file.h
That means for example Character.h has class definitions together with declarations.
At this point the error disappears and all is ok. I think that the compiler or linker rather
cannot find the cpp file related to respective header file.
Maybe this helps… I do not know how to give more information than that.
Thank you. \So, maybe there is a way to tell the linker that Prop.h and Prop.cpp are related?

In the meantime I consider this fine for now. I have huge problems with collision detection, it does but the collision detection is shifted… This is a problem for another topic though…

In the cpp file you would use #include file.h where ‘file’ is the name of your header file.

While putting everything in the header file can work, this would become cumbersome for larger code-bases or class definitions.

As for showing me the error, a screenshot of your terminal output after you try to compile would have been enough.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms