Functions and gamestates

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Kevin Tillman
Prole
Posts: 14
Joined: Sun Nov 22, 2009 12:42 am

Functions and gamestates

Post by Kevin Tillman »

Ok I am very very newbish, and I have some more questions for the community....

I am trying to help myself understand the general concept of coding game menus and I have come to a few roadblocks. What I want to do is go into the options and change the volume of the music that is currently playing. I sort of have an idea as to how I am suppose to solve my problem, and I am guessing of multiple ways that this can be accomplished, such as.....

A- I should have a function called changeVolume which will return a value to set the current volume of the music that is playing.
B- Just do a regular userInput to change the value of the volume to up or down.

But if option b is the case, do I just make a variable called chnge_volume so I can use the up and down arrow key to only affect the value of the volume by placing this in its own gamestate? Or are my options A and B completely wrong alltogether?

And one more thing, I am having the feeling that I can accomplish what I have so far with much less code,mainly using a for loop somewhere.
Any suggestions on these issues I am having would be much appreciated.

Code: Select all

function load()

font = love.graphics.newFont(love.default_font, 20)
love.graphics.setFont(font)

img_menu = love.graphics.newImage("game menu.jpg")

music_menu = love.audio.newMusic("memory alloys.ogg")
love.audio.play(music_menu, 0)

sound_ping = love.audio.newSound("beep ping.ogg")


menu = {}
menu[1] = "Start"
menu[2] = "Options"
menu[3] = "High Scores"
menu[4] = "Cheats"
menu[5] = "Exit"

state = "menu"
current = 1
previous = current - 1
end

function update(dt)

end

function draw()

if state == "menu" then
showMenu()
end
if state == "start" then
start()
end
if state == "options" then
options()
end

if state == "high scores" then
highScores()
end

if state == "cheats" then
cheats()
end

if state == "exit" then
exit()
end

end

---------------------------------User Functions---------------------
-- These make it so when the variable "current" changes
-- so does the current menu selection
function keypressed(key)
 
 if key == love.key_up then
   current = current - 1
   love.audio.play(sound_ping)
 end        
 if key == love.key_down then
   current = current + 1 
   love.audio.play(sound_ping)
 end
 
 if key == love.key_q and state == "options" or "high scores" or "cheats" or "exit" then
   state = "menu"
 end

 --This takes us to our given location in the games menu 
 if key == love.key_return and current == 1 then
   state = "start"
 end   
 if key == love.key_return and current == 2 then
   state = "options"
 end     
 if key == love.key_return and current == 3 then
   state = "high scores"
 end     
 if key == love.key_return and current == 4 then
   state = "cheats"
 end     
 if key == love.key_return and current == 5 then
   state = "exit"
 end     
end


function showMenu()
if current == 1 then
love.graphics.draw(menu[1], 340,100)
else love.graphics.draw(menu[1], 240,100)
end

if current == 2 then
love.graphics.draw(menu[2], 340,150)
else love.graphics.draw(menu[2], 240,150)
end

if current == 3 then
love.graphics.draw(menu[3], 340,200)
else love.graphics.draw(menu[3], 240,200)
end

if current == 4 then
love.graphics.draw(menu[4], 340,250)
else love.graphics.draw(menu[4], 240,250)
end

if current == 5 then
love.graphics.draw(menu[5], 340,300)
else love.graphics.draw(menu[5], 240,300)
end

--Makes it so the selection goes from the bottom to top 
if current == 6 then
current = 1
end
if current == 0 then
current = 5
end 
end

function start()
love.graphics.draw(menu[1], 300,300)
end

function options()
love.graphics.draw(menu[2], 300,300)
love.graphics.draw("setvolume: ", 300,350)
love.graphics.draw("set difficulty: ", 300,400)
end

function highScores()
love.graphics.draw(menu[3], 300,300)
end

function cheats()
love.graphics.draw(menu[4], 300,300)
end

function exit()
love.graphics.draw(menu[5], 300,300)
end
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Functions and gamestates

Post by kikito »

Hello Kevin,

You may want to have a look at MiddleClass and MindState. These are my libraries for adding Object Orientation and Stateful Objects to Lua (similarly to what Unreal Engine does)


MiddeClass is explained on the wiki, but I'm afraid I didn't find the time to write the proper tutorials yet for the MindState yet. The best I can offer for now is the PÄSSION engine forum thread. PÄSSION is an engine I'm building on top of MiddleClass and MindState, but you don't have to use it if you don't want to. You can download both files from their code.google.com page (direct link to the code)

What I would do would be creating a "Game" class with several states. Something like this:

Code: Select all

require 'MiddleClass.lua'
require 'MindState.lua'

Game = class('Game', StatefulObject)

function Game:initialize()
  super(self) --good practice to allways do this at the beginig of a MiddleClass initializer
  self:gotoState('menu')
end

function Game:draw() end -- do nothing by default; in case some state doesn't have a draw function, or we set the state to nil.
function Game:keypressed(key) end -- same case as the previous line.

Game:addState('menu')
function Game.states.menu:draw()
  ... --draw whatever you want to draw when the status is "menu"
end
function Game.states.menu:keypressed(key)
  ... --handle here the keypresses when the status is "menu"
      --you can change the state by doing self:gotoState('start')
      --you can get the currently selected state by doing self.currentState.name
end

Game:addState('start')
function Game.states.start:draw()
  ... --draw whatever you want to draw when the status is "start"
end
function Game.states.start:keypressed(key)
  ... --handle here the keypresses when the status is "start"
end

... -- add states with draw and keypressed functions here


function love.load() -- I'm using the LÖVE 0.6 version here

... -- load images, sounds, etc

  -- create the game controller variable
  game = game.new() -- this will call initialize and will set the initial menu

end

function love.draw() -- again, LÖVE 0.6
  game:draw()
end

function love.keypressed(key) -- LÖVE 0.6
  game:keypressed(key)
end
I hope the example is clear enough. If you have any questions, I'm afraid that they will have to wait for a week. In some hours I'll start my holidays and will not be able to connect to the internet in 1 week. But some of the LÖVE chaps might lend you a hand.

Some random comments:
  • in order to maximize the feedback you receive, please try to make it easy for us to test your code. An attached *.love would be more appropiate for the amount of code you wanted to show. You could have added code in the post, but removing the unimportant parts (such as the image/sound loading code) or some states (2 or 3 instead of the whole 6)
  • It is a bit weird that you integers for refering to the "currently selected state" (the current variable). The code would be more readable if you said "if current='menu' then ..." instead of "if current=1 then ..."
  • similarly, why use the global menu variable for displaying the menu messages? Instead of menu[3] you could just write "High Scores". That would remove the need of using a "menu" variable in the first place ... and you would get 6 lines less of code.
Regards, I'll see you in one week!
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Functions and gamestates

Post by Robin »

kikito wrote:
  • similarly, why use the global menu variable for displaying the menu messages? Instead of menu[3] you could just write "High Scores". That would remove the need of using a "menu" variable in the first place ... and you would get 6 lines less of code.
I beg to differ to that last thing. :P The thing is, Kevin isn't using enough tables. Imagine he could just have done:

Code: Select all

function love.update(dt)
    updatefuncs[current](dt)
end

function love.draw()
    drawfuncs[current]()
end
etc.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 242 guests