Jump to content

Best way to make a pause screen mid game?


lxFirebal69xl
 Share

Recommended Posts

I was wondering how would you guys go about making a pause screen while you play the game, aka, the player presses ESC and a menu shows up, completely stoping everything in the world, then I guess 2 buttons would pop up, "continue" and "exit". Each of them doing exactly what they say, obviously.

 

post-13811-0-06669600-1437997882.png

Link to comment
Share on other sites

Well, I guess you have to add variable in all your scripts that will be true when the game is on pause. For example in enemy script you should check this variable each update and move enemy only if it's false. There is no automatic way to stop time for all entities and scripts.

Or you can stop calling World:Update? Maybe it's a proper way.

Link to comment
Share on other sites

You could do something like this in the main loop of App.lua:

 

In Start()

self.paused = false

 

 

In Loop()

if self.window:KeyHit(Key.P) then
 self.paused = not self.paused

 if self.paused then
   Time:Pause()
 else
   Time:Resume()
 end
end
--Update the app timing
Time:Update()

--Update the world
if self.paused then
 --draw your menu
 --if mouse clicked
   --if mouse is over button
     --do something
else
 self.world:Update()
end

--Render the world
self.world:Render()

 

Basically read up on Time:Pause() and Time:Resume()

Link to comment
Share on other sites

This is my PauseMenu.lua script below that I'm using in my game - I put it on a pivot and make it a child of the player. It also has a restart map option if player is dead. Could probably be improved but it does the job and should be fairly portable.

 

 

--[[
Apply this script to a pivot and make it a child of the player entity
This script uses the Esc key to pause - you will need to remove the Esc key check from App.lua
In App.lua - App:Start() you need to have self.exitgame = false
--]]
Script.restartMap="start.map"--path "Restart Map" "Map (*.map):map|Maps"

function Script:Start()
self.screenWidth = App.context:GetWidth()
self.screenHeight = App.context:GetHeight()
self.fontsize = 24
self.font = Font:Load("Fonts/arial.ttf",self.fontsize)
App.context:SetFont(self.font)

self.resumeTxt = "Resume"
self.resumeTxtWidth = self.font:GetTextWidth(self.resumeTxt)
self.resumeTxtPos = Vec2((self.screenWidth/2)-(self.resumeTxtWidth/2),(self.screenHeight/2)-60)

self.restartTxt = "Restart"
self.restartTxtWidth = self.font:GetTextWidth(self.restartTxt)
self.restartTxtPos = Vec2((self.screenWidth/2)-(self.restartTxtWidth/2),(self.screenHeight/2)-60)

self.exitTxt = "Exit"
self.exitTxtWidth = self.font:GetTextWidth(self.exitTxt)
self.exitTxtPos = Vec2((self.screenWidth/2)-(self.exitTxtWidth/2),self.resumeTxtPos.y+(self.fontsize*3))
self.gamePaused = 0

self.player = self.entity:GetParent()

end
function Script:UpdateWorld()

if App.window:KeyHit(Key.Escape) then
App.window:ShowMouse()
self.gamePaused = 1
end
while self.gamePaused >0 do

-- get key presses
if App.window:KeyHit(Key.Escape) then
App.window:HideMouse()
self.gamePaused = 0
end

--Render the world
App.world:Render()

-- get mouse position and clicks
local mousepos = App.window:GetMousePosition()
local LeftMouseClick = App.window:MouseDown(Key.LButton)
local RightMouseClick = App.window:MouseDown(Key.RButton)
if self.player.script.health<=0 then
App.context:SetBlendMode(Blend.Alpha)
App.context:SetColor(0,0,0,0.7)
App.context:DrawRect(0,0,self.screenWidth,self.screenHeight)
App.context:SetFont(self.font)
App.context:SetColor(1,1,1,1)
-- check for mouseover resume
if mousepos.y>=self.restartTxtPos.y and mousepos.y<=self.restartTxtPos.y+self.fontsize then
if mousepos.x>=self.restartTxtPos.x and mousepos.x<=self.restartTxtPos.x+self.restartTxtWidth then
 App.context:SetColor(0,1,0,1)
 -- player leftclicks on item
 if LeftMouseClick==true then
 App.window:HideMouse()
 self.gamePaused = 0
 changemapname=self.restartMap
 end
end
end
App.context:DrawText(self.restartTxt,self.restartTxtPos.x,self.restartTxtPos.y)
else
App.context:SetBlendMode(Blend.Alpha)
App.context:SetColor(0,0,0,0.7)
App.context:DrawRect(0,0,self.screenWidth,self.screenHeight)
App.context:SetFont(self.font)
App.context:SetColor(1,1,1,1)
-- check for mouseover resume
if mousepos.y>=self.resumeTxtPos.y and mousepos.y<=self.resumeTxtPos.y+self.fontsize then
if mousepos.x>=self.resumeTxtPos.x and mousepos.x<=self.resumeTxtPos.x+self.resumeTxtWidth then
 App.context:SetColor(0,1,0,1)
 -- player leftclicks on item
 if LeftMouseClick==true then
 App.window:HideMouse()
 self.gamePaused = 0
 end
end
end
App.context:DrawText(self.resumeTxt,self.resumeTxtPos.x,self.resumeTxtPos.y)
end

App.context:SetColor(1,1,1,1)
-- check for mouseover exit
if mousepos.y>=self.exitTxtPos.y and mousepos.y<=self.exitTxtPos.y+self.fontsize then
if mousepos.x>=self.exitTxtPos.x and mousepos.x<=self.exitTxtPos.x+self.exitTxtWidth then
App.context:SetColor(1,0,0,1)
-- player leftclicks on item
if LeftMouseClick==true then
 self.gamePaused = 0
 App.exitgame = true
end
end
end
App.context:DrawText(self.exitTxt,self.exitTxtPos.x,self.exitTxtPos.y)


if App.window:Closed() then
self.gamePaused = 0
App.exitgame = true
end

--Refresh the screen
App.context:Sync(true)

end

end

 

 

You need to make some small changes in app.lua ..

 

In app:start add

 

self.exitgame = false

 

In App:Loop() ..

 

--If window has been closed, end the program
if self.exitgame or self.window:Closed() then return false end

--Handle map change
if changemapname~=nil then

--Clear all entities
self.world:Clear()

--Load the next map
Time:Pause()
if Map:Load(changemapname)==false then return false end
Time:Resume()

changemapname = nil
end

  • Upvote 1

Check out my games: One More Day / Halloween Pumpkin Run

Link to comment
Share on other sites

I'd say the most logical approach is for your game to persist a finite state machine during runtime, that way you can handle states for menus, death, active playtime, spectating, etc.

 

Combining that approach with the other advice offered above should lead you towards the light. :)

 

 

Cheers,

Tyler

  • Upvote 1

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Due to the amount of trouble people have with this and the frequency it comes up, a built-in solution is coming.

 

So, are you thinking about adding a function to the World/Time class to remove the step of disabling the world from updating in the loop if paused is set to true? Will old methods still work?

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

I'm talking about something else, but I don't want to say anything yet because I'm not 100% sure yet. Regarding the pausing logic, it is simple, just call a function that pauses the time, renders the menu in a loop, then when the player exits the menu resume the time and return from the function.

 

You'll see soon.

  • Upvote 4

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...