Hello, I have read the jme3 tutorial section about AppStates and Controls, but I just wanted to ask a few questions to clear up if I am using them in best practice. I’ve studied Java for a few months now and I have a good understanding of the basics, but I’ve only fiddled around with small business-like applications with the Java Swing/FX and a handful of threads. I’m interested in game development as a hobby, and I understand a good chunk in theory, but now there are things like optimization, garbage collection and other topics that I didn’t “need” to know when I made those small business applications. So I just want to check if I got it right.
For example, I’m working on a 3D first person game, let’s say I have a scene, with a room that has a door which leads to a hallway. I want to make a control that handles opening doors in that scene, I would write something like this:
[java]
public class DoorControl extends AbstractControl {
//Fields that store information about collision, "You opened a door" BitmapText that will display using the guiNode, door opening audio etc.
private CollisionResults results;
private Ray ray;
...
@Override
protected void controlUpdate(float tpf) {
//Ray casting code
}
public DoorControl(SimpleApplication app) {
//Constructor to initialize the fields
}
private ActionListener mouseClickListener = new ActionListener() {
//Code that handles door movement in the 3D scene
};
}
[/java]
Now my questions regarding this simple example and in general are:
-
If I recall correctly, when you are finished using an AppState, you detach it and then the cleanup method is called in which I detach nodes etc., I noticed there isn’t a cleanup method in the AbstractControl class, what happens to the field values/objects in the control when I detach the control from a node? Does the garbage collector get them? I should manually clean them up?
-
When I unregister all my listeners, detach all my nodes etc., in the AppState, do I need to assign null values to the fields to tell the GC that they are ready for garbage collection or does the cleanup method do that automatically?
Example, I’m currently doing this:
[java]
@Override
public void cleanup() {
super.cleanup();
this.app.getRootNode().detachChild(someNode);
gunfireSound = null;
worldNode = null;
…
}
[/java]
I’m worried about memory consumption, in other words, memory leaks. I’m sorry if this is a stupid question, but the books I’ve read didn’t give much advice regarding the GC process.
-
Let’s say I have a AudioNode field in the DoorControl that plays a 0.5MB wav file, if I have 4 instances of DoorControl assigned to 4 different spatials in the scene, does that mean there are 4 instances of the audio file loaded in the memory, consuming 2MB in total? Should I create an AppState containing only 1 instance of all audio/dialog/text files and then play them from the individual controls?
-
Is it good practice to use a lot of fields, if any, in the custom Control classes that manage behaviour in my game?
-
What is the best way to communicated between AppStates and Controls? I know how to communicate between the AppStates using the state manager:
[java]
this.app.getStateManager().getState(MyAppState.class).doSomeCustomStuffInThisState();
[/java]
But how does one communicate between an AppState/Control or Control/Control? Should I pass the app object through he Control constructor like in the example above? Or should I use the getControl() accessor to get Control objects from spatials and then access the fields using custom methods in the controls I’m accessing?
Even a short answer will suffice, just a pointer to avoid the pitfalls. As you see, I think I understand the logic behind using AppState and Controls, but I’m feeling as I’m using them incorrectly which will bite me back when I will reach a high in-game model count, a lot of audio etc., I fear that I will get memory leaks and OutOfMemoryExceptions, and that I’m leaving a lot of objects in the memory that aren’t being used anymore which should be properly cleaned up.
Thank you for your time, and I hope I didn’t embarrass myself with these questions. :facepalm: