The "Zilarrezko needs help" post

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
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

The "Zilarrezko needs help" post

Post by Zilarrezko »

Hello, I'm quite new to programming and am heavily learning. I just wanted to make a simple game to help myself learn and am watching a couple tutorials. Though I don't feel I'm learning much just because it's incredibly heard for me to comprehend the tables and arguments systems.

Though onto the problem... while trying to make a simple skeleton menu for a future more advanced one, I did it! But too bad the gameState variable or something isn't working and won't go into the "playing" state. I'm not really sure how to explain it, but I wanted to click "start" and it go to a different colored screen just to show it worked. And it doesn't. No lua errors, just nothing.

oh and btw the playerDraw() (in a different lua file) is just a function that changes the background color to show that it was working, in which it didn't.
Attachments
menu.lua
(1.26 KiB) Downloaded 146 times
main.lua
(903 Bytes) Downloaded 131 times
Last edited by Zilarrezko on Thu Dec 13, 2012 2:20 am, edited 1 time in total.
User avatar
headchant
Party member
Posts: 105
Joined: Fri Sep 03, 2010 12:39 pm
Contact:

Re: Not Changing States

Post by headchant »

Please provide a .love file for your problem, so we see the code in proper context.

(see the Help rules!)
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Not Changing States

Post by Zilarrezko »

you mean this?
Attachments
Game.love
(9.04 KiB) Downloaded 145 times
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Not Changing States

Post by Zilarrezko »

everytime I run it though, it just shows a piggy thing with hearts swirling around it
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Not Changing States

Post by Boolsheet »

There are a few things that went wrong.
  • You used the RAR archive format for the .love, but LÖVE officially only supports the Zip format.
  • Filenames in Zip archives are case sensitive. Rename Conf.lua to conf.lua.
  • You're using the require function in the draw callback, this doesn't seem right at all. In case of a Lua file, require loads and executes it once, saves the first return value which will get returned the next time you would call it with the same module name.
  • You should define the LÖVE callbacks (love.draw, love.mouspressed, ...) only once. The love.draw function in menu.lua overwrites the love.draw from main.lua. You can do it this way, but I think this was not intentional.
Let's clean some things up.

In main.lua, move 'require("menu")' out of love.draw. You could put it besides 'require("player")', but menu.lua relies on globals (exitOn, exitOff, ...) that get set later in love.load. You can resolve that by moving the loading of the images into menu.lua. They're menu buttons after all, right?

Move 'love.graphics.setBackgroundColor(122,122,122)' in love.draw to the draw function of the menu in menu.lua. You would never see the player background color if you keep it there, it would reset it every time.

Instead of 'require("menu")', let's instead call a function menuDraw. We'll rename the love.draw in menu.lua to that.


In menu.lua, rename "local function menu.draw" to "local function drawButton". You can't define a local variable on a table.

Delete love.mousepressed. You already have that in main.lua.

Rename the love.draw function in this file to menuDraw.

It should look somewhat like this now. main.lua:

Code: Select all

require("player")
require("menu")

function love.load()
	medium = love.graphics.newFont(45)

	gameState = "menu"
end

function love.update(dt)
end

function love.draw()
	if gameState == "playing" then
		playerDraw()
	end
	if gameState == "menu" then
		menuDraw()
	end
end

function love.mousepressed(x, y, button)
	if button == "l" then
		for k, v in pairs(menu) do
			local inside = insideBox( x, y, v.x - (v.w/2), v.y - (v.h/2), v.w, v.h)
			if inside then
				if v.action == "play" then
					gameState = "playing"
				elseif v.action == "exit" then
					love.event.quit()
				end
			end
		end
	end
end
menu.lua:

Code: Select all

exitOff = love.graphics.newImage("textures/exitoff.png")
exitOn = love.graphics.newImage("textures/exiton.png")
startOff = love.graphics.newImage("textures/startoff.png")
startOn = love.graphics.newImage("textures/starton.png")

bW = exitOn:getWidth()
bH = exitOn:getHeight()

menu = {
	{imageOn = startOn, imageOff = startOff, x = 400, y = 400 - 128, w = bW, h = bH, action = "play"},
	{imageOn = exitOn, imageOff = exitOff, x = 400, y = 400 + 128, w = bW, h = bH, action = "exit"}
}

local function drawButton(off, on, x, y, w, h, mx, my)
	local inside = insideBox( mx, my, x - (w/2), y - (h/2), w, h)
	
	love.graphics.setColor(255,255,255)
	
	if inside then
		love.graphics.draw( on, x, y, 0, 1, 1, (w/2), (h/2))
	else
		love.graphics.draw( off, x, y, 0, 1, 1, (w/2), (h/2))
	end
end

