With the game that i’m currently working on, once you discover something, you have the ability to name it. I have my custom typeable text boxes working, but I don’t know how to incorporate that into the Objects that I’m creating at different times as their names. How can this be accomplished? I’m really looking for a concept here on how to do this. Any help is appreciated.
[quote]how to incorporate that into the Objects that I’m creating at different times as their names
[/quote]
Doesn’t really clear here. Reflection?
It’s exactly clear what the problem is. Are you having trouble getting the value in your text box into an object property? Are you trying to figure out how to store the name so that it can be used later when an instance of that object comes up?
Yeah, sorry if that’s all a bit confusing. I need to figure out how to get the info from that prompt into the specific Object that i’ve created once the user hits enter.
I hope that this diagram helps
So like you type “human” and all instance of Human objects show up?
Not quite, I’d have a new object that I want to name, and on Initialization, the consructor requires a String for a name, how do i go about taking that info from the prompt and sticking it as one of the parameters?
prepare a field like name:String in your object class, then
public youObject(String name){ this.name = name; }
...
String newName = //read the input
new yourObject(newName);
Wrong again? correct me. Posting is free.
You should probably post code that demonstrates the problem you’re having, otherwise about the only useful thing we can say is ‘pass the name as a parameter to the object’.
I don’t have any code to show for this part, because i had no idea where to start.
The problem with this is that I create the object before i get the name, so it can be shown to the player for them to name.
But, if I remove the name from the constructor, and create the object. Then set the name, what would be the best way for it to remember the object at hand?
Post whatever you’ve got. We’re not mind readers here.
Again, we can only offer simple advice with limited info. Want to ‘remember’ an object? Store it as a member variable in your gui class.
Why not just do a simple object.setName(“myObjectName”) after you’ve created it?
Or you could store the settings or whatever as variables, and instantiate the object when you hit Enter after choosing the name.
Sorry if I’m not seeing exactly what the problem is here…kinda hard with no code
Create the object with a blank name, set it again when they enter a name in the GUI. This is GUI 101 here, any swing tutorial that covers a textbox will cover that. Since it sounds like you’ve got your own widgets going, the only one who knows how those work is you.
If the object amount is small, you can use HashMap<String, YourObject> or HasMap<String, List> if the name isn’t unique.
Well, this is my GuiManager class.
package com.toastedpixel.gui;
import java.awt.Graphics2D;
import java.util.ArrayList;
public class GuiManager {
public enum guiState {
NONE,
TYPE,
MENU;
}
public guiState currentState = guiState.TYPE;
public String typeString = "";
public ArrayList<TextBox> textBoxes = new ArrayList<TextBox>();
public TypableTextBox typeBox = new TypableTextBox(0,0);
public GuiManager(){
textBoxes.add(new TextBox(2,2));
}
public void addTextToNewestBox(String text){
if (!(textBoxes.get(textBoxes.size() - 1) == null))
textBoxes.get(textBoxes.size() - 1).addLine(text, false);
}
public void drawAll(Graphics2D g){
for (TextBox e:textBoxes)
e.Draw(g);
if (currentState == guiState.TYPE);
typeBox.Draw(g);
}
public void createNewtextBox(){
TextBox t = new TextBox(0,0);
t.placeBelow(textBoxes.get(textBoxes.size() - 1));
textBoxes.add(t);
}
public void createNewTextBoxAt(int x, int y){
textBoxes.add(new TextBox(x, y));
}
public void putTypeBoxAt(int x, int y){
typeBox.x = x;
typeBox.y = y;
}
public void createTextBoxFrom(String[] args,int x ,int y){
TextBox t = new TextBox(x,y);
for (String e: args){
if (e != null)
t.addLine(e, false);
}
textBoxes.add(t);
}
public void clearAllBoxes(){
textBoxes.clear();
typeBox.clearLastLine();
}
public void tickAll(long deltaTime){
for(TextBox e:textBoxes)
e.tick(deltaTime);
if(currentState == guiState.TYPE)
typeBox.tick(deltaTime);
}
}
And this is my Typable text box class.
package com.toastedpixel.gui;
import java.awt.Graphics2D;
import com.toastedpixel.andromeda.Display;
public class TypableTextBox extends TextBox{
boolean hasFocus = true;
public Line text = new Line(":", true);
public String finalText;
public TypableTextBox(int x, int y) {
super(x, y);
}
public void tick(long deltaTime){
text.tick(deltaTime);
if (hasFocus){
if(!(Display.GUI.typeString == null))
text.setText(":" + Display.GUI.typeString);
if(Display.input.isKeyDown(Display.input.ENTER) && text.getText().length() != 2);
finalText = text.getText().substring(1, text.getText().length() - 1);
}
}
public int getWidth(){
return text.getText().length() * 6 + 10;
}
public void Draw(Graphics2D g){
g.drawImage(Font.getTextBG(),x, y, getWidth(), 9,null);
int x1 = x + 2;
int y1 = y+2;
Font.RenderText(g, text.getText(), x1, y1);
}
}

Create the object with a blank name, set it again when they enter a name in the GUI. This is GUI 101 here, any swing tutorial that covers a textbox will cover that. Since it sounds like you’ve got your own widgets going, the only one who knows how those work is you.
Yeah, I haven’t taken any classes on java, nor have I practiced a lot int Swing. But, yeah these are custom widgets
Yeah, like sproingie said. Create the object with a blank name-string (remove it from the constructor).
Then when the user hits Enter (that closes your box, right?), then before you destroy the box, do:
myNewObject.setName(myBox.getFinalString())
Obviously this is pseudo-code, but you should get the picture.
If you need the constructor to be able to set the name with a parameter for creating other objects, just overload the constructor, like this:
String name = "", information = "";
int type;
public MyObject(String name, String information, int type){
this.name = name;
this.information= information;
this.type= type;
}
public MyObject(String information, int type){
this.information= information;
this.type= type;
}
Ok, so you have a text box but no way of tying in custom logic to the ‘enter’ press. The obvious approach would be to use a listener/observer pattern: http://en.wikipedia.org/wiki/Observer_pattern
In short, whatever created the text box would register a listener and be notified when the user pressed enter. You should probably take a look at how swing handles this (see http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/ActionListener.html ) which should make things clearer.
Reading a few swing tutorials to get a grasp on how it handles this kind of thing would probably be helpful if you’re trying to write your own gui system. Swing may not be perfect, but it gets a lot of the basics right.
So what i’ve gathered from this and a tutorial or two, is that I have to
1:create a listener from the new Object that I want to name
2:create a textbox for that name
3:fire an event when the enter key is pressed
4:have the listener deal with the event
5:the listener would store the text to the object’s name?
if that’s the basic concept, then i have that much(apart from some errors that i don’t understand). Or I’m completely wrong…