Jump to content

this nullptr exception


Andy90
 Share

Go to solution Solved by Josh,

Recommended Posts

Hello, i have 2 components wich work together. So within 1 component i check some cases if they are true i call a function within a second component. When i call the function everything is fine but when i try to use this i get an nullptr exception. Here are my 2 codes

Component 1

else if (!pick.entity->tags.empty()) {
  for (auto tag : pick.entity->tags) {
    if (tag == "gather_water_task") {
      auto task = entity->GetComponent<GatherWaterTask>();
      task->OnTaskStart(entity, pick.entity, task);
      break;
    }
  } 
}

Component 2

void GatherWaterTask::OnTaskStart(shared_ptr<Entity> source, shared_ptr<Entity> dest, shared_ptr<GatherWaterTask> task) {
    Print("Task Started!");
    auto controler = source->GetComponent<MouseMovementController>();
    controler->destination = dest->GetPosition();
    this->taskStarted = true;
}

and the error
image.thumb.png.1253695549d304de964db6168c80d317.png

image.thumb.png.6f9b36ea7ba4068742813b18a65b6443.png

does anyone has an idee why this happens?

Link to comment
Share on other sites

Here is my class.

Header

#pragma once
#include "UltraEngine.h"
#include "MouseMovementController.h"

using namespace UltraEngine;

class GatherWaterTask : public Component
{
private:
    shared_ptr<Entity> entity;
    
public:
    string description;
    string title;
    long duration;
    long cooldown;
    bool taskStarted;

    GatherWaterTask();

    void OnTaskStart(shared_ptr<Entity> source, shared_ptr<Entity> dest, shared_ptr<GatherWaterTask> task);
    void OnTaskEnd();
    virtual void Start();
    virtual void Update();
    virtual bool Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const LoadFlags flags);
    virtual bool Save(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const SaveFlags flags);
    virtual shared_ptr<Component> Copy();
};

Source

#include "UltraEngine.h"
#include "GatherWaterTask.h"


using namespace UltraEngine;

GatherWaterTask::GatherWaterTask() {
	this->name = "GatherWaterTask"; // here this works
}

void GatherWaterTask::Start() {
    this->entity = GetEntity();
}

void GatherWaterTask::Update() {

}


void GatherWaterTask::OnTaskStart(shared_ptr<Entity> source, shared_ptr<Entity> dest, shared_ptr<GatherWaterTask> task) {
    Print("Task Started!");
    auto controler = source->GetComponent<MouseMovementController>();
    controler->destination = dest->GetPosition();
    this->taskStarted = true; // here this is null
}

void GatherWaterTask::OnTaskEnd() {

}

bool GatherWaterTask::Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const LoadFlags flags)
{
    this->title = properties["title"];
    this->description = properties["description"];
    return true;
}

bool GatherWaterTask::Save(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const SaveFlags flags)
{
    properties["title"] = this->title;
    properties["description"] = this->description;
    return true;
}

shared_ptr<Component> GatherWaterTask::Copy()
{
    return std::make_shared<GatherWaterTask>(*this);
}

 

Link to comment
Share on other sites

18 minutes ago, Andy90 said:

Same error. Well im kinda confused. Normaly it should work or ?

I'm just use class name instead of this to assign a value like this:

    void setTileMap(map<iVec3, shared_ptr<Tile>> tileMap)
    {
        Unit::tileMap = tileMap;
    }

But in your case you don''t need neither. I don't see a reason why you have a null pointer issue here but when you have no overlapping vars with same name you can just do:

 

taskStarted = true; 

 

btw all my components atm are hpp. I will rework them into .h and .cpp later but maybe that a reason? In beta with precompiler for a components .hpp was the only option.

 

 

Link to comment
Share on other sites

More wired things happend. I changed the header to an hpp and removed the c++ source file. After this its not possible anymore to access the other component. The compiler says that "MouseMovementController" is not declarated but i includet the header file for this....

 

#pragma once
#pragma once
#include "UltraEngine.h"
#include "MouseMovementController.hpp"

using namespace UltraEngine;

class CollectWaterTask : public Component
{
public:
    shared_ptr<Entity> entity;
    shared_ptr<Entity> player;
    shared_ptr<Map> scene;

    string description;
    string title;
    long duration;
    long cooldown;
    bool taskStarted;

    CollectWaterTask() {
        this->name = "CollectWaterTask";
    }

    virtual void Start() {
        this->entity = GetEntity();

        for (auto e : this->scene->entities) {
            auto controller = e->GetComponent<MouseMovementController>();
            if (controller != NULL)
            {
                this->player = e;
                break;
            }
        }
    }

    virtual void Update() {

    }

    virtual void OnTaskStart(shared_ptr<Entity> source, shared_ptr<Entity> dest, shared_ptr<CollectWaterTask> task) {
        auto controler = source->GetComponent<MouseMovementController>();
        controler->destination = dest->GetPosition();
        this->taskStarted = true; // here this is null
    }

    virtual void OnTaskEnd() {

    }

    virtual bool Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const LoadFlags flags)
    {
        this->scene = scene;
        this->title = properties["title"];
        this->description = properties["description"];
        return true;
    }

    virtual bool Save(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const SaveFlags flags)
    {
        properties["title"] = this->title;
        properties["description"] = this->description;
        return true;
    }

    virtual shared_ptr<Component> Copy()
    {
        return std::make_shared<CollectWaterTask>(*this);
    }
};

 

Link to comment
Share on other sites

Please upload your project so other people can run it. If "this" is NULL inside a method it means a method is being called on an object that does not exist. I'm not sure how that would be possible, but there is definitely something weird going on in the code.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • Solution

In MouseMovementController.cpp, GetComponent is returning null:

                else if (!pick.entity->tags.empty()) {
                    for (auto tag : pick.entity->tags) {
                        if (tag == "gather_water_task") {
                            auto task = entity->GetComponent<GatherWaterTask>();
                            task->OnTaskStart(entity, pick.entity);// set a breakpoint here and look at the task variable
                            break;
                        }
                    }
                }

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Omg you are right...it has to be the entity from the pick not the entity from the Mouse Controller :D thanks alot. 

----

but if you have the project all ready you could have a look to the navmesh. The offset from the player character and the building from a bigger navmesh. 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...