Jump to content

can i find out if an entity was freed?


AnniXa
 Share

Recommended Posts

Hey guy´s,

Another question:

When we have some entitys, and we refer to them from my own list or something, can we anyhow test if FreeEntity() was used on that entity, to nullyfy my refer then afterwards?

 

Maybe some field in the entity that is indicating if the entity was freed or something? =)

 

Greetings

Whuts wroong?

Link to comment
Share on other sites

..you should call garbage collector after FreeEntity, and given example is from Bmax, and it works fine..so im not sure what you using..other way is to have a flag what will determine is your mesh unloaded or not...that will require writing of some Loader/Unloader class, what is a must anyway..so withing that class should be member determining state of given entity, something like 'Loaded:Int'

So, when you load your entity then Loaded flag will be set to true..soon as you use FreeEntity, it will be set to false...on that way you can always know whats going on with entity by accessing its state member from loader class..

 

however, after use of FreeEntity, always Null given handler..so, previous example should look like this

 

MyEntity:TMesh=LoadMesh("abstract::MyMesh.gmf)

FreeEntity(MyEntity)
MyEntity=Null

If MyEntity=Null
 DrawText("YAY, its freeeeeeeed !",0,0)
End if

 

Link to comment
Share on other sites

..you should call garbage collector after FreeEntity, and given example is from Bmax, and it works fine..so im not sure what you using..other way is to have a flag what will determine is your mesh unloaded or not...that will require writing of some Loader/Unloader class, what is a must anyway..so withing that class should be member determining state of given entity, something like 'Loaded:Int'

So, when you load your entity then Loaded flag will be set to true..soon as you use FreeEntity, it will be set to false...on that way you can always know whats going on with entity by accessing its state member from loader class..

 

okay this might help,ty very much.

 

i got it the following now:

 

GCSetMode(2) somewhere at the top, and somewhere in the mainloop GCCollect().

 

but i still can access it, i think the GC will not clear it until i made it =null

 

(same as with mode 1)

 

i think i just try that loaded field :)

Whuts wroong?

Link to comment
Share on other sites

..well..equalizing with null should certanly do a job..after that you should call GCCollect(), not before..

 

