Jump to content

Game ending


Mr.Wolfas
 Share

Recommended Posts

Hello, it's me again. I have a question. How to make my game turn off( I want to black screen with letters or short video( I trying to make it) appear and then the game just turn off) then player hit the last switch. Sorry for my english mistakes( I'm not english speaker) hope you will understand what I wanna do.

Link to comment
Share on other sites

A real basic approach would be something like this (pseudo code)

 

while gameIsActive do   
    --Start the ending phase of the ending     
	if  window:KeyHit(Key.Escape) then 
        closingGameInitiated = true
    end

    -- The game is closing and by pressing escape again, you close the game completely
    if closingGameInitiated and window:KeyHit(Key.Escape) then
        return false 
    if 

	Time:Update()
	world:Update()
    world:Render()
    
    --Draw some text that the game is closing and that they need to press escape again.
    if closingGameInitiated then
        --Draw a black screen
        context:DrawRect(0,0,context:GetWidth(),context:GetHeight())

        --Draw red text
        context:SetColor(1,0,0)
        context:SetBlendMode(Blend.Alpha)
        context:DrawText("Thanks for playing. Press escape to quit)",0,200,512,512)
        context:SetBlendMode(Blend.Solid)
    end

	context:Sync(false)
end

 

Link to comment
Share on other sites

EDIT: Well seems like we both answered at the same time xD haha

To make the game turn off you need to access the main loop, you find within the main.lua (find it in: "Scripts/main.lua"). By default Leadwerks has a build in game menu where you can quit the game, so normally there is already code inside which gives you the ability to break the main loop (which makes your game quit). You have to try around your self a bit. This is how my main loop looks:

 

while window:Closed()==false and RunGame do 
--this is the main loop "RunGame" is a global variable I created to handle whether the main loop is called or not
--here is some code
end

--Now example code for exiting the game (script for any kind of entity)
function Script:UpdateWorld()
	if window:KeyHit(Key.Escape) then RunGame = false end
  	--Now the game exits if you press escape
end


--if you want to do any actions before quitting do this
function Script:UpdateWorld()
	if window:KeyHit(Key.Escape) then self:ExitGame() end
  	--Now it calls a function (not nessecarily needed but recommended) and this function does stuff before setting RunGame false
end

function Script:ExitGame()
  --Any code you want to apply before ending here
  System:Print("Game Quits NOW!")
  RunGame = false
 end

 

Link to comment
Share on other sites

Okay, I'm new in this. So I tried something doesn't work out to me. So I created switch with swinging door lua script. Then I created pivot and put to it your code. Then in flowgraph editor I tried to connect pivot with switch. But I can't connect pivot. So I guess I doing something wrong or your scripts do something else?( I wanna that my game work like this: player comes to the room. he hits the switch. Black screen( or the table) with words" The end. Thanks for playing. Press esc to quit the game" will appear. Player will press esc key and the game will turn off.)

Link to comment
Share on other sites

First off I would recommend watching some tutorials about how lua scripting works in combination with Leadwerks. Some usefull tutorials: 

 

 

Secondly:

Pasting in the code from the previous posts isn't going to work because they are not object scripts, but adjustments to the main.lua script.

Create a new script and call it something like endscreen.lua and attach it to your pivot. Drag this pivot in to the flowgraph as well. Use the code below for the endscreen.lua script. (warning: I have not tested this.)

--This function is used to connect to in the flowgraph editor.
function Script:EnableEndScreen()--in
	self.drawEndScreen = true
end

--Draw some text that the game is closing and that they need to press escape again.
function Script:PostRender(context)
    if self.drawEndScreen then
        --Draw a black screen
        context:DrawRect(0,0,context:GetWidth(),context:GetHeight())

        --Draw red text
        context:SetColor(1,0,0)
        context:SetBlendMode(Blend.Alpha)
        context:DrawText("Thanks for playing. Press escape to quit)",0,200,512,512)
        context:SetBlendMode(Blend.Solid)
    end
end

 

Link to comment
Share on other sites

I can't fix it... I really tried... But I think here is a problem in the script. Then I hit the switch, I get white screen( thats good), but I dont get any letters. So I press esc key( you know I get that table where is these functions: resume game; options; quit), then I press quit and only then the text go out on screen. But then I drag my mouse on button" okay" or" cancel" the text disappears. Maybe you can make the text pop up on screen with the white screen? I don't know how to code... Thank you for your time, hope you understand what I wanna say.

Link to comment
Share on other sites

Okay so the white screen is a good sign. The original in game menu code is still in place and also reacts to the escape key being pressed. The easiest solution at that point is to use another key. Or you have to make sure that the mainmenu script doesn't work when you have initiated the ending screen. 

The reason you are seeing a white screen is because white is the active color. Use the SetColor() command right before drawing a rect to set the color.

I would recommend following the tutorials in the docs and possibly the lua basics. https://www.leadwerks.com/learn?page=

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...