[SupaSOLVED]Newbie Text Wrap

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
kcilds
Prole
Posts: 4
Joined: Tue Feb 10, 2015 6:40 pm

[SupaSOLVED]Newbie Text Wrap

Post by kcilds »

Hi guys,
I've been holding my self back on writing this topic... but i didn't found anything that helped me (in Google and Forum Search).
Probably it already exists somewhere but i didn't managed to find it.

So.. i'm learning Lua and LÖVE and to acomplish that im working on the simplest kind of game first. The aim for the future of the game is damn high but the first low goal is to be an simple Q&A Bot.
By now i'm developing the UI.

So i managed to do this with my noob (artist that dont know an programmer) skills.
Image




but my code couldn't handle this


Image


interesting that it handles if i just type "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" to the end, but multiple letters it does not.


here it is.. my lame code :P don't hit me pls.
(i already took a look at game states and understand the logic, but im stil in my first steps into programming the game, so this code will adapt as i add more features and etc)

[edit]
Apparently i forgot to ask what i want heuheue sorry

i want an text box (not realy visible box) where the player is limited to 3 lines and the limit is about 500px width.

AND MOST IMPORTANT: I want to learn

i dont want anything done... I just want some path walk in.. because i couldn't find an tutorial and the UI library is confusing, since it have a lot of things that i don't want (so i can't find where exactly the thing happens).

[edit]

Code: Select all

function love.load()
	love.success = love.window.setMode(800, 600, {borderless = true, centered = true})
	pc_init = ":<>:"
	player_console = ""
end

function love.update(dt)
	pc_len = string.len(player_console)
end

function love.textinput(t)
	player_console = player_console .. t
	if pc_len == 90 then
		player_console = player_console .. " "
	elseif pc_len == 180 then
	    player_console = player_console .. " "
	elseif pc_len == 270 then	    
		love.keyboard.setTextInput(false)
	elseif pc_len <= 269 then
	    love.keyboard.setTextInput(true)
	end
end

function love.draw()
	love.graphics.print(pc_init, 20, 500)
    love.graphics.printf(player_console, 50, 500, 500, Right)
end

function love.keypressed(k)
	if k == "escape" then
		love.event.quit()
	elseif k == "backspace" then
	    player_console = string.sub(player_console,1,-2)
	end
end
Last edited by kcilds on Thu Feb 12, 2015 2:59 pm, edited 1 time in total.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: Newbie Text Wrap

Post by Muris »

Well you could ask from the font the width of the string. then determine if the width would be more than 500 with added character, and just add "\n" before the character.
https://love2d.org/wiki/Font:getWidth
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Newbie Text Wrap

Post by davisdude »

The problem with your text wrap is a simple one if I understand it correctly.
Your problem lies in love.textinput: it checks if the character limit is at EXACTLY 90 characters. When you're pressing a lot of keys like that, it's likely that more than one key will be pressed per frame, causing it to entirely skip that. Just try it for yourself: if you type in the text slowly you'll notice that it doesn't miss it. You'll want to check if the current line is >= a certain length.

If you want some more help, feel free to ask! :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: Newbie Text Wrap

Post by Muris »

davisdude wrote:The problem with your text wrap is a simple one if I understand it correctly.
Your problem lies in love.textinput: it checks if the character limit is at EXACTLY 90 characters. When you're pressing a lot of keys like that, it's likely that more than one key will be pressed per frame, causing it to entirely skip that. Just try it for yourself: if you type in the text slowly you'll notice that it doesn't miss it. You'll want to check if the current line is >= a certain length.

If you want some more help, feel free to ask! :)
Also I think it would make more sense to update the text lenght inside the input-thing, instead of inside update.
User avatar
kcilds
Prole
Posts: 4
Joined: Tue Feb 10, 2015 6:40 pm

Re: Newbie Text Wrap

Post by kcilds »

ENLIGHTENMENT is the word that defines your two simple answers! huehuehue

I'm VERY thankful! (i was affraid of getting one go to google or go to UI lib)

Muris just gave me the logic that i was missing![and the \n that i wasn't reminding... some day i explain why(just at the end of this reply)]
(thats all we need in programming understand logic than just run through books to find the right command line)

and you, my friend davisdude, you gave me an answer to something that i condemned as "oh.. this is witchcraft.. doomed witches"!
now that you said .. i tested and its that... simple like that.. and probably that is an codeway to amplify the power of keypress to it get more than one key as i hit the keyboard with my head!

and the Width way is a better way (there's never a right way... just a common one) then set number of char!

So thank you my löves and sorry for the long texts! (i really get excited with discoverys and when you are blind and someone saves you)


A Brief Sad Story

Code: Select all

I stalk this forums and the engine since 2012 or even 2011, not sure).
I fell into the "I will make a really amazing openworld rpg that you can live as an shopkeeper that the most dangerous fight was against the cockroaches in the store .. or be an hero of all the lands". So.. i started my languages adventure:
from ruby (rpg maker) to Blitz3D,to ,ruby again, to C#, to C++, to Lua, to C# back again(this time Unity), to think about modding ToME and finally the surrender to my dumbness that i would neve be able to program since im an designer.
Then... back to Steam days ago I saw an game made with Löve and thought that i was just doing it wrong!

So if you read this.. don't fall like me and understand that the articles on devblogs saying that you have to fly low to then fly high.. is absolutely true!

So here i am ... lost years that i could have used to master Lua (an awesome language and made here in my country(Lua == Moon  :3 )) trying to find an easy to do engine.
So now im focused, but it was a long time that i first read the Lua book... so have to remember everything, and avoid to get cofused with other languages (i confused the ~= with != (dont even remember in what lang the "not equal" is like this))
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Newbie Text Wrap

Post by davisdude »

No problem helping! If you go with the newline part (using \n), you can get the length of the current line with some string "magic" and shorten your code to just one if-statement instead of 3/more. The code below is really small, so you can try to implement it if you want first, and then look at it if you need to.
instead of using pc_len you can use:
current_length = string.len( string.match( player_console, '^.*\n(.-)$' ) or player_console )

and then in the love.textinput do:

if current_length >= 90 then ...
And save lots of code!


Edit: also, I agree with Muris- you should update the length inside of love.textinput
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest