Software Architect at Genzeon Corporation in Malvern, Pennsylvania, Microsoft .NET MVP, Husband, Dad and Geek.
17033 stories
·
21 followers

GameMaker Basics: Pause and Unpause

1 Share

1123-pauseunpause-hero.gif

Hello, and welcome to another GameMaker basics tutorial. Today, I want to talk to you about how to pause your game. It is a fairly simple process, but it can be a bit confusing if you are unfamiliar with certain GameMaker functions.

Basic setup

Pausing is simply the act of stopping, or freezing, the objects in your game. The code stops running in the object when paused, and picks back up when unpaused. There is a very handy GameMaker function called instance_deactivate_all(); that will make this easy for us to manage. This particular function will essentially turn off all of your game objects. The objects are not deleted, but they are no longer shown on screen when deactivated. When we need to unpause we simply call the function instance_activate_all(); to turn everything back on.

1123_pauseunpause_1.gif

The easiest way to manage pausing (and lots of other global game related things) is to do so from a Game Controller. No, I don’t mean an Xbox controller! I mean an object in your game that acts as the overseer for all other objects, game states, score, screen size, pausing, etc etc. Let’s start by creating that object. I prefer to keep the name of the object as short as possible, since you may be referring back to it often from other objects. I’m going to use oGame for this example. Create a new object, name it oGame, and add the Create Event. Inside the Create Event, we need to initialize some variables.

paused = false;
screenShot = -1;

The paused variable is simple enough. Its a boolean (a true/false) that we will use to trigger the pause/unpause state. The other variable, screenShot, will be used to store the index (in GameMaker an index is a number that points to a resource like a sprite or object) of a sprite that we will be creating later. For now you can ignore it.

Go ahead and add the Step Event to your oGame object. This is where we will set up the button press to pause and unpause. Let’s add a few lines of code.

if(keyboard_check_pressed(vk_space)){
    paused = !paused;
}

if(paused){
    instance_deactivate_all(1);
}else{
    instance_activate_all();
}

The first if statement simply flips the paused variable when we press the spacebar. By using the exclamation mark operator in front of a variable, we are essentially calling the opposite of the variable. paused = !paused will set the paused variable to the opposite of what it currently is. Since our variable is a boolean, it is set to true or false.

The second if statement is what deactivates, and reactivates, all of our objects based on whether or not the paused variable is true or false. You’ll notice that I’ve placed a one in the instance_deactivate_all(); function. That is because instance_deactivate_all(); needs to know if it should deactivate the object that is calling the function. In this case, our oGame object is calling the function. We don’t want that object to be deactivated, as it is the object in control of pausing. If it were deactivated we couldn’t unpause the game. If a zero were provided instead of a one, all objects, including the object calling the function, would be deactivated.

On the flip side of instance_deactivate_all(); we have instance_activate_all();. This function will activate any object that has previously been deactivated. It does not require any arguments or additional information.

Now let’s test this out. Go ahead and create some objects, give those objects a sprite, and place them in your room. It doesn’t matter if the objects are moving or running any sort of code. We need them to verify that our code is working. Once you’ve placed your new objects in the room, along with your oGame object, go ahead and run the game. If everything was set up correctly you should see your objects disappear and reappear when pressing the spacebar.

Additionally you may want to draw some text on the screen letting players know that the game is paused. Add the Draw Event to your oGame object and add the following code.

draw_set_halign(fa_center);

if(paused){
    draw_text(view_wview * 0.5, view_hview * 0.5, "Paused");
}

The first line will align our text to the center.

The if statement will draw the word “Paused” at the center of your screen while the paused variable is true. You could also draw your pause menu here, or draw a background behind the text if you wanted to.

But what if you wanted to keep drawing all of the objects on the screen that were deactivated? Well, that is slightly more advanced.

Slightly more advanced setup

