Need Help With Displaying Text on Another State

Hi, I need help making this piece of text work. I am not exactly sure how I am supposed do this.

Here is my code from my MainMenu state:

[java]
package mygame;

import com.jme3.scene.Node;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.font.BitmapText;
import com.jme3.math.ColorRGBA;

public class MainMenu extends AbstractAppState {

private SimpleApplication app;

private Node rootNode; 

private AssetManager assetManager;
private Node guiNode;

@Override
public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    this.app = (SimpleApplication) app;
    
    this.rootNode = this.app.getRootNode();
    this.assetManager = this.app.getAssetManager();
    this.guiNode = this.app.getGuiNode();
    
    BitmapText hudText = new BitmapText(guiFont, false);            // "Cannot find symbol" for guiFont
    hudText.setSize(guiFont.getCharSet().getRenderedSize());    // "Cannot find symbol" for guiFont
    hudText.setColor(ColorRGBA.Blue);                                           
    hudText.setText("You can write any string here");                     
    hudText.setLocalTranslation(300, hudText.getLineHeight(), 0); 
    guiNode.attachChild(hudText);
}

@Override
public void update(float tpf) {
    
}

@Override
public void cleanup() {
    
}    

}
[/java]

I am getting an error at guiFont saying “Cannot find symbol” What am I supposed to do? Any help is appreciated. Thanks

It could mean that your font does not contain the symbol you are trying to write

For example a font with only letts A-Z and 1-9 would not have the # symbol.

This could be a possibility.

I do not understand what you are saying. Its just in a different class. This same code works fine in the Main class.

guiFont is something that exists by default in SimpleApplication because (unfortunately) a long time ago the developers of SimpleApplication opted for convenience instead of clarity or correctness… so now new users end up being more confused instead of less confused because they wonder where these magic words came from and why they don’t work in some other class.

Suffice it to say, guiFont is just a class field like any other… but your class doesn’t have this field so you get an “Cannot find symbol” error because… well… the symbol hasn’t been defined.

Fear not, though, because the magic necessary to get this field turns out to be pretty simple:

BitmapFont guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);

Edit: just an addendum… I don’t mean to sound like I’m ragging on the original author(s) of SimpleApplication. It’s a product of its time and local momentum as much as anything else. I have the benefit of hindsight in my favor. :slight_smile:

1 Like

Thank you very much!

As a secondary option to @pspeed 's answer… you could always set up a get method and pass a reference to your main class so your not creating a new instance of BitmapFont when it already exists. It’s not a huge issue one way or the other, but it makes it easier to modify the font globally if you ever decide to change which font you are using.

@t0neg0d said: As a secondary option to @pspeed 's answer... you could always set up a get method and pass a reference to your main class so your not creating a new instance of BitmapFont when it already exists. It's not a *huge* issue one way or the other, but it makes it easier to modify the font globally if you ever decide to change which font you are using.

Maybe the asset manager should cache these things so that we don’t create a new one every time. Oh, wait… :wink:

But yeah, if you decide you want a different font later than you have a search/replace to do. A getter is a fine solution for that.

Edit: or the font could be passed to the state if it is added in simpleInit() (versus app constructor)… then you could even have a different font for the menu than for everything else if you wanted.