How to make a restart button? :/

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
ZoNe
Prole
Posts: 16
Joined: Thu Feb 06, 2014 9:32 pm

How to make a restart button? :/

Post by ZoNe »

Hey, i am extremely new to programming in general (3 weeks) and after abit of research i chose .lua as my first language to learn :) so far i love LOVE and have spent every minute spare having a go at learning how to programme using it :)

From a mixture of you-tube tutorials and a fair bit of head scratching and figuring things out from what i had picked up i have managed to get together a very basic game concept not too far from a space invaders sort of genre.

The problem i have become stuck on is...

I have a "menu" gamestate from which you have two button options, play or quit.. both of these work respectively but i want to add a restart button, i gather that if i call love.load() it would reload everything as if i had just loaded up the game from a fresh?? but when i do this it seems to crash the whole game :/

Any light on how to code a restart button would be greatly appreciated (go easy on me though, i am new to this :) haha)

I hope i explained well enough too :) any more info needed just ask :)

thanks in advance,

ZoNe
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to make a restart button? :/

Post by davisdude »

ZoNe wrote:Hey, i am extremely new to programming in general (3 weeks) and after abit of research i chose .lua as my first language to learn :) so far i love LOVE and have spent every minute spare having a go at learning how to programme using it :)
Hello! Welcome to LÖVE! :ultraglee:
ZoNe wrote:Any light on how to code a restart button would be greatly appreciated (go easy on me though, i am new to this :) haha)
I'll try! ;)
ZoNe wrote:I have a "menu" gamestate from which you have two button options, play or quit.. both of these work respectively but i want to add a restart button, i gather that if i call love.load() it would reload everything as if i had just loaded up the game from a fresh?? but when i do this it seems to crash the whole game :/
Okay, so here's how I see it:
Make a start menu state, which resets all variables and stuff. This also brings up options, configurations, what ever.
Anyway, if the person clicks "Restart", just bring them to the start menu! That way, since you're already cleared all previous variables, you don't have to worry about anything like that!

Hope this helps, and welcome to LÖVE! :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
ZoNe
Prole
Posts: 16
Joined: Thu Feb 06, 2014 9:32 pm

Re: How to make a restart button? :/

Post by ZoNe »

hi, and thank you :)

thanks for your response too :)
davisdude wrote:Make a start menu state, which resets all variables and stuff. This also brings up options, configurations, what ever.
Anyway, if the person clicks "Restart", just bring them to the start menu! That way, since you're already cleared all previous variables, you don't have to worry about anything like that!
im not sure how to do this though :/ i have a "menu" gamestate and a "playing gamestate already, if i press esc during gameplay it takes me from the "playing" state to the "menu" state and if i click my already made and working "play/continue" button then it carrys on the game as if it were paused.. thats fine but I'm just unsure how to code the "restart" button that i have created that is in the "menu" gamestate also so that instead of carrying on with the present game it starts a new one :/ i tried...

Code: Select all

function button.click(x,y)
	for i,v in ipairs(button) do
		if x > v.x and
		x < v.x + button_font:getWidth(v.text) and
		y > v.y and
		y < v.y + button_font:getHeight() then
			if v.id == "play" then
				gamestate = "playing"
			end
			if v.id == "quit" then
				love.quit()
			end
			if v.id == "restart" then
				love.load()
				gamestate = "playing"
			end
		end
	end
end
but it seams to crash the game when clicked :/

thanks for your input though much appreciated :)
User avatar
ZoNe
Prole
Posts: 16
Joined: Thu Feb 06, 2014 9:32 pm

Re: How to make a restart button? :/

Post by ZoNe »

aha... did this...

Code: Select all

function button.click(x,y)
	for i,v in ipairs(button) do
		if x > v.x and
		x < v.x + button_font:getWidth(v.text) and
		y > v.y and
		y < v.y + button_font:getHeight() then
			if v.id == "play" then
				gamestate = "playing"
			end
			if v.id == "quit" then
				love.quit()
			end
			if v.id == "restart" then
				background.load()
				player.load()
				enemy.load()
				gamestate = "playing"
			end
		end
	end
end
and it works.. is that sort of what you meant?... i could make a separate function to hold it in then just call that function to keep things tidier in my script like..

function game.restart()
background.load()
player.load()
enemy.load()
end

then just call game.restart() instead?

only problem i have found with this is that any enemy that was left on the screen from the previous game are still there. i understand why but how do i remove them? they are inserted via table but obviously i don't want the command to interrupt the enemy being spawned for the new game?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to make a restart button? :/

Post by davisdude »

That's exactly what I meant! :)
For the enemies, just do

Code: Select all

EnemyTableName = {} 
What I would recommend doing is setting a table where you store all the enemies you make. Then, reset that table.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
ZoNe
Prole
Posts: 16
Joined: Thu Feb 06, 2014 9:32 pm