MyEntity:TMesh=LoadMesh("abstract::MyMesh.gmf)

FreeEntity(MyEntity)
MyEntity=Null
GCCollect()

If MyEntity=Null
 DrawText("YAY, its freeeeeeeed !",0,0)
End if

 

..what is the reason behind use of GCSetMode(2) ??

 

EDIT:

Also, keep in mind that garbage collector DOESNT flush out things imidiatelly, once command is called...

 

Link to comment
Share on other sites

..well..equalizing with null should certanly do a job..after that you should call GCCollect(), not before..

 

MyEntity:TMesh=LoadMesh("abstract::MyMesh.gmf)

FreeEntity(MyEntity)
MyEntity=Null
GCCollect()

If MyEntity=Null
 DrawText("YAY, its freeeeeeeed !",0,0)
End if

 

..what is the reason behind use of GCSetMode(2) ??

 

EDIT:

Also, keep in mind that garbage collector DOESNT flush out things imidiatelly, once command is called...

 

 

i just tried it like in some example, normaly i use mode 1 =)

 

well i just found out that the ..valid field seems to be set on 0 when freeEntity() is runned, so i use this now to check if the entity was freed

 

I write an "EntityButton" for my gui, some button that can be attached to an entity and wich is updating its position regulary to the entityPos on screen, and since i want a strict split between my gui stuff and my game stuff, i cannot just do myentity= null, because the EntityButton got a field Entity, wich wil not know if i use FreeEntity(), but now i just check on every button position update if entity.valid >0 and remove the button if not. it looks like this works.

Whuts wroong?

Link to comment
Share on other sites

The method I use doesn't seem to give me any problems, but I don't use BlitzMax, so I have no idea how to port it over.

 

Basically, a leadwerks TEntity is part of a class:

 

class Object
{
   TModel LeadwerksModel;
   NewtonBody * NewtonObject;
   void Initialise(std::string DataFile);
   void Initialise(TModel NewModel, NewtonBody * NewBody);
public:
   Object(std::string DataFile){Initialise(DataFile);}
   Object(TModel NewModel, NewtonBody * NewBody){Initialise(NewModel, NewBody);}
   Object(TModel NewModel){Initialise(NewModel, 0);}
   Object(NewtonBody * NewBody){Initialise(0, NewBody);}
   ~Object();

   TModel GetLeadwerksModel(void);
   NewtonBody * GetNewtonBody(void);
};

//Then to load the model...

void Object::Initialise(std::string DataFile)
{
   //By now, we will have read a file to find which models to use;
   if(ModelName.compare("none") == 0)
   {
       LeadwerksModel = 0;
   }
   else
   {
       LeadwerksModel = LoadModel(CStr_ModelName); //That's a char pointer variable, since ModelName.c_str() seems not to work...
   }
}

//To free the entity

Object::~Object()
{
   if(LeadwerksModel != 0)
   {
       FreeEntity(LeadwerksModel);
   }
   if(NewtonObject != 0)
   {
       NewtonDestroyBody(NewtonObject);
   }
}

//For using the entity

TModel Object::GetLeadwerksModel()
{
   return LeadwerksModel;
}

//create, use and delete a model

Object * MyNewObject;
{ //Just for scope limitation
   std::string DataFile = "Models\\MyObject.dat";
   MyNewObject = new Object(DataFile);
} //DataFile now out of scope again

for(float val = 0.0f; val < 360.0f; val += 0.75f)
   TurnEntity(MyNewObject->GetLeadwerksModel(),Vec3(val,val,val));

delete MyNewObject;

//Now, doesn't matter is FreeEntity has worked immediately or not, I can't access that entity again because the object that held a reference to it has been deleted...

 

I presume you'd be able to work something similar in BlitzMax

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

all you need to do in Bmax really is

 

FreeEntity (MyEntity)

MyEntity = Null

GcCollect()

 

..after handler is nulled, thats it..some objects may require FreeEntity, such as meshes, but some DONT, such as sound sources, or sound files...either way, they have to be nulled after FreeEntity is performed (if FreeEntity, apply on to such entities), and thats all..im just not sure, how is possible to have entity handler available with its properities, after is nulled..

 

Link to comment
Share on other sites

all you need to do in Bmax really is

 

FreeEntity (MyEntity)

MyEntity = Null

GcCollect()

 

..after handler is nulled, thats it..some objects may require FreeEntity, such as meshes, but some DONT, such as sound sources, or sound files...either way, they have to be nulled after FreeEntity is performed (if FreeEntity, apply on to such entities), and thats all..im just not sure, how is possible to have entity handler available with its properities, after is nulled..

 

 

the problem in my case is, that i just cant do this:

 

FreeEntity (MyEntity)
MyEntity = Null

 

because there are more than one refers to this entity in my example :

1 is in the type of an "EntityButton" type, a clickable button that is allways located to the entity, and is handled via my gui.

2 is somewhere in the game logic

and since i make a strict seperation between my gui stuff and my game logic stuff (because i want to have the gui for itselfs for later projects), do they have a own refer to the same entity.

when now somewhere in the game logic the entity is freed, then the button type does not take any notice from this, because the field entity of the button was not made to null.

 

so i wanted to make the buttons a pre-test, if the entity that they are dedicated to was free from somewhere (and this works fine for now with the .valid field)

 

... somewhere before:
type tEntityButton
field entity
end type

local myentity:tentity = loadmesh(...)

button:tentitybutton = new tentitybutton
button.entity = myentity

... somewhere in the game loop

FreeEntity(MyEntity)
MyEntity = Null

 

after this

button.entity ist still not null, because it still refers to it.

 

now when the next button position update comes i just do something like

method UpdatePosition()
if self.entity.valid = 0 then
 .... remove the button (because the entity of this button was freed
else
  .... updatding the button pos to the entity on screen
endif
end method

 

this works fine, but i dont know what the .valid field really is for, maybe someone knows?

all i know that freeentity() seems to set it on 0 for any reason.

Whuts wroong?

Link to comment
Share on other sites

..of course button.entity is not null..in fact button.entity is just an instance, pointer on to original entity handler MyEntity, so its NOT nulled by simply freeing MyEntity...entity.valid is true or false based on state of original handler (false if nulled, true if not), while pointer (button.entity) still has value different than Null (pointer address)....this indicates that you haven't structured properly your classes, where none of instances (button.entity) should exist if original handler (MyEntity) is freed..

 

Link to comment
Share on other sites

..of course button.entity is not null..in fact button.entity is just an instance, pointer on to original entity handler MyEntity, so its NOT nulled by simply freeing MyEntity...entity.valid is true or false based on state of original handler (false if nulled, true if not), while pointer (button.entity) still has value different than Null (pointer address)....this indicates that you haven't structured properly your classes, where none of instances (button.entity) should exist if original handler (MyEntity) is freed..

 

Maybe this is a thing of taste, i want a tentity button that just dont need any extra stuff/clases then just an entity where it is attached to.

Because of the gui should be absolutely independent of the rest game process, and just run with leadwerks itself.

 

But however, thank you for the help and your time alot.

Whuts wroong?

Link to comment
Share on other sites

Welcome to the not so wonderful world of garbage collection. :o

 

Remember, it makes programming easier! :)

 

In BlitzMax, you have to call FreeEntity() AND set all variables that refer to the entity to Null, in order to delete it entirely.

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

..ocky-docky :) important is..it works... by the way, what GUI you use ?

 

yea maybe someone knows what the .value field is really for :o

 

I use my own gui, that i wrote by myself in openGL and ported now over to LeadWerks engine (tbh i rewrite it compleply), (check my block if you want to know more :D)

Whuts wroong?

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