Having a little problem my texture2d members aren't being overriden

and are loading the ones from BaseCharacter which is the knight texture

image

main.cpp

#include "raylib.h"
#include "raymath.h"
#include "Character.h"
#include "Prop.h"
#include "Enemy.h"
#include <cstdio>

int main()
{
    const int windowWidth{384};
    const int windowHeight{384};
    char gameName[] = "classy_clash";

    InitWindow(windowWidth, windowHeight, gameName);

    Texture2D map = LoadTexture("nature_tileset/OpenWorldMap24x24.png");
    Vector2 mapPos{};
    float mapScale = 4.0f;

    Character knight{windowWidth, windowHeight};
    // knight.SetScreenPos(windowWidth, windowHeight);

    // Prop rock(Vector2{}, LoadTexture("nature_tileset/Rock.png"));
    Prop props[2]
    {
        Prop(Vector2{600.0f, 300.0f}, LoadTexture("nature_tileset/Rock.png")),
        Prop(Vector2{400.0f, 500.0f}, LoadTexture("nature_tileset/Log.png"))
        };

    Enemy goblin
    {
        Vector2{},
        LoadTexture("characters/goblin_idle_spritesheet.png"),
        LoadTexture("characters/goblin_run_spritesheet.png")
        };

    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(WHITE);

        mapPos = Vector2Scale(knight.GetWorldPos(), -1.f);

        // Draw map
        DrawTextureEx(map, mapPos, 0.0, mapScale, WHITE);

        // rock.Render(knight.GetWorldPos());

        // Draw the props
        for (auto prop : props)
        {
            prop.Render(knight.GetWorldPos());
        }

        knight.Tick(GetFrameTime());

        // Check map bounds
        if (knight.GetWorldPos().x < 0.f ||
            knight.GetWorldPos().y < 0.f ||
            knight.GetWorldPos().x + windowWidth > map.width * mapScale ||
            knight.GetWorldPos().y + windowHeight > map.height * mapScale)
        {
            knight.UndoMovement();
        }

        // Check for prop collision
        for (auto prop : props)
        {
            if (CheckCollisionRecs(prop.GetCollision(knight.GetWorldPos()), knight.GetCollision()))
            {
                knight.UndoMovement();
            }
        }

        goblin.Tick(GetFrameTime());

        // DrawFPS(0, 0);
        EndDrawing();
    }
    UnloadTexture(map);
    CloseWindow();
}

BaseCharacter.cpp

#ifndef BASE_CHARACTER_H
#define BASE_CHARACTER_H
#include "raylib.h"

class BaseCharacter
{
public:
    BaseCharacter();
    Vector2 GetWorldPos() { return worldPos; }
    void UndoMovement();
    Rectangle GetCollision();

protected:
    Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};
    Texture2D idle{LoadTexture("characters/knight_idle_spritesheet.png")};
    Texture2D run{LoadTexture("characters/knight_run_spritesheet.png")};
    Vector2 worldPos{};
    Vector2 screenPos{};
    Vector2 worldPosLastFrame{};

    // used to tell what direction character is facing
    float flip = 1.0f;

    // Animation variables
    float runningTime{};
    float updateTime{1.0f / 12.0f};
    int frame{};
    int maxFrames{6};
    float speed = 4.0f;
    float width{};
    float height{};
    float scale{4.0f};

private:

};

#endif

Enemy.cpp

#include "Enemy.h"
#include "raymath.h"

Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture)
{
    worldPos = pos;
    idle = idle_texture;
    run = run_texture;
    width = texture.width / maxFrames;
    height = texture.height;
}

void Enemy::Tick(float deltaTime)
{
    worldPosLastFrame = worldPos;

    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        runningTime = 0.0f;
        frame++;
        if (frame > maxFrames)
        {
            frame = 0;
        }
    }

    // Draw Character
    Rectangle textureSource
    {
        frame * width,
        0.0f,
        flip * width,
        height
    };

    Rectangle textureDest
    {
        screenPos.x,
        screenPos.y,
        scale * width,
        scale * height
    };

    Vector2 textureOrigin{};

    DrawTexturePro(texture, textureSource, textureDest, textureOrigin, 0.0f, WHITE);
}

No errors, maybe its something I’m missing

1 Like

I am more of a unity guy, so take this with a grain of salt, remember you can always ctrl+z to undo if my answer doesn’t work:

Anyways now to the answer, it seems as if the issue is within your Enemy script, within the constructor of the enemy class more specifically.

Within this constructor you are assigning the idle_texture and the run_texture to idle and run respectively, all is good here. However I believe the problem lies when you are still using the texture member variable(the one inherited from BaseCharacter) to calculate the width and height variables.

A possible solution to this would to simply use the idle_texture and run_texture instead of the texture in the enemy class.

Here is what a updated Enemy.css would look like:

#include "Enemy.h"
#include "raymath.h"

Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture)
{
    worldPos = pos;
    idle = idle_texture;
    run = run_texture;
    width = idle.width / maxFrames; // Use idle_texture instead of texture
    height = idle.height;
}

void Enemy::Tick(float deltaTime)
{
    worldPosLastFrame = worldPos;

    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        runningTime = 0.0f;
        frame++;
        if (frame > maxFrames)
        {
            frame = 0;
        }
    }

    // Draw Character
    Rectangle textureSource
    {
        frame * width,
        0.0f,
        flip * width,
        height
    };

    Rectangle textureDest
    {
        screenPos.x,
        screenPos.y,
        scale * width,
        scale * height
    };

    Vector2 textureOrigin{};

    DrawTexturePro(idle, textureSource, textureDest, textureOrigin, 0.0f, WHITE); // Use idle instead of texture
}

Edit for small explanation:

With this change, the Enemy class will use the correct textures for the animation instead of using the textures from the BaseCharacter class.

Hey I tried your solution didn’t work unfortunetely.
I think that it not rewriting the BaseCharacter default because it isn’t getting the braced data being provided from main.cpp when we create the enemy goblin, tested this by trying to change the vector2{0.0f, 0.0f} to Vector2{1000.0f, 1000.0f} and the goblin character doesn’t move position.

 Enemy goblin
    {
        Vector2{1000.0f, 1000.0f},
        LoadTexture("characters/goblin_idle_spritesheet.png"),
        LoadTexture("characters/goblin_run_spritesheet.png")
        };

tested some more in debug mode and worldPos is being updated so textures should be as well
image

1 Like

Ok, maybe update enemy.css to this:

#include "Enemy.h"
#include "raymath.h"

Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture)
{
    worldPos = pos;
    idle = idle_texture;
    run = run_texture;
    width = idle.width / maxFrames;
    height = idle.height;
    screenPos = worldPos; // Set screenPos to worldPos initially
}

void Enemy::Tick(float deltaTime)
{
    worldPosLastFrame = worldPos;

    runningTime += deltaTime;
    if (runningTime >= updateTime)
    {
        runningTime = 0.0f;
        frame++;
        if (frame > maxFrames)
        {
            frame = 0;
        }
    }

    // Update screenPos based on worldPos
    screenPos = Vector2Subtract(worldPos, Vector2{ width / 2.0f, height });

    // Draw Character
    Rectangle textureSource
    {
        frame * width,
        0.0f,
        flip * width,
        height
    };

    Rectangle textureDest
    {
        screenPos.x,
        screenPos.y,
        scale * width,
        scale * height
    };

    Vector2 textureOrigin{};

    DrawTexturePro(idle, textureSource, textureDest, textureOrigin, 0.0f, WHITE);
}

When it comes to the enemy’s position on the screen, that’ll come a few lectures later (Enemy Screen Position) so I wouldn’t worry about it right now. The reason it doesn’t work right now is because you’re setting worldPos and not screenPos.

As for the texture, you’re using the base texture variable which does not get overwritten in the constructor for the Enemy.

But this lecture also leaves some things unfinished which makes things look broken when it actually gets addressed later. Some things need to be overrided first.

If you want me to take a deeper look, you can always submit your project using this form

2 Likes

I should have let you handle this in the first place :sweat_smile: Unreal definitely isn’t my thing…

To be fair, this isn’t Unreal either :stuck_out_tongue:

And there’s nothing wrong with giving it a try! I actually like when students help each other, you all learn faster that way.

How do you think I got this job?! Haha

1 Like

Oh lol, I automatically assumed c++ was unreal for some reason, I usually try to help out but I only really do unity, so I should realize what my skill set is compromised of, that being said, I usually try to stay active in the community, so I have been learning a lot that way😁

I’ll just keep going then thanks for the help

Thanks for trying sometimes a second pair of eyes helps

1 Like

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

Privacy & Terms