reepblue Posted September 16, 2023 Share Posted September 16, 2023 GetEntity() isn't valid within the component Start function when the old raw pointer was. Place this Start function in any Component. virtual void Start() { auto self = GetEntity(); Assert(self != NULL, "entity is null!"); } Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Josh Posted September 16, 2023 Share Posted September 16, 2023 How is the component being added? 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...
reepblue Posted September 16, 2023 Author Share Posted September 16, 2023 6 minutes ago, Josh said: How is the component being added? By code. Stage::Stage() { // Menu world and camera. auto sz = GetProgram()->GetFramebufferSize(); menuworld = CreateWorld(); menucamera = CreateCamera(menuworld, PROJECTION_ORTHOGRAPHIC); menucamera->SetPosition(float(sz.x) * 0.5f, float(sz.y) * 0.5f); menucamera->AddComponent<LoadingScreen>(); menucamera->SetHidden(false); // Create the game world. gameworld = CreateWorld(); gameworld->SetLightQuality(GetProgram()->GetSetting(SETTING_LIGHTQUALITY, 1)); // Scene gamescene = NULL; pausestate = false; // Set the menu world as the active world. activeworld = menuworld; } If you call a function after it that uses GetEntity(), it'll be fine. A lot of engine changes broke things on my end. My camera component loaded by the map file works fine with this, virtual void Start() { // Enable sound. GetEntity()->Listen(); Listen(EVENT_PAUSESTATE, GetProgram()); Listen(EVENT_GRAPHICSWINDOW, GetProgram()); GetInput()->CenterCursor(); GetInput()->SetCursorHidden(true); GetInput()->SetActiveSet("InGameControls"); } Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Solution reepblue Posted September 16, 2023 Author Solution Share Posted September 16, 2023 @Josh This is why: shared_ptr<T> AddComponent(const bool start = true) { auto o = GetComponent<T>(); if (o) return o; o = std::make_shared<T>(); std::shared_ptr<Component> c = std::dynamic_pointer_cast<Component>(o); if (c == NULL) RuntimeError("Type must be a Component."); //c->entity = As<Entity>().get(); if (start) c->Start(); m_components.push_back(c); //c->entity = this; c->entityptr = As<Entity>(); auto world = GetWorld(); if (world) { world->entitieswithnewcomponents.insert(As<Entity>()); InternalRecordCollisions(true); } return o; } Assign the entity pointer before calling start! Actually make the start function call the LAST thing it does before returning! Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Recommended Posts