Total Newb Confused with Simple Test (More Issues)

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.
Ozfer
Prole
Posts: 5
Joined: Tue Jan 17, 2012 10:01 pm

Total Newb Confused with Simple Test (More Issues)

Post 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?
Last edited by Ozfer on Tue Jan 17, 2012 10:55 pm, edited 1 time in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Total Newb Confused with Simple Test

Post 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.
Kurosuke needs beta testers
Ozfer
Prole
Posts: 5
Joined: Tue Jan 17, 2012 10:01 pm

Re: Total Newb Confused with Simple Test

Post by Ozfer »

Thanks :)
Ozfer
Prole
Posts: 5
Joined: Tue Jan 17, 2012 10:01 pm

Re: Total Newb Confused with Simple Test

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

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

Post 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.
Ozfer
Prole
Posts: 5
Joined: Tue Jan 17, 2012 10:01 pm

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

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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.
When I write def I mean function.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

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

Post 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.
Ozfer
Prole
Posts: 5
Joined: Tue Jan 17, 2012 10:01 pm

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

Post by Ozfer »

Thank you so much for that huge post, and everyone else too :). This should be enough to get me started.
Post Reply

Who is online

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