Text Input and Chat Box

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
User avatar
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Text Input and Chat Box

Post by OdnsRvns »

I'm in the beginning stages of making a chat box for my TextBased RPG. I thought I'd get some ideas on how to accomplish this task. Right now my thinking is is put the user text in a variable "Player1['input']" then when user hits enter Game parses the sentence and feeds back an answer and empties the variable out. Right now I'm stuck on how to get the SHIFT/Spacebar and Punctuation to work correctly. This is what my function looks like so far. Before I go to far in building the chat function what are some tips from you guys to help me out. I'm pretty new to programming.

Code: Select all

function love.keypressed( key, isrepeat )
   
	if key == 'escape' then love.event.quit() end
        if Settings['state'] == "PLAYING" and key and key:match( '^[%w%s]$' ) then Player1['input'] = Player1['input']..key end
	if Settings['state'] == "PLAYING" and key == "backspace" then Player1['input'] = Player1['input']:sub(1, -2) end

end


Also feel free to give me some tips in general, I'll take them.
Attachments
Base_003.love
Work in progress
(4.83 MiB) Downloaded 130 times
User avatar
TurtleP
Party member
Posts: 147
Joined: Thu Mar 22, 2012 9:20 pm
Contact:

Re: Text Input and Chat Box

Post by TurtleP »

For something like this, I'd recommend using love.textinput(text). This function avoids anything you wouldn't want such as "backspace" or "escape" being typed in (even though you are excluding them). You'd not even need to worry about uppercase and punctuation since pressing shift + those keys work easily with love.textinput.
User avatar
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Re: Text Input and Chat Box

Post by OdnsRvns »

Worked like a charm I didn't even know this was a love function.

Code: Select all

function love.textinput(t)
   if Settings['state'] == "PLAYING" then Player1['input'] = Player1['input'] .. t end
end
User avatar
DeltaF1
Citizen
Posts: 64
Joined: Mon Apr 27, 2015 4:12 pm
Location: The Bottom of the Stack
Contact:

Re: Text Input and Chat Box

Post by DeltaF1 »

This is looking really pretty!
User avatar
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Re: Text Input and Chat Box

Post by OdnsRvns »

DeltaF1 wrote:This is looking really pretty!
Thanks. I wish my code was half as nice as it looks. I have some awesome ideas for the storyline but its all about me deciding to finish a project for once.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 48 guests