Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

18. September 2017

In this section of our Bowling with Game Engines series, we will be implementing our bowling game using the open source Godot engine. The idea is straight forward, implement the same simple 3D game across a number of game engines. One warning right up front, this engine uses Godot 2.x on the eve of Godot 3.0 being released. Godot 3.0 will receive rather large changes to the 3D portions of Godot. We will go step by step through the process of creating our game, both in text as well as a video version available. All of the assets used in this tutorial are available on Patreon as part of the bowling game kit, along with project files and this document in PDF form. Don’t worry, these aren’t needed to follow along.  There is a video version of this entire process available here or embedded below.

First fire up Godot and create a new project. Once Godot is loaded, click New Project, then pick a location to create the project in. The project directory will be the project’s name.

clip_image002

Now double click your newly created project to load it.

clip_image004

Let’s start things off by creating our Title screen. Before we go too far ahead, lets create an empty node to parent our scene then save it. In the Scene panel, click the Plus Icon

clip_image006

Select Node from the following dialog:

clip_image008

Select the Scene menu, then Save Scene as:

clip_image010

Name it TitleScene.tscn and click Save. Ok now the we have a Scene to work with, time to get to work. Let’s import the title scene image. You can use any supported texture format, but if you are working from the Patreon files, the document you want is Titlescreen.png. Select the Import Menu, then Texture

clip_image012

In the following dialog, be sure to select 2D Texture, locate the texture you want to import, then click the Import button. The defaults for Texture options and format are fine. Please note, for 2D steps you can actually skip this step and directly copy the texture into your project folder using Explorer/Finder.

clip_image014

Now that we have our title screen texture, it’s time to go ahead and use it. In the Scene panel, select the node we created earlier, then click the + Icon. This time we want to add a node of type TextureFrame. You can filter the options available in the node list to quickly find the node you want to create.

clip_image016

Now that we’ve created a TextureFrame node, in the Inspector, locate the Texture property, drop down the menu and select Load. Select our newly loaded texture.

clip_image018

Now turn the Expand property on:

clip_image020

And resize the Texture to the full size of your viewport:

clip_image022

Woot, most of the way there… now we want to add some looping title music that palys when the game starts. We need to import a song to use. In this case I’m using a simple WAV file that we are going to loop. The process is just like with Textures, select Import->Audio Sample

clip_image024

In the resulting dialog, locate the WAV file you want to import, in options select Loop then finally select Import.

clip_image026

Ok, now that we have a sound effect to play, let’s play it. In the Scene panel, select TextureFrame then click the + Icon. This time the node type we want is a sample player. Your scene should look like this now:

clip_image028

Hmmm, that error Icon can’t be a good thing, can it? No worries, we just need to define the sample to play. This is going to take a couple step sthough, first we need to add a Sample Library, then add our sample to it. Don’t worry, it’s not that difficult. With the SamplePlayer selected, in the Inspector drop down the Samples option and select New SampleLibrary.

clip_image030

Now select samples again and this time choose Edit.

clip_image032

This brings up a new editing window:

clip_image034

Click the folder icon, select your imported sample in the resulting dialog. It should then look like:

clip_image036

Now in the Inspector we should be able to select our newly loaded sample in the Play menu:

clip_image038

This whoever has one EXTREMELY annoying side effect… the sample will play over and over in the editor while you have this scene open. Yeah, it gets annoying fast. Time to do our first scripting instead! Let’s add some code that plays our sound when the scene is loaded… a sure way to keep sane! This means we need to add a script to node in our scene… don’t worry, its pretty easy.

We are going to create and attach a script to the TextureFrame node, the parent of our SamplePlayer. Right click the Texture Frame in the Scene panel, then select Attach Script.

clip_image040

In the resulting dialog click the .. next to Path and name your file TitleScreen.gd, then click Create.

clip_image042

This will bring up the script editor like so:

clip_image044

Now we want to change the code like so:

func _ready():
set_process_input(true)
get_node("SamplePlayer").play("BowlingOhYeah")


Now it’s time to check out all of our hard work. Now click the Play icon:

clip_image046

Since this is the first time we’ve run it, we need to tell Godot which Scene is the entry point for our application.

clip_image048

Click the Select button. Then select TitleScreen.tscn in the resulting dialog. Now your application should run!

clip_image050

Now of course we have to get to the guts of our actual game. Next we create our next scene where the majority of the game is going to occur. First make sure to save your existing scene if you haven’t already. Then click Scene->New Scene.

clip_image052