Re: How to make a restart button? :/

Post by ZoNe »

ahh cool thought you might of meant that after i figured it out :)
For the enemies, just do

Code: Select all

EnemyTableName = {} 
What I would recommend doing is setting a table where you store all the enemies you make. Then, reset that table.
im not sure i know what you mean.. this is how i have spawned them...

Code: Select all

enemy = {}
function enemy.load()
	screenWidth = love.graphics.getWidth()
	enemy.width = 25
	enemy.height = 25
	enemy.friction = 10
	enemy.speed = 50
	enemy.xvel = 0
	enemy.yvel = 0
	enemy.timer = 0
	enemy.timerLim = math.random(1,2)
	enemy.spawnX = math.random(0, screenWidth - enemy.width)

	enemyR = {}
	enemyR[1] = love.graphics.newImage("images/enemy2.png")
	enemyR[2] = love.graphics.newImage("images/enemy1.png")

	enemy.animtimer = 0
	enemy.pic = enemyR[1]
	enemy.pnum = 1
end
--
function enemy.spawn(x,y)
	table.insert(enemy, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
--
function enemy.spawningHandler(dt)
	enemy.timer = enemy.timer + dt
	if enemy.timer > enemy.timerLim then
		enemy.spawn(enemy.spawnX, -enemy.height)
		enemy.timer = 0
		enemy.timerLim = math.random(1,2)
		enemy.spawnX = math.random(0,screenWidth - enemy.width)
	end
end
thanks :)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to make a restart button? :/

Post by davisdude »

In the

Code: Select all

function enemy.spawn(x,y)
   table.insert(enemy, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
Change "enemy" to something like "enemy.spawned", that way you don't reset all of your custom functions and stuff. It also makes it more organized. :monocle:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
ZoNe
Prole
Posts: 16
Joined: Thu Feb 06, 2014 9:32 pm

Re: How to make a restart button? :/

Post by ZoNe »

okay...like this?...

Code: Select all

enemy = {}
function enemy.load()
   screenWidth = love.graphics.getWidth()
   enemy.width = 25
   enemy.height = 25
   enemy.friction = 10
   enemy.speed = 50
   enemy.xvel = 0
   enemy.yvel = 0
   enemy.timer = 0
   enemy.timerLim = math.random(1,2)
   enemy.spawnX = math.random(0, screenWidth - enemy.width)

   enemyR = {}
   enemyR[1] = love.graphics.newImage("images/enemy2.png")
   enemyR[2] = love.graphics.newImage("images/enemy1.png")

   enemy.animtimer = 0
   enemy.pic = enemyR[1]
   enemy.pnum = 1
end
--
function enemy.spawn(x,y)
   table.insert(enemy.spawning, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
--
function enemy.spawningHandler(dt)
   enemy.timer = enemy.timer + dt
   if enemy.timer > enemy.timerLim then
      enemy.spawn(enemy.spawnX, -enemy.height)
      enemy.timer = 0
      enemy.timerLim = math.random(1,2)
      enemy.spawnX = math.random(0,screenWidth - enemy.width)
   end
end
I'm getting errors whichever way i try doing it :/ thanks :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to make a restart button? :/

Post by Robin »

A line enemy.spawning = {} to enemy.load() (although using the past tense, like davisdude suggested, makes more sense here).
Help us help you: attach a .love.
User avatar
ZoNe
Prole
Posts: 16
Joined: Thu Feb 06, 2014 9:32 pm

Re: How to make a restart button? :/

Post by ZoNe »

sorry typo there.. meant "enemy.spawned" :) yeah, I've added...

Code: Select all

enemy = {}
function enemy.load()
	enemy.spawned = {}
	screenWidth = love.graphics.getWidth()
	enemy.width = 25
	enemy.height = 25
	enemy.friction = 10
	enemy.speed = 50
	enemy.xvel = 0
	enemy.yvel = 0
	enemy.timer = 0
	enemy.timerLim = math.random(1,2)
	enemy.spawnX = math.random(0, screenWidth - enemy.width)

	enemyR = {}
	enemyR[1] = love.graphics.newImage("images/enemy1.png")
	enemyR[2] = love.graphics.newImage("images/enemy2.png")

	enemy.animtimer = 0
	enemy.pic = enemyR[1]
	enemy.pnum = 1
end
--
function enemy.spawn(x,y)
	table.insert(enemy.spawned, {x = x, y = y, xvel = enemy.xvel, yvel = enemy.yvel,width = enemy.width, height = enemy.height})
end
but this disables the enemies spawning completely :/ I'm confused haha... i gather what I'm trying to do here is insert tables "enemy" into a separate table "enemy spawned" ?? i must be doing something wrong here... :)
Post Reply

Who is online

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