Jump to content

Help with entity processing.


smashthewindow
 Share

Recommended Posts

In the attached file, there is a very simplified system of how I set up my entity system and abstraction.

As you see, if you build the project I've provided and press 0 during in game, the game crashes at ResetFramework() (Whether in debug or release).

 

Below is a small description of how I do things.

 

Actor is a class that modifies an entity in some way. Bombs, traps, characters etc inherits from this class.

CallbackRegistration class registers callbacks to the actors. For the base Actor class, FreeEntityCallbackRegistration is inherited as the Actor C++ object needs to be deleted when the entity is freed.

 

Download & change VC dir & compile, and tell me what you think.

I'm looking for some quick explanation on why the program crashes and some ways I could fix this.

 

Thank you for all those who are trying to help me.

 

EDIT: File attached. :D Forgot to press "Attach This File" button. (Why can't I attach 7z archive? Smaller size... :( )

Blog & Portfolio

 

Current project: moon.chase.star

Link to comment
Share on other sites

Ohh. Sorry guys, I made a small mistake in a line.

In the main loop,

 

if( LE::KeyHit( LE::KEY_0 ) )
{
LE::FreeEntity( mesh );
//LE::ResetFramework();
}

 

that should be

 

 

if( LE::KeyHit( LE::KEY_0 ) )
{
//LE::FreeEntity( mesh );
LE::ResetFramework();
}

This throws an exception at LE::ResetFramework() when in debug mode.

Could anyone check this again?

 

This may seem trivial, but the crash tends to lead to more memory problems when I do stuff that are more complex in the game.

Blog & Portfolio

 

Current project: moon.chase.star

Link to comment
Share on other sites

I do in directly yeah. Like you when a scene is loaded TEntity variables are wrapped in my own classes (like your Actor) classes. Then I keep track of all Actors (or whatever else) and free those when a new scene is loaded. When they get freed they free the TEntity variable(s) that belong to the class.

Link to comment
Share on other sites

If you want to I think you could make a pivot and make it the parent to all entities you load. Then free the pivot and all other entities will be freed also, I think.

 

That's what I've tried with the pivot returned from the LE::LoadScene command but that also causes memory problems (specifically a 1-byte memory shift if located under a debugger). I'm thinking that the garbage collection mechanism doesn't go well with C++, and is just better to free them myself.

Blog & Portfolio

 

Current project: moon.chase.star

Link to comment
Share on other sites

If you want to I think you could make a pivot and make it the parent to all entities you load. Then free the pivot and all other entities will be freed also, I think.

 

It sounds like a bit of a dodgy work around to me. (sorry no offense)

 

I think it's better if you just have a content factory class to keep tabs on all of your entities and free them and create them as needed. That way all the entities themselves are in one place and the other appropriate classes have access to the content factory.

STS - Scarlet Thread Studios

AKA: Engineer Ken

 

Fact: Game Development is hard... very bloody hard.. If you are not prepared to accept that.. Please give up now!

Link to comment
Share on other sites

It sounds like a bit of a dodgy work around to me. (sorry no offense)

 

I think it's better if you just have a content factory class to keep tabs on all of your entities and free them and create them as needed. That way all the entities themselves are in one place and the other appropriate classes have access to the content factory.

 

Yep, I've coded with templates for the fourth time now... Here's my factory :D.

 

class ActorRegistrationBase
{
public:
ActorRegistrationBase() { ActorFactory::getInstance()->registerBuilder(this); }
virtual ~ActorRegistrationBase() { ActorFactory::getInstance()->deregisterBuilder(this); }

virtual int getType() = 0;
virtual Actor* createActor( LE::TEntity entity_ ) = 0;
private:
};

class ActorFactory : public Singleton< ActorFactory >
{
public:
typedef std::map<int,ActorRegistrationBase*> Container;

void registerBuilder(ActorRegistrationBase* item_)
{
items.insert(std::pair<int,ActorRegistrationBase*>(item_->getType(),item_));
}

void deregisterBuilder(ActorRegistrationBase* item_)
{
Container::iterator iter_ = items.find(item_->getType());
items.erase( iter_ );
if(items.empty())
{
destroyInstance();
}
}

Actor* createActor( LE::TEntity entity_, int actor_key_ = 0 )
{
if( actor_key_ == 0 )
{
actor_key_ = ctoi( LE::GetEntityKey(entity_,"actor_key","0" ) );
}

Container::iterator iter_ = items.find( actor_key_ );
if( iter_ != items.end() )
{
return iter_->second->createActor( entity_ );
}
else
{
TRACE("ActorRegistrationBase for actor_key %i doesn't exists.", actor_key_ );
ASSERT(true);
}
}

private:
friend class Singleton<ActorFactory>;

ActorFactory(void);
~ActorFactory(void);

Container items;
};

template<typename T>
class ActorRegistration : public ActorRegistrationBase
{
public:
ActorRegistration( int actor_key_ ) : ActorRegistrationBase(),
key( actor_key_ )
{
}

virtual int getType()
{
return type;
}

virtual Actor* createActor( LE::TEntity entity_ )
{
Actor* actor_ = new T( entity_ );
MemoryMgr::getInstance()->addActor( actor_ );
return actor_;
}

private:
int key;
};

Blog & Portfolio

 

Current project: moon.chase.star

Link to comment
Share on other sites

Dang never realized how difficult it is to read code when there is no white spacing / tabing lol. I assume it copy pasted that way.

 

Looks a lot different to my Content Factory but it looks fine to me. However, if you are loading from .sbx then I guess it makes it more difficult to register entities to the factory.. Unless you either make your own scene loader that parses the .sbx file (which is just a .txt file) and loads things as appropriate use databases..

 

hmm... just realised thats why mine seems so different... it's because my content factories have database functionality and handle the database reading and storing data as well. Makes sense now lol.

STS - Scarlet Thread Studios

AKA: Engineer Ken

 

Fact: Game Development is hard... very bloody hard.. If you are not prepared to accept that.. Please give up now!

Link to comment
Share on other sites

It sounds like a bit of a dodgy work around to me. (sorry no offense)

 

I think it's better if you just have a content factory class to keep tabs on all of your entities and free them and create them as needed. That way all the entities themselves are in one place and the other appropriate classes have access to the content factory.

 

No offense taken as it was just 1 possible way to do it. It's actually the "default" way LE handles it in how the loading of the sandbox works. If you were to use LE's way of doing things you would use Sandbox, load your scene and keep every entity loaded the child of the scene pivot, and then use that pivot to free your scene. However you are correct in that most people won't go that route and will start loading their own stuff which isn't directly related to the sbx file, but it is one way to handle it.

Link to comment
Share on other sites

Looks a lot different to my Content Factory but it looks fine to me. However, if you are loading from .sbx then I guess it makes it more difficult to register entities to the factory.. Unless you either make your own scene loader that parses the .sbx file (which is just a .txt file) and loads things as appropriate use databases..

 

ActorRegistration objects are created once globally at application initialization and it registers the appropriate constructor and the actor_type ID to the factory. I find this was a lot more easy to maintain than adding switch cases to call constructors. (I've actually got this idea from the book Game Engine Gems and Source Engine.)

Edited by smashthewindow

Blog & Portfolio

 

Current project: moon.chase.star

Link to comment
Share on other sites

Dang never realized how difficult it is to read code when there is no white spacing / tabing lol. I assume it copy pasted that way.

Yeah, it's a bug in IP boards. I tested it also on their official support forum, and got the same bug with tabs getting stripped from code tags. They never even bothered to publish my test report, so I think their support is actually less than zero, like a negative number.

I would hire someone to make a completely new board software with Apache+PHP+PostgreSQL, or maybe I should make one myself and sell it for free.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

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