function menuDraw()
	love.graphics.setBackgroundColor(122,122,122)
	local x = love.mouse.getX()
	local y = love.mouse.getY()
	
	for k,v in pairs(menu) do
		drawButton(v.imageOff, v.imageOn, v.x, v.y, v.w, v.h, x, y)
	end
end

function insideBox( mx, my, x, y, wx, wy)
	if mx > x and mx < x + wx then
		if my > y and my < y + wy then
			return true
		end
	end
	return false
end
Shallow indentations.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Not Changing States

Post by Zilarrezko »

alright, so it works... now I just want to get a few things in my head to remember so this doesn't happen every time I try to smile and jamm buttons...

-all require() functions should be at the beginning of main.lua (ALWAYS?) where require("player") is.

-all "love." stuff should only be in the main.lua and no where else

-I should make a ZIP not a rar when compressing and making things a .love (i've been using the .bat and pathed it to the love.exe to run the program)

are all of these true?

also, just throwing it in there. Do you know anywhere that I can learn, indepth about functions (cause I'm not getting arguments at all (been following tutorials for the coding in my code)) as well as the steel plated table and the various (to me confusing) ways to access the data in those tables along with the argument combo that has me scratching my head like a rodeo clown asked to make sense of COBOL.

thanks for your help Boolsheet, been slaving on the code for a few days trying to make sense of it, not knowing the problem.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Not Changing States

Post by Boolsheet »

Zilarrezko wrote:-all require() functions should be at the beginning of main.lua (ALWAYS?) where require("player") is.
There's no rule to that, it's mostly a design decision. Once you understand how the require function works, it should be more clear where to put it. It's tempting to say that require just loads the content of the Lua file and pastes it into the other, but what actually happens is that the code of the loaded file is turned into a function and then gets called. That gives the file its own scope (local variables stay local to that file) and it can receive and return values like other functions (although the receiving is done with the vararg expression '...').
Zilarrezko wrote:-all "love." stuff should only be in the main.lua and no where else
You mean the callbacks? You can create them anywhere and anytime, you just have to be aware that LÖVE always calls these specific values in the love table. The keys in a table are unique. That means there can only be one love.draw and it can only point to one thing too (the function that handles the drawing in this case).
Zilarrezko wrote:also, just throwing it in there. Do you know anywhere that I can learn, indepth about functions (cause I'm not getting arguments at all (been following tutorials for the coding in my code)) as well as the steel plated table and the various (to me confusing) ways to access the data in those tables along with the argument combo that has me scratching my head like a rodeo clown asked to make sense of COBOL.
Hm. Have you looked at the Lua manual, Programming in Lua, and the Lua users wiki yet?

You can also use Lua's standalone interpreter to learn the basics of Lua without having to worry about how to draw stuff on the screen.
Last edited by Boolsheet on Wed Dec 12, 2012 7:14 am, edited 1 time in total.
Shallow indentations.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Not Changing States

Post by Zilarrezko »

the reading segments like that burn my brain... I can't learn like that.

btw I found another problem, probably on my part. but I have this going on where I just want to simply move my chracter. and it can't simply pull the data from the table and do some simple math every millisecond.

Code: Select all

function love.load()
	medium = love.graphics.newFont(45)
	
	gameState = "menu"
end

function love.update(dt)
	playerMove(dt)
end

function love.draw()
	if gameState == "playing" then
		love.graphics.setBackgroundColor(255,255,255)
		playerDraw()
	end
	if gameState == "menu" then
		menuDraw()
	end
end

that's the draw and update function, and heres the player.lua

Code: Select all

player = {}
player.x = 400
player.y = 400
player.speed = 80
player.health = 3

function playerDraw()
	love.graphics.setColor(255,255,255)
	w = player:getWidth()
	h = player:getHeight()
	love.graphics.draw(player, player.x, player.y, 0, 1, 1, w/2, h/2)
end

function playerMove(dt)
	if love.keyboard.isDown("down") then
		player.y = player.y + player.speed *dt
	end
	if love.keyboard.isDown("up") then
		player.y = player.y - player.speed *dt
	end
	if love.keyboard.isDown("right") then
		player.x = player.x + player.speed *dt
	end
	if love.keyboard.isDown("left") then
		player.x = player.x - player.speed * dt
	end
end
am I missing something here? cause I'm not seeing it...

thanks for your help again, because I'm an idiot.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Not Changing States

Post by Boolsheet »

love.graphics.draw takes a Drawable as the first argument. You seem to be passing a table (player). You probably also meant to call getWidth and getHeight on a image, not on the table.
Shallow indentations.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Not Changing States

Post by Zilarrezko »

holy crap it took me 10 minutes to interpret what you said. Anyways yeah, I see what I did now, I called the player.png "player" and used a table as the same name... this is going to be a loooonnnggg learning curve.

geezus thanks again man. Alright, how do I "solve" this post?
Post Reply

Who is online

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