Page 1 of 2

Total Newb Confused with Simple Test (More Issues)

Posted: Tue Jan 17, 2012 10:07 pm
by Ozfer
Hi,
I am somewhat experienced with lua programming, but totally new to love. I put this in a file as a test-

function love.load()
function love.draw()
test = love.graphics.print("test",10,10)
function love.keypressed(b)
if key == 'b' then
function love.draw()
test = love.graphics.print("test complete",10,10)
end
end
end
end
end


But the keypressed part won't work. Anyone know what's wrong?

Re: Total Newb Confused with Simple Test

Posted: Tue Jan 17, 2012 10:39 pm
by tentus
love.***() functions don't go inside of each other, generally speaking. Look at the structure used in https://love2d.org/wiki/Tutorial:Hamster_Ball to understand how Love works better.

Also, please use [ code ] tags to make your lua easier to read on the forums.

Re: Total Newb Confused with Simple Test

Posted: Tue Jan 17, 2012 10:46 pm
by Ozfer
Thanks :)

Re: Total Newb Confused with Simple Test

Posted: Tue Jan 17, 2012 10:55 pm
by Ozfer

Code: Select all

function love.keypressed(b)
   if key == 'b' then
  love.graphics:newFont("Test", 10,10)
	end
end
I switched to this, but its still not working right. I'm totally shooting in the dark here, because I only used Lua on a program called ROBLOX, and its totally different.

Re: Total Newb Confused with Simple Test (More Issues)

Posted: Tue Jan 17, 2012 10:59 pm
by bartbes
There's several things going wrong here.
First, the call, it's love.graphics.newFont, not love.graphics:newFont.
Then, you're not supposed to call something like that repeatedly, it creates fonts, which are particularly memory-heavy.
The argument to the function is called b, but you later refer to it as key.
You also mean love.graphics.print, newFont is used to create a font, not to output text.
And last, but not least, you can't draw in love.keypressed, you should do that in love.draw.

To do that last bit, you'll probably need to set a global variable that tells your love.draw function whether or not to draw the text.

Oh, and, please don't post twice in rapid succession, if there's multiple things to say, just put them all in one post.

Re: Total Newb Confused with Simple Test (More Issues)

Posted: Tue Jan 17, 2012 11:04 pm
by Ellohir
Check out https://love2d.org/wiki/love.graphics.print https://love2d.org/wiki/love.graphics.newFont and https://love2d.org/wiki/love.graphics.setFont for more info about it.

In general, try checking out the wiki and reading the tutorials there. It can be hard to get the idea of how it works if you don't.

Re: Total Newb Confused with Simple Test (More Issues)

Posted: Tue Jan 17, 2012 11:09 pm
by Ozfer
Ok, thanks, and I'll try to improve my post count :). I hate to ask this, but could you explain how I would go about using this global variable? I know it probably seems like I've never programmed in my life, and I appreciate the help.


EDIT-
Actually, if its not to much trouble, I would appreciate the full script. Then I can dissect it to learn.

Re: Total Newb Confused with Simple Test (More Issues)

Posted: Wed Jan 18, 2012 12:04 am
by kikito
There are several ways. Here is one:

Code: Select all

function love.load()
  showMessage = false -- showMessage is a global variable (it doesn't have "local" in front of it)
end

function love.draw()
  if showMessage then
    love.graphics.print("test",10,10)
  end
end

function love.keypressed(b)
  if key == 'b' then
    showMessage = true
  end
end
Happy dissection, and welcome!

If you want to know more about global and local variables, I explain them in Chapter 0 of my love tile tutorial.

Re: Total Newb Confused with Simple Test (More Issues)

Posted: Wed Jan 18, 2012 12:12 am
by MarekkPie
I'm kind of confused on what you are specifically asking...so I'll just run through using love.graphics.print() :)

There are two ways of dealing with fonts. Either just simply use the pre-loaded font (I think it's Arial - 12pt), in which case you don't need to do any loading with love.graphics.newFont(), or import your own:

Code: Select all

function love.load()
	font = love.graphics.newFont("PATH\TO\FONT", size)
end
Unlike many other programming languages, all declared variables in Lua are implicitly set to global, meaning they can be accessed from anywhere within the code once they have been declared. So this:

Code: Select all

function love.load()
	t = 5
end
Is pretty much the same as this:

Code: Select all

t = 5
function love.load()
end
With that in mind, if you go the route of declaring a new font, that variable is accessible everywhere. However, in order for love to start writing with that font, you must use love.graphics.setFont() (In LOVE 0.7.2, you could cut out the middle man and simply load a new font directly into setFont(), but 0.8.0 [the next release] gets rid of that feature).

So updating our font code:

Code: Select all

function love.load()
	font = love.graphics.newFont("PATH\TO\FONT", size)
	love.graphics.setFont(font)
end
This will now make all calls to love.graphics.print() or love.graphics.printf() use that font.

Now, to display the font, you are needing to draw it onto the screen. In LOVE, all drawing (repeat: ALL DRAWING) must original from the callback love.draw() in some form or fashion. So, when we have some string of text we want to be in the window, we do:

Code: Select all

function love.draw()
	love.graphics.print("Hello, world!", x-coordinate, y-coordinate)
end
However, this would cause "Hello, world!" to be always drawn to the screen, and it seemed as if you wanted to toggle it on or off with a key press. love.keypressed(), like love.draw(), must handle all key pressed events. Additionally, like all the love callbacks, you should only declare it once.

What I do for toggling is this:

Code: Select all

function love.load()
	textToggle = false
end

function love.draw()
	if textToggle then
		love.graphics.print("Hello, world!", 0, 0)
	end
end

local function toggle(bool)		-- A helper function for toggling
	if bool then return false	-- for if you do a lot of toggling
	else return true end		-- Returns false if true and true if false
end

function love.keypressed(k)
	if k == "p" then
		textToggle = toggle(textToggle)
	end
end
If you want to only have the text appear as you are holding down a key, the easiest way would be:

Code: Select all

function love.draw()
	if love.keyboard.isDown("p") then
		love.graphics.print("Hello, world!", 0, 0)
	end
end
Although for some reason I got yelled at by Robin for suggesting that to someone.

Re: Total Newb Confused with Simple Test (More Issues)

Posted: Wed Jan 18, 2012 12:42 am
by Ozfer
Thank you so much for that huge post, and everyone else too :). This should be enough to get me started.