0

My program keeps crashing once I try to make my custom class use some of its own functions
My custom class looks like this:
Entity.h

#ifndef _ENTITIES
#define _ENTITIES
#include "actual.h"
#include "Manager.h"

struct Position
{
    float x, y, z;
};

class Entity
{

public:

    Entity();
    ~Entity();

    bool Init(Actual*, int);
    Actual* GetActual();
    Position* GetPosition();

    void SetPosition(float, float, float);
    void SetPosition(Position*);

    bool IsSpawned();

    bool Render(Manager*);
    bool Spawn(Position*);

    int GetID();

private:

    bool spawned;
    Actual* actual;
    Position* position;
    int entityID;

};

#endif

Entity.cpp

#include "stdafx.h"
#include "Entity.h"
#include <string>
#include "Debug.h"

Entity::Entity()
{
    spawned = false;
}

Entity::~Entity()
{

}

bool Entity::Init(Actual* base, int id)
{

    if (base->IsLoaded() == false)
    {
        return false;
    }

    std::string::size_type sz;

    entityID = id;
    actual = base;

    return true;

}

Position* Entity::GetPosition()
{
    return position;
}

void Entity::SetPosition(float x, float y, float z)
{
    Position* _P = new Position;
    _P->x = x;
    _P->y = y;
    _P->z = z;
    position = _P;
}

void Entity::SetPosition(Position* newPos)
{
    position = newPos;
}

bool Entity::IsSpawned()
{
    return spawned;
}

bool Entity::Spawn(Position* SpawnPos)
{

    pos = SpawnPos;
    spawned = true;

    return true;

}

int Entity::GetID() // this is the unique id that the entity gets
{
    return entityID;
}

// render the entity (only if spawned)
bool Entity::Render(OtherManager* rm)
{


    // this is where the crash happens
    if (IsSpawned() == true)
    {

        if (actual->HasEntryValue("EntityModel") == false)
            return false;

        model* m;

        rm->GetResource((char*)actual->GetEntryValue("EntityModel", 0).c_str(), m);

        // render code

        return true;

    }

    return false;

}

the Manager.h

#ifndef _MANAGER_
#define _MANAGER_

#include "Actual.h"
#include "Entity.h"
#include "OtherManager.h"
#include <Windows.h>

const int MAX_ENTITIES = 2024;

class Manager
{
public:
    Manager();
    ~Manager();

    bool AddComponent(Actual*, DifVector); // returns Entity

    bool Render(OtherManager*);

private:

    Entity* Entities[MAX_ENTITIES];

    int entity_count;
    int nextEID;

};

#endif

the Manager.cpp

#include "stdafx.h"
#include "Manager.h"
#include "Debug.h"

Manager::Manager()
{

    entity_count = -1;
    nextEID = 0;

}

Manager::~Manager()
{
}

bool Manager::AddComponent(Actual* base, DifVector spawnpos)
{

    bool result;

    entity_count = nextEID;

    Entities[entity_count] = new Entity();
    result = Entities[entity_count]->Init(base, entity_count);
    if (!result)
    {
        return false;
    }

    Position* tempPOS = new Position;

    tempPOS->x = spawnpos.x;
    tempPOS->y = spawnpos.y;
    tempPOS->z = spawnpos.z;

    result = Entities[entity_count]->Spawn(tempPOS);
    if (!result)
    {
        return false;
    }

    nextEID = nextEID + 1;

    return true;

}

bool Manager::Render(OtherManager* rm)
{

    if (entity_count = 0)
        Entities[0]->Render(rm);

    for (int i=0; i=nextEID; i++)
    {

        Entities[i]->Render(rm);

    }

    return true;

}

I have modified the code a bit to not shown all the stuff I dont think has anything to do with the problem.
The way I use the code is:

DifVector spawnposition = SelectionPositionSpawnFromCamera(MainCam);
_Manager->AddComponent(ToSpawn, spawnposition); // ToSpawn is an 'Actual' class that has been preset same goes for the spawnposition

// render function
_Manager->Render(rm_WB);
2
Contributors
2
Replies
9
Views
3 Years
Discussion Span
Last Post by ceelos1974
0

Build it with debugging symbols inserted, and then run it under a debugger. The debugger will show you the line of code it crashes on and allow you to examine the variables to see what the problem is.

0

Found the problem it was because my render loop accidentally rendered an unitialized class.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.