Alright, this is where things can get a little bit tricky. We already set up the variable we needed for this setup. Remember the screenShot variable? Now we are going to use it. What we want to do is take a screenshot of the screen when pause is pressed, draw that screenshot to the screen while paused, and then delete the screenshot when we unpause. This can be especially tricky if you are unfamiliar with surfaces in GameMaker, so here is a quick crash course on surfaces.

A surface is like a canvas you are drawing on. Whenever you start a project in GameMaker, you are automatically drawing to the application surface, which is really just the default surface. If you wanted to you could draw to additional surfaces to achieve different effects. For now all we need is the application surface, and that is already provided for us.

Open your Step Event code in your oGame object and make the following changes.

if(keyboard_check_pressed(vk_space)){
    paused = !paused;
    if(!sprite_exists(screenShot)){
        screenShot = sprite_create_from_surface(application_surface,0,0,view_wport,view_hport,0,0,0,0);    
    }
}

if(paused){
    instance_deactivate_all(1);
}else{
    if(sprite_exists(screenShot)){
        sprite_delete(screenShot);
    }
    instance_activate_all();
}

The first addition is in the keyboard check statement. We check to see if NO sprite exists by checking our screenShot variable, which is set to -1 when initialized. If that particular sprite does not exist, we create a new sprite based on the application_surface and store it in our screenShot variable. The function used is sprite_create_from_surface(); and as you can see it requires a lot of arguments. You can see that we are addressing the application_surface to take a screenshot.

Check out the handy dandy table from the GameMaker Studio documentation for information on each argument.

1123_pauseunpause_3.jpg

Quick side note about assets and their indexes. An asset in GameMaker is a sprite, background, sound, room, path, font, or timeline. When an asset is created, it has an index assigned to it. Indexes are always positive numbers! So when we check to see if a sprite exists, as we did above, we are really checking to see if there is a sprite asset with that particular index. Since our screenShot variable had a negative number, there is no way it could have been an existing index.

The second addition made is to the unpause portion of our second if statement. We check again to see if a sprite exists, only this time if it does exist we delete it. If we didn’t delete the sprite we created we would very quickly run out of compute resources and everything would crash! Creating sprites on the fly like this is very expensive in terms of compute resources!

Finally, open up your Draw Event in the oGame object and let's draw the screenshot we captured. Only one line needs to be added.

draw_set_halign(fa_center);

if(paused){
    draw_sprite_ext(screenShot,0,0,0,1,1,0,c_white,1);
    draw_text(view_wview * 0.5, view_hview * 0.5, "Paused");
}

If you are unfamiliar with draw_sprite_ext();, click here to read a blog post I’ve written that explains it in great detail. Now, just a heads up if you run the game and your screenshot isn’t being drawn correctly to the screen. The x and y scale of the screenshot needs to be relative to your x and y viewport. In my example above, the viewport is the same size as the view itself, so it is a one-to-one scale. If your viewport is twice the size of your view, then you will need to reduce the scale to 0.5, and so on.

1123_pauseunpause_2.gif

If everything was set up correctly you should have a functional pause/unpause feature that draws everything to the screen! This is definitely not the only way to set up pause/unpause in your game, but it is a great starting point.

That's it for this entry! I hope you learned something and put this new knowledge to good use. You can follow me on Twitter and on my website for more gamedev related stuff. 

Additional resources 

NathanRBio.jpg

Nathan Ranney is the founder of punk house game dev shop, RatCasket. He’s best known for the creation and development of Kerfuffle, an online indie fighting game.

Read the whole story
alvinashcraft
7 hours ago
reply
West Grove, PA
Share this story
Delete

Getting more from the Windows Command Line

1 Share

One of my recent blog posts - "Git GUIs versus the CLI", discussed why I think that a GUI is far better suited for source control than the CLI. However ... being a big command line fan (albeit, perhaps not always for source control!), the last thing I wanted was to come across as anti-CLI! Far from it! So I thought a post about the CLI, together with a few tips to help you get the most out of it was in order!

Be warned - I'm a Powershell fan, so this post is a bit bias - but a lot applies to other shells, eg. Bash.

Why use a CLI?

