Textboxes and talking to NPCs

I am completely lost as to how I am going to go about a system for talking to NPCs and displaying text boxes. I currently have it set up to listen for if the player is at one of the four tiles around an NPC and if a certain key is pressed but I’m not sure how to get it to recognize that key ONLY ONCE and execute the code to display the textbox only once as well.

Also, as far as getting a textbox on the screen, what is the best way to display text? I have a sprite for a textbox that is 1116 pixels long and 716 pixels high and I want to display text within it. The only way I can think of to get text on the screen is have a spritesheet of characters and display them in each of the slots the textbox is able to hold. Is there an easier way to do this?

So what I’d like to know is:

  1. How can I get a keypress to only be recognized once and make a boolean true once to make the textbox render to the screen?
  2. Is the method I mentioned very efficient for displaying the dialogue or is there an easier/more efficient way?

The src!

  1. Thats pretty much how you draw text… The only other way is to create images with the complete text in it but that is just more difficult.
  2. Check if there is already a text box on the screen (maybe have some sort of variable stating what textbox is on the screen, or null if there isnt any)… if there isn’t a textbox on the screen, setup the textbox which needs to be set up…

Hope this helps

Hello dear Cosmic Kid :slight_smile:

I’ll try to answer your question, with the simplest methods that i know of :slight_smile:

First, (in case you are using LWJGL 2) the keyboard control part, i.e, recognizing if a button is pressed once:

First i recommend you to read through this doc of the LWJGL Keyboard stuff, if you want :slight_smile:
Link: http://wiki.lwjgl.org/index.php?title=LWJGL_Basics_2_(Input)

This is how you recognize if a key is pressed once, i.e, if the key is ‘pressed’ or ‘released’

Here i quote the LWJGL 2 wiki for better understanding:

For example the Keyboard buffer can be scrolled through using the following:


while (Keyboard.next()) {
    // get event key here
}

The Keyboard.getEventKey() method will return which key generated the event.


if (Keyboard.getEventKey() == Keyboard.KEY_A) {
    System.out.println("A Key Event");
}

Now the most important bit!
The Keyboard.getEventState() method will return true if the event key was pressed or false if it was released.


if (Keyboard.getEventKey() == Keyboard.KEY_A) {
    if (Keyboard.getEventKeyState()) {
        System.out.println("A Key Pressed");
    }
    else {
        System.out.println("A Key Released");
    }
}

So you could us it like the following where you put your controls:


while(Keyboard.next()){
	if(Keyboard.getEventKey() == Keyboard.KEY_A){
		if(Keyboard.getEventKeyState() == true)/*Key is pressed once*/;
		if(Keyboard.getEventKeyState() == false)/*Key is released*/);
	}
}

Thats is it for the raw input, but i advise you to write/use a class, that scans through the keyboard ‘event buffer’ BEFORE updating the rest of your game.
You could say, ‘record’ the input and then ask the input manager something like: InputManager.isKeyPressed(Keyboard.KEY_A) and this would return a boolean, if they has been pressed once, released, or is held. (If you want advice on that, let me know and i’ll try to help you with that, it is WAY easier, one method call and you get your input boolean the way you want :))

Now for the text rendering:
For normal text, displaying it as quads is certainly fine (if theres not hundreds and thousands of characters, even then it shouldnt be a problem).
But i would advise you to use and existing library or class from somebody else, text and OpenGL is a nightmare.

You can use the slick util:
Download here: http://slick.ninjacave.com/slick-util/
Tutorial how to use it: http://ninjacave.com/slickutil3

The forum is full of made up classes from users for text rendering, you have to search a little to see what you like, and how you implement it.

I i missed something or you have more question, tell me, and i’ll try to guide you through the jungle, at least of what i know :slight_smile:

Greetings and all the best,
JJ

  1. I’m not the one asking the question… just to let u know.
  2. @Cosmic Kid, what library r u using?
  3. That LWJGL tutorial is out of date…

(gotta love lists :wink: )

  1. Fixed: I misread the names somehow :), still have to get used to the whole forum thing.
    2)…
  2. What do you mean by out of date? If you are talking about LWJGL 3 then you maybe right, i havent looked into that yet.
    I use the latest LWJGL 2 (Build 2.9.1) and it works fine with the code i posted.
    I am not yet going LWJGL 3 :stuck_out_tongue:

Greetings and all the best , JJ

Sorry for not getting back sooner; I’ve been really busy lately. However, I appreciate all your guys’ help! I’ve got the textbox rendering figured out and I’m about to try your suggestions for the keyboard input.

Also, I have LWJGL and slick2D set up on the project, but because I was originally following a tutorial series, the game is in pure Java minus the music system. For the music player I used openAL.

The more you use the stuff the better you’ll know your way around it :slight_smile:

Look at this site for basic LWJGL stuff: http://ninjacave.com/tutorials
Even if some things might not be the cutting edge methods of doing things, but, they still work.(at least for me :))

The more you use it the easier it gets :slight_smile:

And here’s a link concerning all different kinds of techniques, for programming ‘architecture’, that will help you in all sorts of situations:
http://gameprogrammingpatterns.com/contents.html

All the best and keep it up :slight_smile:
Greetings, JJ