Every scene must have at least one node. So in our newly created scene, again click the + Icon in the Scene Panel and select Node. Now save the scene as GameScene.tscn.

Perfect, now we have somewhere to go from our title screen... Next, we need to have some kind of action or trigger to switch between scenes. We are going to handle the scene change whenever the user presses any key or clicks any mouse button. This involves appending a bit of code to our script. Simply add the following function at the bottom of TitleScene.gd:

func _input(event):
  if(event.type == InputEvent.KEY || event.type == InputEvent.MOUSE_BUTTON):
  get_tree().change_scene("GameScene.tscn")

Excellent! We are now done with the Title scene… Time to move onto the main event!

If it isn’t already loaded, load up GameScene.tscn. Simply double click it in the asset view:

clip_image054

Multiple scenes can be open at once and you cant toggle between them using tabs across the top of the screen. You can also switch between scripting, 2D and 3D modes. We are working in 3D in this scene, so make sure that’s selected.

clip_image056

Now we are going to assemble all of the various assets we need to create our game. We have a Bowling Pin, Bowling Lane and in this example a Bowling Ball. You may note this differs from other “Bowling With” tutorials, in that in other tutorials we procedurally generate the bowling ball. In Godot 2.x there are no editor assessable geometric primitives so we instead import the bowling ball as a 3D model. There is however a plugin available for Godot that enables you to create meshes like spheres, cubes and planes that I document the use of in this video ( https://www.youtube.com/watch?v=Ca2FcVb2lBk ).


Click here for next part in the series.


The Video

Programming , ,

18. September 2017

Click here for the previous tutorial section.

Now it’s time to create our bowling lane. If you are a Patreon, the resources we are using are located in the GFS Dropbox in the GameKits\Bowling folder. Godot works a bit different from other game engines in that complex models are imported as scenes. Simply select Import->Scene

clip_image002

At a minimum select the DAE (COLLADA) file location, then the location within your project to import into, then finally click import.

clip_image004

One thing that is very important to realize here is this process will import the entire scene from the model file. This includes lights, cameras, etc… so be sure when you export to DAE, you only export the items you want imported. Of course, you’ve got the option of deleting unneeded items in Godot if needed.

It is possible that the import process doesn’t always bring in the textures, so we will cover doing this part manually. If your model imported fully textured, you can skip ahead and ignore this section. First import that texture object into your scene. The bowling lane has two textures, a diffuse (color) and normal (depth) map. Select Import->Texture.

clip_image006

Then select the texture, where to import it and finally hit the import button.

clip_image008

Repeat this process for each texture file you need to import. Now we need to define a material on our BowlingLane node. Select the BowlingLane, locate MeshInstance->Material->0, click the drop down and select New FixedMaterial.

clip_image010

This will create a new material. Drop it down again and this time select Edit.

clip_image012

Now locate Diffuse, drop down the pull down and select Load.

clip_image014

Select your newly imported Diffuse texture. Now repeat the process for Normal

clip_image016

You can control the strength of the normal map using the Normal Depth setting:

clip_image018

Your Bowling Lane should now look a lot more like a bowling lane than before!

clip_image020

Now we need to add some Physics nodes to our lane. In the Scene graph, select the BowlingLane mesh instance, right click and select Add Child Node.

clip_image022

Select StaticBody.

clip_image024

This will make our lane part of the physics simulation, but as the name suggests, it wont be affected by it. So basically, a static body can be hit, but nothing will happen to it. Finally, we need to define the geometry of physics object. Right click your newly created StaticBody, select Add Child Node Again and this time select CollisionShape.

clip_image026

With the newly created CollisionShape selected, locate Shape, drop down and select New BoxShape.

clip_image028

Now select the Shape drop down again and select Edit

clip_image030

Now modify the Extents until it tightly wraps the underlying shape.

clip_image032

We are now done with the lane. Save the scene file and close it.

Creating the Bowling Pin

Now we repeat the exact same process, except this time with our bowling pin. The process is actually identical, except instead of creating a StaticBody, we create a PhysicsBody. However, in this case the RigidBody needs to be the parent of the Pin. Don’t worry, its pretty simple. Be sure to make the RigidBody node a child of the Root “BowlingPin” node, then drag the Pin node onto the newly created RigidBody. So we can identify the node in code later, also rename it from RigidBody to PinRigidBody. The end result should look like:

clip_image034

In the properties of our RigidBody, we also want to set Can Sleep off and Contact Monitor on, like so:

clip_image036

Creating our Bowling Alley

Now it’s time to put it all together. Go back to GameScene in the editor and we need to create some instances of our lane and pins. Simply locate the lane in the assets view, right click and select Instance.

clip_image038

Now repeat the process for the bowling pin. Your scene should now look something like:

clip_image040

Next reposition the bowling pin and so it’s above the lane and down a bit. Now it’s the moment of truth… are you a 5 pin or 10 pin fan? Either way, duplicate the first pin. Locate it in the Scene panel, right click and select duplicate ( or select and hit Ctrl + D ).

clip_image042

Then move the pin into position and repeat the process. If you want, now is your chance to create 7 pin bowling! Personally, I went with 5, and it looks like this:

clip_image044

5 pin for life! We are certainly getting there! It’s at least starting to feel like a bowling game. It would be a good time to check out our work, but press play and you’ll notice a problem… nothing shows up! That’s because we need to create a camera! With the root node selected, create a new node and select Camera:

clip_image046

Now position the camera in your scene using the transform manipulator. With a camera node selected, you can hit the Preview button at any time to, well, preview the camera.

clip_image048

Now when we press play…

clip_image050

Hmm, a bit dark in here isn’t it? Well that makes sense, we have no lights in our scene. We have two options here… we can add some lights or we can add ambient lighting. Getting the later right is probably a bit easier, so lets take that approach. With the camera selected, located the Environment setting, drop it down and select New Environment

clip_image052

Now drop it down again, this time selecting Edit. Now turn Ambient lighting on and select a color close to white.

clip_image054

One thing you might find is that the results are really blurry and undesirable. There are two ways to address this… first, select the texture, locate the flags propert and turn off MipMap and Filtering:

clip_image056

Finally, in the mesh of an imported scene, you can turn off baked lighting:

clip_image058

Both of these steps are completely optional. At this point our game should look like:

clip_image060

And our scene should look like:

clip_image062

Now all we need is a ball!  Click here for the next tutorial section.

Programming , ,

18. September 2017

Click here for the previous section in this tutorial.

Time for the exact same process as pin and lane, except this time rename the RigidBody to BowlingBallRigidBody. The major differences are we used a SphereShape for our collision shape.

clip_image002

Plus, in physics, in addition to setting Sleep off, and Contact Listener on, we increase the Mass to 3. The value doesn’t really matter, this just means our bowling ball is 3x heavier than the pins.

clip_image004

Just for now lets get things rolling (ugh… the puns) and give our ball some initial velocity, so when we play our game, things happen. We will override this in a bit when we implement user control. Simply set the velocity to -15 or so on the Z axis:

clip_image006

Now when you play the game, if you’ve done everything correctly, the ball should roll down the lane and hit the pins.

clip_image008

Almost there! Now let’s add some audio that happens when the ball hits a pin. It’s the exact same process as back when we created our title screen, so I’m not going to go over it again. Just be sure to add the SamplePlayer to the bowling ball like so:

clip_image010

Then go in and create a sample library and add our bowling ball sound effect, in my case Hit.wav.

Now it’s time to do a bit of coding. The BowlingBall is going to be the heart of our game, so let’s add the script to it. Using the same process as back in the title screen, select the BowlingBall root node and attach a new script. Now select the child BowlingBallRigidBody node, switch to the Node tab, select body_enter and hit the Connect button at the bottom right hand corner.

clip_image012

Another window will pop up asking you which node to connect to. Select our BowlingBall node then click Connect.

clip_image014

This will open up the script editor and create a new function for us. Inside the function enter the following code:

func _on_BowlingBall_body_enter( body ):
	var name = body.get_name()
	if name == "PinBody":
		self.get_node("SamplePlayer").play("Hit")

Now when you play the scene, when the bowling ball hits the pins, audio should play!

Handling User Input

** Before you continue… go back to the bowling ball and remove the Linear velocity we added earlier! We are going to handle that manually from this point on! **

Next we need to give the user some control over the bowling ball. First off, we need to create an Input map. We are going to use left and right arrows for moving the ball, well… left and right. Additionally, the space bar will be used to throw the ball.

Go to the Scene -> Project Settings Menu, then switch to the Input Map tab. Now we are going to add an entry. Simply enter LEFT in the text box, then click Add:

clip_image016

Click the + Icon, then select Key

clip_image018

When prompted, press the Left arrow key

clip_image020

Now repeat this process for Right and Space. You can of course map multiple keys to a single ID, so if you wanted LEFT to also trigger when the A key is pressed, or on Left mouse click, you can. Now lets wire our script up to handle input. Simply add or replace the following in your BowlingBall script:

func _ready():
	set_process_input(true)

func _input(event):
	if(event.is_action_released("LEFT")):
		self.get_node("BowlingBallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(-1,0,0))

	if(event.is_action_released("RIGHT")):
		self.get_node("BowlingBallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(1,0,0))
		
	if(event.is_action_released("SPACE")):
		self.get_node("BowlingBallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(0,0,-35))

Great, we can now control our ball, have collisions with pins and audio plays. We are 99% of the way toward a finished game… only one catch… you can only play once… that’s a bit of an issue no? Let’s do a quick and direct reset by reloading the scene when the user presses the R key. Add RESET to the InputMap and append the following code to your _input() function:

	if(event.is_action_released("RESET")):
		get_tree().reload_current_scene()

And our final complete code should look like:

extends Spatial

func _ready():
	set_process_input(true)
	set_process(true)

func _input(event):
	if(event.is_action_released("LEFT")):
		self.get_node("BowlingBallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(-1,0,0))

	if(event.is_action_released("RIGHT")):
		self.get_node("BowlingBallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(1,0,0))
		
	if(event.is_action_released("SPACE")):
		self.get_node("BowlingBallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(0,0,-35))

	if(event.is_action_released("RESET")):
		get_tree().reload_current_scene()

		
func _on_BowlingBallRigidBody_body_enter( body ):
	var name = body.get_name()
	if name == "PinRigidBody":
		self.get_node("SamplePlayer").play("Hit")


And all done, one complete if primitive bowling game!  Once again, here is the video version.

Programming , ,

7. August 2017


With the upcoming release of Godot 3.0, Visual Programming is now officially part of the game engine.  In this tutorial we are going to look at the basics of programming using the new Visual Programming interface.


To get started using Visual Programming, just like using GDScript or other options, you have to attach a script to an object in Godot, like so:

image


In the script creation dialog, you can now select the programming language to use:

image


It will now bring you to the following programming interface:

image


To get started, we need to implement one of the available Godot callbacks.  These are exactly the same as GDScript and represent various different functions that will be called over time by the game engine.  To get started, in the Members section, click the following icon:

image


The various different callback functions available for the selected object type are available:

image


The most commonly used callbacks are _process() which is called every pass through the game loop, _ready() which is called when the object has been created, _init() which is called to create the object (think constructor), and _input() which is called when input is received.  We are going to create an exceedingly simple example here that moves our sprite each from, so select _process.  Now you should see:

image


White arrows represent program flow.  Connections on the right represent output, while connections on the left represent input.  This is the entry point for our code and is going to be called each frame.  You can see we are passed a floating point value “delta” as a parameter.  Now we need to get a reference to our object, we use a “self” node for this.  You can locate notes via search:

g1


This gives us access to the Sprite object our script is attached.

image


Now click on the blue/green ball output from the Sprite and drag a connection out, selecting Get as the option:

g2


This will now give you access to all of the data of a sprite object and all other inherited classes:

image


Locate and select position, which is a Vector2 object.  Now lets create a variable we are going to use to define our move amount.  In the variables section, click +:

image


Now right click and select Edit Variable:

image


Select type as Vector2 and value of (1,0)

image

Finally click the variable name and rename it to moveBy.  Now drag our newly created variable onto the canvas:

image


Now we want to create an Add node and add our two vectors together, like so:

image


Now we use another Self (or the same one we created earlier) and this time do a Set and select position.  We now pass in the added Vector2 result, also we attach program flow from _process, like so:

image


This simple program will cause the sprite to move by 1 pixel per update.


Video Version

Programming ,

27. July 2017


Today Godot just released the first downloadable version of the upcoming Godot 3.0 game engine.  This is an alpha level release, so while mostly feature complete do expect to encounter several bugs.  If you are godwaiting for C# support, unfortunately it did  not make it into the alpha release but hopefully will be released soon.  This alpha however is absolutely packed with new functionality including the completely new 3D renderer and material system, a new visual programming language, GDNative, an updated UI and much more.


From the release notes:

The features

Now, there are still some cool features that can already be played with in this alpha1 build, and we are looking forward to seeing what you will come up with using the new 3D renderer. There is no exhaustive listing of all the new features to experiment with, but you can read past articles of this blog and check Juan's Twitter feed for some teasers :)

Please use the community channels to discuss with existing users and learn how to use the new workflows of Godot 3.0 - as of this writing there is almost no documentation on the new features, but this alpha1 build should serve as a starting point for documentation writers.


I made a 20 minute hands-on video showcasing some of the new functionality available in Godot 3.0 available here and embedded below.

GameDev News

Month List

Popular Comments