Jump to content

C+ Actors - No way to remove them


reepblue
 Share

Recommended Posts

Hello,

I've noticed when using the C++ actor system that the actor class doesn't automatically get released with the entity.  I've tried releasing it before the entity, but it's still in memory after the entity has been released. Below is example code to demonstrate this behavior. Is this a bug or am I not creating the actor class correctly and it's not working with the engine's reference system? I expect that after the entity is released, it would take the actor with it making the actor pointer null. Please provide insight on how to clear actors properly.

#include "Leadwerks.h"
using namespace Leadwerks;

class MyActor : public Actor
{
public:
	virtual void UpdateWorld()
	{
		entity->Turn(0, Time::GetSpeed(), 0);
	}

	void Test()
	{
		System::Print("Actor still in memory...");
	}
};

int main()
{
	Leadwerks::Window* window = Window::Create();
	Leadwerks::Context* context = Context::Create(window);

	World* world = World::Create();
	Camera* camera = Camera::Create();
	camera->SetRotation(35, 0, 0);
	camera->Move(0, 0, -6);
	Light* light = DirectionalLight::Create();
	light->SetRotation(35, 35, 0);
	light->SetShadowMode(0);

	Model* model = Model::Box();
	model->SetColor(1.0, 0.0, 0.0);
	model->SetPosition(0, 0, 0);
	model->SetShadowMode(0);

	MyActor* actor = new MyActor();
	model->SetActor(actor);

	while (window->Closed() == false)
	{
		if (window->KeyHit(Key::T))
		{
			if (actor != nullptr)
			{
				actor->Test();
			}
			
		}

		if (window->KeyHit(Key::Space))
		{
			if (model != nullptr)
			{
				model->GetActor()->Release();
				model->Release();
				//delete actor; <- This line crashes because Actor is off of Object.
				model = nullptr;
			}
		}

		Leadwerks::Time::Update();
		world->Update();
		world->Render();
		context->DrawStats(0, 0, false);
		context->Sync(true);
	}

	return 0;
}

 

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

I also assumed the actors were being released with entity->Release() but I've never checked.

Maybe the entity only stores a reference to the memory and we need to delete the actor like this?

MyActor* actor = new MyActor();
model->SetActor(actor);

//End of game
delete actor;

 

Link to comment
Share on other sites

Ok, I think I got it down now. Create the actor, set the entity to it, and then null the pointer. You probably don't need to null the actor pointer after asignment, the memory address carries over and gets assigned to entity->actor.

#include "Leadwerks.h"
using namespace Leadwerks;

class MyActor : public Actor
{
public:
	virtual void UpdateWorld()
	{
		if (Window::GetCurrent()->KeyHit(Key::T))
		{
			System::Print("Actor still in memory...");
		}

		entity->Turn(0, Time::GetSpeed(), 0);
	}
};

void SetActor(Entity* p)
{
	MyActor* actor = new MyActor();
	p->SetActor(actor);
	actor = nullptr;
}

int main()
{
	Leadwerks::Window* window = Window::Create();
	Leadwerks::Context* context = Context::Create(window);

	World* world = World::Create();
	Camera* camera = Camera::Create();
	camera->SetRotation(35, 0, 0);
	camera->Move(0, 0, -6);
	Light* light = DirectionalLight::Create();
	light->SetRotation(35, 35, 0);
	light->SetShadowMode(0);

	Model* model = Model::Box();
	model->SetColor(1.0, 0.0, 0.0);
	model->SetPosition(0, 0, 0);
	model->SetShadowMode(0);

	SetActor(model);

	while (window->Closed() == false)
	{
		if (window->KeyHit(Key::Space))
		{
			if (model != nullptr)
			{				
				model->Release();
				model = nullptr;
			}
		}

		Leadwerks::Time::Update();
		world->Update();
		world->Render();
		context->DrawStats(0, 0, false);
		context->Sync(true);
	}

	return 0;
}

 

  • Upvote 1

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

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...