Hello, trying to print a text i input

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.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Hello, trying to print a text i input

Post by veethree »

MysticPing2 wrote:Also, since im only using this for a simple tax calculator for a forum, is it OK if i use and alter your code (and distribute it, ofcorse i wont earn money on it :P)

I learn alot from reading it



-- I keep pressing save instead of submit
Yeah i don't mind.
MysticPing2
Prole
Posts: 33
Joined: Tue Dec 10, 2013 7:08 pm

Re: Hello, trying to print a text i input

Post by MysticPing2 »

console works now, not shure how, your fingers must be magic
MysticPing2
Prole
Posts: 33
Joined: Tue Dec 10, 2013 7:08 pm

Re: Hello, trying to print a text i input

Post by MysticPing2 »

I tried adding another table, text_box2 and doing the same variables, and then c+v'ing the other functions and replacing the tex_box with textbox_2, its not reallt working
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Hello, trying to print a text i input

Post by veethree »

MysticPing2 wrote:I tried adding another table, text_box2 and doing the same variables, and then c+v'ing the other functions and replacing the tex_box with textbox_2, its not reallt working
Define "not working". Also if could post the code that would help.
MysticPing2
Prole
Posts: 33
Joined: Tue Dec 10, 2013 7:08 pm

Re: Hello, trying to print a text i input

Post by MysticPing2 »

Basicly it behaves like i didnt edit it at all, exept when i press backspace all text disssapear

Code: Select all

 function love.load()
	text_box = { --Table that holds all the textbox properties
			x = 12, 
			y = 12,
			width = 256, 
			height = 24,
			text = "",
			selected = false
		}

	text_box2 = {
			x = 12, 
			y = 48,
			width = 256, 
			height = 24,
			text = "",
			selected = false
		}
end
I changed that
And for ever other function i created a copy and replaced the names, example:

Code: Select all

function love.draw()
	--Drawing the textbox
	if text_box2.selected then --Settting the color to bright red if the textbox is selected, and dark red if not.
		love.graphics.setColor(255, 0, 0)
	else
		love.graphics.setColor(100, 0, 0)
	end
	love.graphics.rectangle("line", text_box2.x, text_box2.y, text_box2.width, text_box2.height)
	love.graphics.setColor(255, 255, 255)
	love.graphics.print(text_box2.text, text_box2.x + 5, text_box2.y + 5)
end
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Hello, trying to print a text i input

Post by veethree »

There should only be one of each of the callback functions (love.load, love.draw, love.update, love.mousepressed etc.).

So if you have multiple boxes, You draw them all in one love.draw.

like this:

Code: Select all

function love.draw()
   --Drawing the textbox
   if text_box1.selected then --Settting the color to bright red if the textbox is selected, and dark red if not.
      love.graphics.setColor(255, 0, 0)
   else
      love.graphics.setColor(100, 0, 0)
   end
   love.graphics.rectangle("line", text_box1.x, text_box1.y, text_box1.width, text_box1.height)
   love.graphics.setColor(255, 255, 255)
   love.graphics.print(text_box1.text, text_box1.x + 5, text_box1.y + 5)

    if text_box2.selected then 
      love.graphics.setColor(255, 0, 0)
   else
      love.graphics.setColor(100, 0, 0)
   end
   love.graphics.rectangle("line", text_box2.x, text_box2.y, text_box2.width, text_box2.height)
   love.graphics.setColor(255, 255, 255)
   love.graphics.print(text_box2.text, text_box2.x + 5, text_box2.y + 5)
end
Like i already mentioned, This isn't really a proper implementation. Ideally you would store all the textboxes in a table, then to draw them you would loop through that table with a for loop and draw it that way, Would make the code significantly cleaner and shorter.
MysticPing2
Prole
Posts: 33
Joined: Tue Dec 10, 2013 7:08 pm

Re: Hello, trying to print a text i input

Post by MysticPing2 »

thanks, i have to go now and while i might be online at school, i havnt figured out how to run stuff on my mac yet, but well thanks.

As for looping trough would i do all the tables in 1 table and do it like this? Whereas when i wanted to do something i would do



for i=1,#text_boxes do

text_boxes -- would be what i used?

well as i said g2g now, thank you, you are awesome


forgot to mention it would be like

if i > #text_boxes then
i = 1
-- then i would have some kind of wait inbetween updates
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Hello, trying to print a text i input

Post by veethree »

MysticPing2 wrote:thanks, i have to go now and while i might be online at school, i havnt figured out how to run stuff on my mac yet, but well thanks.

As for looping trough would i do all the tables in 1 table and do it like this? Whereas when i wanted to do something i would do



for i=1,#text_boxes do

text_boxes -- would be what i used?

well as i said g2g now, thank you, you are awesome

That would work, but you could use an ipairs loop instead.
MysticPing2
Prole
Posts: 33
Joined: Tue Dec 10, 2013 7:08 pm

Re: Hello, trying to print a text i input

Post by MysticPing2 »

I dont really understand ipairs, though
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Hello, trying to print a text i input

Post by veethree »

MysticPing2 wrote:I dont really understand ipairs, though
It's pretty simple, Say you have a table like this:

Code: Select all

things = {"thing", "another thing", and one more thing"}
Then loop through it with ipairs like so

Code: Select all

for i,v in ipairs(things) do
   --do things here
end
Then in the loop you could do something like:

Code: Select all

love.graphics.print(v, 12, 12)
and it would do the same thing as

Code: Select all

love.graphics.print(things[i], 12, 12)
in a standard numeric loop (for i=1, #things do)

ipairs is design to iterate through tables, So your code will be a bit cleaner, and quicker to type with ipairs as opposed to a numeric loop.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests