Create a message box without using libraries by others

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
carbajosa
Prole
Posts: 15
Joined: Fri Jul 12, 2013 5:17 am

Create a message box without using libraries by others

Post by carbajosa »

Hello everyone! I wanted to ask if it is possible to create a very simple message boxes?

I am creating an event handler for my game and well, I wanted to implement a message handler. Like for example, a player has stepped on an event block and pressed Z, a message box will appear.

I have also created an example of what I wanted to achieve. Because I wanted to make it in sequence. For example, a message has been shown then you pressed Z, the next message will show until the end of the "array of messages" has been reached and then starts to clears everything. (the array and the printing on the game screen)
messagebox.love
Sample
(585 Bytes) Downloaded 245 times
The reason of refusing the use of other existing message box libraries like NAVI is because I wanted to learn how to do it on my own. I tried checking the source code of NAVI but it's quite complicated for a simple game that I am creating. I just wanted to make a message show. And when I learned the base of the code then surely, I can enhance and extend its features. :)

That is all, thank you :)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Create a message box without using libraries by others

Post by micha »

What you need is an array of messages (table with string entries) and a counter that tells you which of the messages to show.

Code: Select all

messages = {'First', 'Second', 'And so on'}
counter = 1
The drawing read the counter and creats a box with the correct message. Something like this:

Code: Select all

showBox(messages[counter])
And each time you press "z" you increase the counter by one.

When the counter is increased you have to check if you are at the end of the messages:

Code: Select all

if counter > #messages then
and then remove the stack of messages from the handler.

Then you should also think about what should happen to the game, when a message is displayed? Should the rest of the game stop? Or only parts? Or nothing? Depending on that you have to integrate it into the love.update() and love.draw() accordingly.
User avatar
pacman
Citizen
Posts: 81
Joined: Thu Mar 14, 2013 4:10 pm

Re: Create a message box without using libraries by others

Post by pacman »

Yesterday I just finished my code for little dialog boxes.
Basically I needed single popup above NPC which I could skip with button press.

I made it using player.dialogState. It is nil if there is noone to talk, "inactive" if I'm close enough to someone (then I'm displaying "TALK" icon) and "active" when the dialog magic is happening.

When you talk to NPC it's filling "dialog" array with elements - it can be plain text, image, a lot of options, X/Y position etc.
It also changes dialogState to "active" so you can't do anything else other that skip dialog.

Code: Select all

if currentElement < #dialog then
  display another one (...)
else
  change dialogState to nil so everything can move again
end
Update and Draw everything only if dialogState is active.
That's the simpliest way I could think off... I guess it's not bad for game with a few "Hello hero!" popups here and there.
But if you really NEED dialogs make it better ;)
carbajosa
Prole
Posts: 15
Joined: Fri Jul 12, 2013 5:17 am

Re: Create a message box without using libraries by others

Post by carbajosa »

But how about clearing the rectangle that I used? (love.graphics.rectangle)
I can't seem to clear it out after showing the message lol
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Create a message box without using libraries by others

Post by micha »

carbajosa wrote:But how about clearing the rectangle that I used? (love.graphics.rectangle)
I can't seem to clear it out after showing the message lol
Since the whole graphics are drawn new each frame, there is no 'clearing' but only 'not drawing'. But I guess that is what you mean.
I'd do it this way. With the stuff I wrote in the post above I could do this:

Code: Select all

if counter <= #messages then
  showMessage(messages[counter])
end
So if the counter gets larger than the number of messages, then nothing is displayed.

Can you please upload a .love-file? The solution to this problem depends on how you implemented everything.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: Create a message box without using libraries by others

Post by RedHot »

With very little fiddling I have managed to allow printing letters one by one by a custom delta.

Lua is such a nice thing

Check it out :)
Attachments
messagebox2.love
Printing letters with delay
(629 Bytes) Downloaded 240 times
Last edited by RedHot on Thu Jul 18, 2013 12:47 pm, edited 1 time in total.
carbajosa
Prole
Posts: 15
Joined: Fri Jul 12, 2013 5:17 am

Re: Create a message box without using libraries by others

Post by carbajosa »

Hi everyone! Thank you for the help, I finally got it!

Here's a demo if you want to see it in my game
game.love
Demo
(54.33 KiB) Downloaded 239 times
Instruction: Step on the event blocks and press "z". It will run the event if you are facing the right direction.

It's a bit fugly and buggy at the moment but I got the idea so the thing I will do now is to improvise and fix all the existing bugs.
(One notable bugs in it is the message container. If the other event has 2 messages and the other one has only 1 message then it will print out the second message from the other event. Also if you kinda move so fast at the edge of the map while pressing Z, you'll get stucked. sometimes.)

Thank you all! :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests