Andy90 Posted December 24, 2023 Share Posted December 24, 2023 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 does anyone has an idee why this happens? Quote Link to comment Share on other sites More sharing options...
Dreikblack Posted December 24, 2023 Share Posted December 24, 2023 11 minutes ago, Andy90 said: this->taskStarted = true; I'm not sure if there is a any difference but try GatherWaterTask::taskStarted = true; Quote Link to comment Share on other sites More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 Same error. Well im kinda confused. Normaly it should work or ? Quote Link to comment Share on other sites More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 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); } Quote Link to comment Share on other sites More sharing options...
Dreikblack Posted December 24, 2023 Share Posted December 24, 2023 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. Quote Link to comment Share on other sites More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 8 minutes ago, Dreikblack said: taskStarted = true; i allready tryed this as well. Same problemm Quote Link to comment Share on other sites More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 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); } }; Quote Link to comment Share on other sites More sharing options...
Dreikblack Posted December 24, 2023 Share Posted December 24, 2023 Try to remove this include and add: class MouseMovementController; above class CollectWaterTask : public Component Quote Link to comment Share on other sites More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 Ok the last error i managed to fix with geting back to header and source files. Back to the problem with the nullptr Quote Link to comment Share on other sites More sharing options...
Josh Posted December 24, 2023 Share Posted December 24, 2023 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. Quote 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 More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 Ok its uploaded. You can find it here: https://1drv.ms/u/s!Aum5gwXzTrOyvzFp9Q_eF9_0AiZl?e=77digt ----- You need to run the solution and then when the game is loaded left klick on the water well. Quote Link to comment Share on other sites More sharing options...
Solution Josh Posted December 24, 2023 Solution Share Posted December 24, 2023 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; } } } Quote 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 More sharing options...
Andy90 Posted December 24, 2023 Author Share Posted December 24, 2023 Omg you are right...it has to be the entity from the pick not the entity from the Mouse Controller 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.