Total Newb Confused with Simple Test (More Issues)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Total Newb Confused with Simple Test (More Issues)
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?
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.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Total Newb Confused with Simple Test
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.
Also, please use [ code ] tags to make your lua easier to read on the forums.
Kurosuke needs beta testers
Re: Total Newb Confused with Simple Test
Code: Select all
function love.keypressed(b)
if key == 'b' then
love.graphics:newFont("Test", 10,10)
end
end
- 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)
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.
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)
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.
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)
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.
EDIT-
Actually, if its not to much trouble, I would appreciate the full script. Then I can dissect it to learn.
- 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)
There are several ways. Here is one:
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.
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
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.
Re: Total Newb Confused with Simple Test (More Issues)
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:
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:
Is pretty much the same as this:
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:
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:
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:
If you want to only have the text appear as you are holding down a key, the easiest way would be:
Although for some reason I got yelled at by Robin for suggesting that to someone.
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
Code: Select all
function love.load()
t = 5
end
Code: Select all
t = 5
function love.load()
end
So updating our font code:
Code: Select all
function love.load()
font = love.graphics.newFont("PATH\TO\FONT", size)
love.graphics.setFont(font)
end
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
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
Code: Select all
function love.draw()
if love.keyboard.isDown("p") then
love.graphics.print("Hello, world!", 0, 0)
end
end
Re: Total Newb Confused with Simple Test (More Issues)
Thank you so much for that huge post, and everyone else too . This should be enough to get me started.
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests