Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Monsieur OUXX on Wed 16/09/2020 18:55:31

Title: No "Delete" on Dictionaries?
Post by: Monsieur OUXX on Wed 16/09/2020 18:55:31
Just to be 100% sure, I'd rather ask.

the lifecycle of a dynamic sprite is :
Code (ags) Select

DynamicSprite* spr = DynamicSprite.Create(...);
...
spr.Delete();



But what about dictionaries?
Code (ags) Select

Dictionary* d= Dictionary.Create(...);
...
d.Delete(); // <-- this function does not exist
Title: Re: No "Delete" on Dictionaries?
Post by: Crimson Wizard on Wed 16/09/2020 19:11:29
It gets deleted as soon as there's no reference to object left, and DynamicSprite too.
Title: Re: No "Delete" on Dictionaries?
Post by: Monsieur OUXX on Thu 17/09/2020 08:16:33
Quote from: Crimson Wizard on Wed 16/09/2020 19:11:29
It gets deleted as soon as there's no reference to object left

Perfect.

Quote from: Crimson Wizard on Wed 16/09/2020 19:11:29
and DynamicSprite too.
Are you saying that DynamicSprite.Delete is more of a relic from the past? It gets called automatically by the garbage collector?
Title: Re: No "Delete" on Dictionaries?
Post by: Gilbert on Thu 17/09/2020 09:43:20
Not completely. .Delete() can still be useful in case for dynamic sprites as they usually occupy a certain amount of memory and you may want to remove them manually sometimes.

For a dynamic sprite to have no reference to it, either a pointer to it is destroyed for being out of scoop or you repurpose it to point to something else, or you assign Null to this pointer (or other scenario, such as the sprite is no longer being referenced by an overlay, etc.).

Sometimes it is not that simple, say, when there are multiple references to a single dynamic sprite. If you don't use the .Delete() function, you have to make sure you trash all these references in order to destroy it, which may be a bit messy sometimes. By using .Delete() you can be sure that the sprite is destroyed immediately and the memory is released, without locating each and every reference (though there is still a danger that you destroy a sprite that is still used somewhere in the game).