First of all, let's start with the why ...

As developers, we're hopefully always looking for productivity wins to help us optimise our daily workflows. We're one of the few industries where we're luckily enough to be able to do this very easily. A big part of this, is automating bits of our workflow via tools and scripts.

Whilst our graphical tools play a big part in this - so also do our CLI tools. If you're only using graphical tools, then you're missing a very large subset of tooling you could be taking advantage of. I'm not saying the CLI is for everything. Use the right tool for the job of course - but it turns out that the CLI is the right tool for a surprising number of jobs!

The ability to pipe commands together is also extremely powerful. Allowing you to chain multiple commands together to do various things. A simple example might be this ...

ps IExplorer.exe | kill

Yeah, I know. Who wouldn't want to run that command eh?

Or another one ...

md SomeFolder | cd

Those are obviously very simplistic examples. You can chain together many commands, where each command does a distinct thing, then passes it onto the next command. Once you get comfortable with this - you start using it for a surprising number of things.

As mentioned earlier, my personal preference is PowerShell - where you're passing objects down the pipeline. In Bash, whilst this chaining concept is extremely powerful - you are just dealing with plain text. So to fully take advantage of this, you need to become proficient in using tools like Sed and Awk. Personally, I much prefer dealing with objects like you do in Powershell. That might just be my background as an object-oriented programmer though.

Finally, most shells provide tab completion. If you start typing something, then press the tab key - depending on what it is, the shell will try and complete the word for you. For example, if you start typing a path, pressing tab will cycle through the available sub-directories based on what you've typed so far. If you start typing a Powershell command (when in Powershell of course!), pressing tab will also autocomplete the command name. If you're using PoshGit (see below), then that also provides tab completion of things like branch names, etc!

And when you're comfortable with the CLI, this helps you become more comfortable with scripting languages - whether that be Bash, Powershell, batch files, or whatever. Then when running these scripts, it is more natural from the command line. You can obviously run them from Explorer, but it's not ideal.

A better CLI with ConEmu

Whether you run CMD, PowerShell, or Bash - the default command line experience in Windows isn't great. Even simple things like using the clipboard are a pain.

Enter ConEmu! If you're a Windows user, and you use the command line - this is a MUST! You can run different shells in it - eg. Cmd, PowerShell, bash, etc. It supports multiple tabs; split screen views; and much more! Here's an example screenshot showing a split screen layout ...

conemusplitscreenlayout

The above screenshot is of a project where I have three .NET Core services - Identity Server, a webapp, and an webapi. I run each of these with the usual dotnet run, and I like to see the console output of each of these in a separate pane. Conemu's split screen is perfect for this.

You can even create pre-defined tasks describing the split screen layout, so you can have different layouts for different projects or workflows.

Quickly search command history with Ctrl-R

This one is in both Bash and Powershell. By pressing Ctrl+R, you enter history search mode allowing you to type and search your command history. Given that a lot of the time, you're repeating a command you've already previously typed - this can be a massive time saving. Especially when you have commands with arguments that you may not remember.

Quickly changing directory with Z

A recent blog post by Scott Hanselman brought this very useful tool to my attention, and has changed the way I change directories in the command line!

Basically you can do z <some-substring-of-a-path-you've-used-before>, and it'll look up that path in the history and go to it. But, it gets better! You also get tab completion too! So you can type a bit of it, and tab through all the matches. This makes it switching between different directories really fast! And you don't even need to tab complete. If you just type a substring and hit enter, it'll go to the first match.

Chocolately

If you've used Linux before, you may have heard of apt-get. Chocolately does a similar thing, except it's for Windows. It's very easy to install, and once installed, you can easily install software by typing choco install <packagename>. Or just cinst <packname> for short.

chocolately.png

The cinst -y command you see there, can be run in a script, so you can have a script to install all of the packages you want to install

And you can do cup all -y to quickly ensure all your packages are up to date.

PoshGit

