# Java engine from scratch: Deleting objects?

This topic is 2382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, thank you for taking time to look at my topic.

I recently started learning Java and wanted to build a game engine with it. I can handle most of the general programming but there are some basic game-building techniques that aren't very straightforward for me yet.
I started programming games in Game Maker, which has built-in functions for deleting objects and drawing sprites very easily. As it turns out though I have to build those functions from scratch in Java. I don't have any problems with that (allows for greater freedom and functionality) but I would need a bit of help to get me started on this.

So my questions is:
What is the proper way of removing/deleting objects in your game?

In Game Maker, you would just type out a function like "instance_destroy(object_name)" and -poof-, object is gone. In Java, I haven't really been able to find a proper function that does exactly that. I did find some things about 'disposing' things and that 'garbage collector' thing but that does not really make sense to me yet. Is that the way to go? Alternatively, I heard that the proper way to do this is to initially make a batch of objects and while the game is running, assigning (sub-)classes to them later on. If you would want to 'delete' them you just simply exclude them from the drawing phase and such. Sounds strange, but might work.

So what do you think? I would really appreciate any help, thank you! =)

##### Share on other sites
In Java, you don't need to delete variables or objects. When a variable goes out of range, the JVM (Java Virtual Machine) automatically deletes the variable. This deletion is done by the Garbage Collector.

That's a very stripped-down view of the Garbage Collector. Personally, I think you need to learn more about Java before you start making a game engine in it. The Garbage Collector is a big piece of the language, so it sounds like you don't know very much about Java. Maybe I'm wrong, though.

As for "deleting" in-game objects so that they don't show up or interact with the player, you could do something like this:

for (int i = 0; i < numEnemies; i++) { if (enemies.isAlive()) { enemies.update(); } } // Then later in the code for (int i = 0; i < numEnemies; i++) { if (enemies.isAlive()) { enemies.draw(); } }

You would probably also have to move the enemy to something like (-1000, -1000) in a 2D game when it dies, just so it can't accidentally interact with anything it shouldn't.

##### Share on other sites
Hidden
Well, that really depends on what your objects are, and how you mange them.

Normally, a good way to handle objects would be via vectors. Don't be intimidated by the name, it's basically just an array with dynamic resizing.
After you got all your objects sorted there, you'll usually go through your drawing loops and whatnot, so every object on the vector would be affected.

What do you do when you want to delete your item? Just use Vector::Remove(), and once your object is off the list, it should, naturally, not be drawn, or even be part of the game, anymore.

In Java, you don't need to delete variables or objects. When a variable goes out of range, the JVM (Java Virtual Machine) automatically deletes the variable. This deletion is done by the Garbage Collector.

That's a very stripped-down view of the Garbage Collector. Personally, I think you need to learn more about Java before you start making a game engine in it. The Garbage Collector is a big piece of the language, so it sounds like you don't know very much about Java. Maybe I'm wrong, though.

As for "deleting" in-game objects so that they don't show up or interact with the player, you could do something like this:

for (int i = 0; i < numEnemies; i++) { if (enemies.isAlive()) { enemies.update(); } } // Then later in the code for (int i = 0; i < numEnemies; i++) { if (enemies.isAlive()) { enemies.draw(); } }

You would probably also have to move the enemy to something like (-1000, -1000) in a 2D game when it dies, just so it can't accidentally interact with anything it shouldn't.

So it's better to define your objects/creatures through arrays rather than making a new object for each one of them?

And I think that piece of code is exactly what I need, thank you very much!

##### Share on other sites

In Java, you don't need to delete variables or objects.

This is a rather misleading statement that often comes up, and I've seen it cause no end of confusion for new programmers. So a few quick words to hopefully avoid people misunderstanding what it means.

You the programmer still have to 'delete' items, in that you have to be sure you have removed all references to it at the end of its life span. (Or pool it, and reuse it with reinitialized data.)

If you don't remember to remove all references to the specific data when you finish with it then it will remain active, and consume memory. This becomes an issue if you keep creating new instances, as each will continue to exist in memory, which means they are taking up more and more resources. Depending on how often this happens, and how large the data instances are, it may be a very serious issue, or it may be so minor that you never notice you have a design bug.

I have a tool that I know has this problem, but I've never bothered to fix it because it will generated a few KB at worst.
I have also misread a design spec one time and accidentally tried to generated several GB of data per minute.

The garbage collector just cleans up after you, but you still have to keep in mind that it isn't magic. It cleans up 'garbage', and stuff that appears to be in use by the program isn't garbage and therefore won't be touched.

##### Share on other sites
Like Luckless said, the garbage collector is the one who will free the memory used by your object's instance if they aren't used anymore by your application. You cannot force the GC to delete an object. However, you can help it by setting every references of your instance to null, as it will effectively tell the collector that this instance isn't used anymore. Moreover, if you set an object to null which holds references to other instances, then those will be eligible for garbage collection.

To sum it up, in Java, you can't explicitly free memory because of the garbage collector. You can only help him to do its job faster by nulling unused references.

##### Share on other sites
What you'll have, is some data structure (ArrayList, Vector or something) which contains all the objects in your game. Or several such structures for different types of objects. Or a hierarchy of things in different zones etc, or something like that. It depends on the design of the game engine.

If you want to make something "go away", let's say it's a projectile which has reached its maximum range, then you need to delete it from the list of things you're keeping track of.

Except it isn't that simple.

What tends to happen is, sometimes things have references to other "things" which they keep track of. Consider a guided missile which is homing in on an enemy, but before it reaches its target, the enemy gets killed by something else and gets deleted.

But the Java object for the enemy still exists (because there is still a reference to it, in the HomingMissile object). Does the homing missile continue to home in on some non-existent (from the POV of the player) target?

What I normally do, is have some property of all the objects, which says that it's gone. Normally I call this "dead". Or "alive". Anyway, it's a bool which says whether it still exists.

In some cases, things which are notionally dead to the player, could still be alive in the game (does a monster change into a corpse when it dies? Or is it instantly deleted and replaced by a new corpse object? That's your decision.).

So if the "dead" bit is set on an object:

* The object-manager code removes it from all the lists of things in the world which it's part of - not necessarily immediately, but within a few ticks.
* Anything else which is tracking it (for example, the HomingMissile) stops doing so. The guided missile could fly straight, or try to find another target.
* Any logic which happens to be "part way" through iterating the list(s) or structures, can skip any "dead" objects if it finds them. This solves the problem of removing things from a list while iterating it, which can be hairy (in some structures, in some programming languages).

##### Share on other sites
I feel like the original poster is kind of confused between the difference between a game engine and a game. A game engine is not required to make a game and without extensive knowledge of making games you won't be able to make a decent game engine, or so I've been told.

What you should do is learn pointers and be able to delete them all in java to get rid of something from memory.