Jump to content

Component GetEntity() is null within Start function.


reepblue
 Share

Go to solution Solved by reepblue,

Recommended Posts

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 PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

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 PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

  • Solution

@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 PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

  • Josh locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...