Whilst I mentioned earlier about my recent post discussing why I think a GUI is better suited for source control than the CLI - I'd definitely recommend that you are at least comfortable in both. And sometimes, the CLI is faster - eg. very quickly cloning a repository.

If you're a Git user, then I'd definitely recommend installing PoshGit. This adds additional information to your command prompt about the state of your current directory (if you're inside a Git project).

poshgit

You can see the branch name (master in the above screenshot), and you can see status summary information. You also get tab completion - eg. you can start typing a branch name and press tab to auto-complete.

It's very easily to install via the above mentioned Chocolately too!

RipGrep

I've only fairly recently discovered this gem, thanks to Joseph Woodward mentioning it on Twitter and taking the time to blog about it!

Again, installable via Chocolatey (cinst -y ripgrep) - this adds very fast grep functionality, including match highlighting.

ripgrep

Summary

To summarise - the command line is awesome. There's a lot of CLI tools to help you become more productive. It's not for everything, but by not using the CLI at all - you're missing all the goodness that the CLI is for. There's also going to be times, where the CLI is all you have - eg. when SSHing into a remote box.

Besides, more and more tools are now using the CLI again - with the dotnet CLI; Azure CLI; Angular CLI; Node CLI; <insert noun here> CLI, etc, etc. The CLI is making a return - so most certainly belongs in the toolbox of all developers!

Happy command-lining!

Read the whole story
alvinashcraft
7 hours ago
reply
West Grove, PA
Share this story
Delete

Top stories from the VSTS community – 2017.11.24

2 Shares
Here are top stories we found in our streams this week related to DevOps, VSTS, TFS and other interesting topics.  TOP STORIES Running Selenium Tests in Docker using VSTS Release Management – Colin DembovskyYou can spin up a Selenium grid (or hub) container and then join as many node containers as you want to (the... Read More
Read the whole story
alvinashcraft
7 hours ago
reply
West Grove, PA
sbanwart
15 hours ago
reply
Akron, OH
Share this story
Delete

Kick Up Your Feet And Relax, It’s Time For The Friday Five

1 Share
oscar-garcia
Oscar Garcia is a Software Solutions Architect who resides in Sunny South Florida. He is a Microsoft MVP and certified solutions developer with many years of experience building solutions using .Net framework and multiple open source frameworks. Currently, he specializes in building cloud solutions using technologies like ASP.NET, NodeJS, AngularJS, and other JavaScript frameworks. You can follow Oscar on Twitter via @ozkary or by visiting his blog at ozkary.com.

Step By Step Azure Network Security Groups NSG

Robert Smit is a Senior Technical Evangelist and a Microsoft MVP since 2009, now in Cloud and Datacenter management. Robert has more than 20 years experience in IT, particularly in the educational, healthcare and finance industries.Robert’s past IT experience in the trenches of IT gives him knowledge and insight which  allows him to communicate effectively with IT professionals. Based in the Netherlands, Robert currently holds a position at Insight at the Caase.com Division as Cloud Architect/Azure Technical Lead. Follow him on Twitter @Clustermvp.

Public Preview For Azure Archive Blob Storage And Blob-Level Tiering

Thomas Janetscheck is a Microsoft MVP for Azure and a Cloud Solutions Architect with Microsoft partner Data One, focused on hybrid infrastructure solutions with Microsoft Azure and on-premises IT environments. He lives in Germany, where he serves his customers in Benelux and the DACH countries. Thomas is one of the Azure Saturday initiators and organizers, as well as leader of two meetups that focus on Azure and Office 365. Thomas shares his 15-year experience working as a blogger, in trainings and as a speaker at meetups and conferences. Follow him on Twitter @azureandbeyond.

Presenting At A UN Event: Second Committee Considers Role of AI In Advancing SDGs

Stephen Ibaraki's positions include: co-chair Practitioner Board Association for Computing Machinery; founding chair Global Industry Council and vice-chair board IP3 International Federation for Information Processing; top 5 blogger IDG-IT World (Canada); founding managing partner REDDS Venture Investment Partners; founder technology advisory board, Yintech Investment Holdings Ltd.; founding member Beyond Initiative (Audi AI Think Tank), founding chairman outreach UN ITU "ICT Discovery" Journal; founding chair-moderator/keynote/organizer UN ITU AI events,  founding member steering committee seminal UN ITU AI for Good Global Summit with XPRIZE Foundation, and many more. Follow him on Twitter @sibaraki.

Using Microsoft Teams To Communicate At MSIgnite: A Case Study

Paul Olenick is Director of Product Strategy for AvePoint with more than 14 years of experience in the tech industry. He has been named a Microsoft Most Valuable Professional (MVP) for the past six years, a Virtual Technical Solutions Professional (V-TSP), and a Certified Technology Specialist (MCT).Paul has helped clients worldwide solve business problems by leveraging the Microsoft stack, and shares his experiences by contributing to books and speaking at conferences around the world. 

Read the whole story
alvinashcraft
7 hours ago
reply
West Grove, PA
Share this story
Delete

SQL SERVER – TCP Provider – An Operation On a Socket Could Not be Performed Because the System Lacked Sufficient Buffer Space or Because a Queue was Full

1 Share

Sometimes, I am contacted by blog readers via email and trust me, I get to learn something new. One of my blog reader contacted me for below error about buffer space.

TCP Provider, error: 0 – An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

I informed the reader that this error is not a SQL Server error. Even though the above was not caused by SQL Server but it was giving errors to client while connecting so their customer said it SQL issue. I did some searching and found a KB. They informed me that they have seen the Microsoft Knowledge Base article 196271. My next obvious question was that did they make the change? They answered that how can they verify that if they have the same issue.

I asked them to use NetStat output and check if they see entries in TIME_WAIT state. As per my understanding this might be leading to buffer/port exhaustion. Here is what I could see in my server and there is no issue and hence less output.

SQL SERVER - TCP Provider - An Operation On a Socket Could Not be Performed Because the System Lacked Sufficient Buffer Space or Because a Queue was Full tcp-wait-01

SOLUTION/WORKAROUND

Here are the solutions which I suggested them:

  1. Add registry setting for MaxUserPort as shown in http://support.microsoft.com/kb/196271
  2. Add registry setting for TcpTimedWaitDelay as mentioned in https://technet.microsoft.com/en-us/library/cc938217.aspx
  3. Other than above, App server reboot or IIS service restarts are few more additional options.

Reference: Pinal Dave (https://blog.sqlauthority.com

First appeared on SQL SERVER – TCP Provider – An Operation On a Socket Could Not be Performed Because the System Lacked Sufficient Buffer Space or Because a Queue was Full

Read the whole story
alvinashcraft
7 hours ago
reply
West Grove, PA
Share this story
Delete

How to Move TempDB to Another Drive & Folder

1 Share

Every time I have to do this, I Google for a script. I might as well write my own and put it here so at least I find myself in the Google results:

SELECT 'ALTER DATABASE tempdb MODIFY FILE (NAME = [' + f.name + '],'
	+ ' FILENAME = ''Z:\MSSQL\DATA\' + f.name
	+ CASE WHEN f.type = 1 THEN '.ldf' ELSE '.mdf' END
	+ ''');'
FROM sys.master_files f
WHERE f.database_id = DB_ID(N'tempdb');

The results:

Moving TempDB

I can then copy/paste the results into SSMS, edit them, and run ’em. I like generating scripts like this rather than flat-out executing it automatically because sometimes I need to tweak specific file locations.

I’m not bothering with detecting what the file names were before, and I’m just generating new file names to match the database object’s name instead.

Erik says: See that path? Make sure you change that path to the right one. Preferably one that exists, and that SQL Server has permissions to. If you don’t, your server won’t start up unless you Google “how to start SQL Server without tempdb” and spend the next morning explaining to your boss why you should keep your job. I mean, I’ve heard stories…

Read the whole story
alvinashcraft
7 hours ago
reply
West Grove, PA
Share this story
Delete
Next Page of Stories