29. April 2015

 

In the previous part we looked at we look in a bit more detail at working with Sprites, now we are going to get a bit more… animated.  In this tutorial we are going to look at using Spritesheets, named Flipbooks in Unreal Engine, to create sprite animations.

 

A bit of a warning before we jump in, as of writing this process is a bit buggy and needing improvement.  Hopefully by the time you read this, the Unreal team have ironed out some of the kinks.  If you want more details on creating the spritesheets and a bit more exposure to some of the Unreal Engine faults, be sure to check out the video version, it goes into much greater detail.

 

This tutorial is available in an HD video here.

 

Importing a Spritesheet

 

A Spritesheet is simply a texture with multiple sprites on it.  This is the one I am using for this example:

spritesheet

 

It is a power of 2 resolution PNG file containing 25 frames of a walk cycle animation.  A bit of a warning however, I HAVE HAD NO SUCCESS WITH PNGs in Unreal.  To get this to work I had to convert to tga format before importing.  Regardless to how the PNG is made, the results are almost always mangled once imported to UE4.

 

To get started, simply drag the texture in as normal.  The critical part of this equation is that your file must have an alpha channel, as the alpha channel is used to calculate each sprites location in the next portion.

 

Now that we’ve added our spritesheet to our textures, simply right click and select Extract Sprites:

image

 

Using the alpha channel, it will automatically create a sprite for each image in the spritesheet, like so:

image

 

With all of the generated sprites selected, I now moved them into their own folder in the Sprites folder.  This step is of course strictly optional.

 

Now a bit of a bug/problem with Unreal Engine…  Each of the generated sprites is automatically named, but the names themselves make no sense.  First off, they are named FILENAME_Sprite###.  However, the Content Browser doesn’t sort properly for alpha numeric, meaning that Sprite11 and Sprite12 come before Sprite2 and Sprite3, which makes things particularly painful to work with.

 

Even worse, the order they are created in makes absolutely no sense right now… so the process of creating a flipbook is needlessly complicated.  You’ll see what I mean in a moment.

 

First lets look at a generated sprite and suddenly Edit Source Region will make a ton more sense.  Simply double click a sprite to bring it up in the Sprite Editor.  Here is the very first sprite in the sequence opened in the editor. 

image

 

Now click Edit Source Region and you will see how multiple sprites can be stored in a single texture:

image

 

As you can see, our sprite is actually just a very small portion of the over all texture.  We would expect the next sprite to be Sprite2, 3, 4, etc…  but sadly, they aren’t.  I honestly cant make rhyme or reason of the naming convention Unreal used for automatically parsed sprites.  Sadly you will need to figure them out in order to deal with the next part of the process.  Hopefully this all gets fixed soon, as it’s extremely irritating.   For now select each sprite and figure out where it is in the order…  mine basically went

 

Row1: sprite, 2,3,4,5,6,7

Row2: 12,13,14,11,10,8,9

Row3: 15,16,17,18,19,20

Row4: 24,25,23,22

 

 

… if you see the logic there, good on you!  Ultimately once you figure the order out, the names really don’t matter, but you can rename them to make sense if you want.  Or you could change the UV values of each so the existing names actually make sense.  Me, I’ll just wait for Unreal to fix this mess.

 

Creating a Flipbook Animation

 

Anyways, once you’ve figured out your sprite frame orders, it’s time to create a new Flipbook.  Simply select New->Animation->Sprite Flipbook, like so:

image

 

A new flipbook will be created, feel free to rename it.  Then double click it to bring up the Flipbook Editor:

image

 

Now what you want to do is drag each Sprite over to the bottom of the page, in the order of the animation frames ( that’s the list I made above.  Basically if your spritesheet is like mine you want the top row, left to right, then the next, and the next, etc ).  The order they are dragged is the order they are played in the sequence:

image

 

You will see a real-time preview of your animation as you drag sprites in:

g2

 

You can change the over all speed of your animation here:

image

 

That is the number of frames that will be shown each second.  So a 26 frame animation would take 2 seconds to complete.  You can also set the duration of each frame, here:

image

Frame Run is how many frames this animation frame will be shown for.  For example, if you had Frames Per Second set to 10 and Frame 1 set to a Frame Run of 5, that particular frame would be shown for 1/2 a second.

 

Now your animated sprite can be added to the scene just as easily as a normal sprite, simply drag the flipbook in to your scene:

g3

 

Truth is however, you will rarely use a Flipbook this way, instead they will generally be owned by an Actor, which we will cover in the next portion.

 

The Video

 

Programming , ,

28. April 2015

 

In this tutorial we are going to explore the tilemap functionality built into the Godot game engine.  A tile map is a 2D game map composed of layers of “tiles”, which are essentially just a fixed size sprite with some additional properties.  It allows you to quickly paint a level and reuse art assets, greatly decreasing storage and memory requirements for levels.  In addition to supporting tile maps, Godot also includes a handy editor, although there certainly are some warts.

 

There is an HD video version of this tutorial available here or embedded below.

 

We are going to require two scenes for this example, one to create our tileset and one to actually use it.

 

Creating a Tileset

 

Before we continue though, I need a tileset to use.  Instead of creating one, I am going to use this one from OpenGameArt.org.  Of course, you can use whatever image you want, just be sure the tiles are the same size and have no spacing between them (if you want to keep the math easy).

 

Here is the tileset I chose:

PathAndObjects

 

This version however is shrunk down, use the link above if you want to use the exact same tiles.  Each tile in this image is 32x32 pixels in size.  This will be important shortly.

 

Now let’s create a tileset in Godot.  Create a new scene.  This process is really labor intensive unfortunately, but very powerful too.  Create a root Node, then a Sprite for your first tile.  Each tile needs to have a unique name.  I am only going to use a subset of what is available on that spritesheet, but you simply repeat the process to add more.

 

Create a hierarchy like this:

image

 

Now for Sprite Tile1.  Now configure the sprite’s Texture to your spritesheet.  Importantly, set Region to on and set the Region Rect to 0,0,32,32, like so:

image

 

This sets the image source of this tile to a 32x32 region at the top left corner of the texture.  Now you should see:

image

 

Now let’s setup snapping, so we can arrange our tiles in a nice grid.  Select Edit->Configure Snap

image

 

Set Grid step to the same dimensions as your tiles, in my case 32x32:

image

 

Now select “Use Snap” as seen in the menu above.

 

The next part is optional, if you want to use physics or collisions or not.  If you do, we now need to set up StaticBody and hit detection boundaries.  This process is the same as we went through in the previous tutorial so I am not going to go into details on how.  You do want the end result like this however:

image

 

Then define the areas of your CollisionsPolygon2D that are collide-able or not, like so:

image

 

Turning snap on and off is critical here.  Snap makes it easy to create the initial bounding box, as it will automatically snap to each corner.  However, when you start adding fine detail, be sure to turn it off.

 

Now repeat this process for each tile you wish to include from your spritesheet.  You can make the process a great deal quicker using the Duplicate option ( or Ctrl +D ).  Simply select your first tile then duplicate.  Then you just need to reposition it on the map, redefine the CollisionPolygon2D bounds and of course, update the region to the next tile, like so:

image

 

This will select the next tile from the top left of the image.  Repeat until you have all of your tiles defined.  Yes, it would be nice if this was an automated process!

 

Because I’m lazy, im only going to define three tiles, like so:

image

 

Which look like this with collision polygons defined:

image

 

Please note, there was no need to put space between each tile like I have here.  Now that we’ve defined our tiles, first save your scene, I called mine tilemap.scn.  Now we are going to export a tilemap.  Simply choose Scene->Convert To…->TileSet…

 

image

 

I called mine tileset.xml.  Be aware of the option Merge With Existing.  When you make changes to your tileset in the future, you probably want to turn this off if you want your changes to be completely overwritten.

image

 

I personally found the Collision polygon exporting to be extremely buggy.  You may want to open up the generated XML file and make sure collision information was properly exported:

image

 

Using a Tilemap

 

Now that we’ve got our tileset created, our collision polygons configured and all things ready to go, it’s time to create a new scene then create a Tilemap.

 

The Node you want to add is a TileMap, like so:

image

 

Configure your tilemap like so:

image

 

If you are using Physics, set Use Kinematic on, otherwise don’t.  Set the Cell Size to the size of your tiles and tileset to your newly exported tileset.

 

You will notice now, with the TileMap node selected, your tiles will appear down the left hand side of the 2D editor window:

image

 

You can now use these to “paint” your level, like so:

image

 

You can create multiple TileMap objects if you need to have different layers of tiles (like foreground props for example).  Today though, we are going to stick to this simple example.

 

Finally I created a RigidBody sprite to interact with our tilemap, so our final scene looks like this:

image

 

Now when we run it, it should look like:

g1

 

You can make some edits to your tilemap from within Godot without re-exporting the tileset.  With your Tilemap selected, select your Tileset property, then Edit:

image

 

You will notice a new Theme menu appears, allowing you to edit your Tileset, such as adding new items:

image

 

You can also see the tiles that make up the tileset in the Inspector:

image

 

The Video

 

Programming , ,

24. April 2015

 

As you may have guessed from the title, it today’s tutorial we are going to look at working with Sprites using Unreal Engine.  We already looked briefly at creating a sprite in the previous tutorial, but today we are going to get much more in-depth.

 

Before you can create a sprite, you need to have a texture to work with.  Unreal Engine supports textures in the following formats:

  • .bmp
  • .float
  • .pcx
  • .png
  • .psd
  • .tga
  • .jpg
  • .dds and .hdr ( cubemaps only, not applicable to 2D )

 

That said, not all textures are created equal.  Some formats such as bmp, jpg and pcx do not support an alpha channel.  This means if you texture requires any transparency at all, you cannot use these formats.  Other formats, such as PSD ( Photoshop’s native format ) are absolutely huge.  Others such as BMP have very poor compression rates and should generally be avoid.  At the end of the day, this generally means that your 2D textures should probably be in png or tga formats.  Unreal also wants your textures to be in Power of Two resolutions.  Meaning that width/height should be 2,4,8,16,32 … 512, 1024, 2048, etc…  pixels in size.  It will work with other sized textures, but MIP maps will not be generated (not a big deal in 2D) and performance could suffer(a big deal).  Keep in mind, your sprite doesn’t need to use all of the texture, as you will see shortly.  So it’s better to have empty wasted space then a non Power of Two size.

 

* Personally I’ve experienced all kinds of problems using PNG, such as distorted backgrounds, while TGA has always worked flawlessly. 

 

Adding a Texture to your game

 

Adding a Texture is simple as selecting a destination folder on the left, then dragging and dropping the appropriate file type (from the list above) from Finder/Exporter to the Content Browser window, shown below:

image

 

Alternately, you can click New –> Import

image

 

Then navigate to the file you wish to use and select it. 

 

You texture should now appear in the Content Browser.

 

Texture Editor

 

Now that you have a texture loaded, you can bring it up in the Texture Editor by either double clicking or right clicking and selecting Edit.  Here is the texture editor in action.  It is a modeless window that can be left open indepently of the primary Unreal Engine window.

 

image

 

The Texture Editor enables you to make changes to your image, such as altering it’s brightness, saturation, etc…  you can also change compression amounts here.  However, for our 2D game, we have one very critical task…  turning off MIP maps.

What's a MIP Map?

History lesson time! MIP stands for multum in parvo, Latin for "much in little". Doesn't exactly answer the question does it? Ok, lets try again. Essentially a MIP map is an optimiziation trick. As things in the 3D scene get further and further from the camera, they need less and less detail. So while right up close to an object you may see enough detail to justify a 2048x2048 resolution texture. However, as the rendered object gets farther away in the scene, the texture resolution doesn't need to be nearly as high. Therefore game engines often use MIPMaps, multiple resolution versions of the same texture. So, as the required detail gets lower and lower, it can use a smaller texture and thus less resources.
You know when you are playing a game and as you move rapidly, often textures in the background "pop" in or out? This is the mipmapping system screwing up! Instead of seamlessly transitioning between versions, you as the user are watching the transition occur.


Support for MIP maps is pretty much automatic in Unreal Engine.  However in the case of a 2D game, you don’t want mipmaps!  The depth never changes, there should never be different resolution versions of each texture.  Therefore, we want to turn them off, and the Texture Editor is the place to do it.  Simply select Mip Gen Setting and select NoMipmaps.

image

 

Before you close the Texture Editor, be sure to hit the Save button.

image

 

Creating A Sprite

 

Now that we have a Texture, we can create a sprite.  This is important, as you can’t otherwise position or display a Texture on it’s own.  So, then, what is a Sprite?  Well the nutshell version is, it’s a graphic that can be positioned and transformed.  The name goes back to the olden days of computer hardware, where there was dedicated hardware for drawing images that could move.  Think back to PacMan…  Sprites would be things like PacMan himself and the Ghosts in the scene.

 

In practical Unreal Engine terms, a Sprite has a texture ( or a portion of a texture, as we will see shortly ) and positional information.  You can have multiple sprites using the same texture, you can have multiple sprites within a texture, and the sprites source within a texture can also change.  Don’t worry, this will make sense shortly. In the meantime, you can think of it this way… if you draw it in 2D in Unreal Engine… it’s probably a Sprite!

 

Once you have a Texture in your project, you can easily create a sprite using the entire texture by right clicking the Texture and selecting Create Sprite, like so:

image

 

You can also create a new sprite using New->Miscellaneous->Sprite

image

 

This will then open up the Sprite Editor.  If you created the Sprite using an existing texture, the texture will already be assigned.  Otherwise you have to do it manually.  Simply click the Texture in the Content Browser.  Then click the arrow icon in the Details panel of the Sprite Editor on the field named Source Texture:

image

 

Your texture should now appear like so:

image

 

You can pan and zoom the texture in the view window using the right mouse button and the scroll wheel.

 

Now remember earlier when I said “all or part of the texture”?  Well a Sprite can easily use a portion of a texture, and that’s set using the Edit Source Region mode:

image

 

This changes the view panel so you can now select a sub rectangle of the image to use as your sprite source.  For example, if you only wanted to use Megatrons head, you could change it like:

image

 

Then when you flip back to View, your texture will be:

image

 

When dealing with sprite sheets, this becomes a great deal more useful, as you will see shortly. 

 

There are a couple other critical functions in the Sprite Editor that we will cover later.  Most importantly, you can define collision polygons and control the physics type used.  We will look at these functions later on when we discuss physics. 

 

Two very important settings available here are:

image

 

Pixels Per Unit and Pivot Mode.

 

Pixels per unit is exactly what it says… a mapping from pixels to Unreal units, which default as mm.  So right now, each pixel is 2.56mm in size.  Pivot Mode on the other hand determines where a sprite is transformed relative to.  So when you say rotate 90 degrees, you are rotating 90 degrees around the sprites center by default.  Sometimes top left or bottom left can be easier to work with, this is where you would change it.

 

The final important point here is the Default Material, seen here:

image

 

This part is about to look a lot scarier than it is!  Just know up front, if you prefer, you can ignore this part of Unreal Engine completely!

 

Materials

 

Every mesh in Unreal Engine has a material attached, and when you peel back all of the layers, a Sprite is still ultimately a mesh… granted, a very simple one.  There are two default options available to you included in the engine, although depending on how you created your project, you may have to change your view settings to access them:

image

 

Then you will find the two provided materials for sprites:

image

 

The name kind of gives away the difference… DefaultLitSpriteMaterial takes into account lighting used in the scene.  DefaultSpriteMaterial ignores lighting completely.  Unless you are using dynamic lighting, generally you will most likely want the DefaultSpriteMaterial.  You can edit the Material by double clicking:

image

 

This is the Material Editor and it is used to create node based materials.  Basically it’s a visual shader programming language, behind the scenes it ultimately is generating a GLSL or HLSL shader in the end.  Truth is the process is way beyond the scope of what I can cover here and in most cases you will be fine with the default shader.  If you do want to get in to advanced graphic effects, you will have to dive deeper into the Material Editor.

 

Creating a Sprite

 

Now that we have our texture and made a Sprite from it, it’s time to instance a Sprite.  That is, add one to our scene.  This is about as simple as it gets, simply drag a Sprite from the Content Browser to the Scene, like so:

 

g1

 

Now that you’ve created a Sprite, you will notice that there area  number of details you can set in the Details panel:

image

 

All sprites by default share the same source sprite and material, but you can override it on an instance by instance basis.  For example, if you wanted a single sprite to be lit and all the others to be unlit, you can change the Material Override on that single sprite.  Obviously using Details you can also set the sprites positioning information and some other settings we probably wont need for now.

 

 

Next up, we will look at sprite animation using a flipbook.

Programming , ,

19. April 2015

 

Today we are going to take a look at the PlayCanvas HTML5 3D Game engine.  While not strictly a tutorial, this guide should give you a pretty good idea of what the PlayCanvas Engine includes,if it is right for you and give you enough information to get you started.

 

There is also a video version of this tutorial available here.

 

First off, what exactly is the PlayCanvas Engine?  Well, let’s let them describe it in their own words:

 

PlayCanvas is the world’s easiest to use WebGL Game Engine. It’s free, it’s open source and it’s backed by amazing developer tools.



The amazing developer tools part isn’t hyperbole either, PlayCanvas ships with an impressive amount of polished and high quality tools.  Where it does get a bit confusing is where the open source part stops, and the pay to use it part starts.  

 

Getting Started

 

There are three ways to develop with PlayCanvas.  Download and run the tools locally, use the online tools and code using a Github or Bitbucket repository or, as I am going to do in this example, work entirely in their online hosted IDE.  It’s not a bad way to work but sadly is missing a few features, like code autocompletion. I would probably work in WebStorm using a Github repository if working on a production project instead of just playing around.

 

To get started with the online hosted tools, you need to create an account then log in to PlayCanvas.  You actually have to create two accounts, first is your over all account, the second is a developer account.  This is because PlayCanvas supports multiple concurrent developers working on and editing the same project.  It is however a premium feature.

 

Let’s talk about that for a second… what is premium and what is not?  PlayCanvas want to make money obviously, so let’s look at the tiers.  First off, the game engine is open source and available on Github.  However, not all of the tools like the content importer are.  I haven’t downloaded the engine locally, so I am unsure how much of an impact this has.  What is clear is the different tiers available.  As of today, the pricing looks like the following:

Pc2

 

There is a second tier entirely for organizations as well.

 

In a nutshell, the number one biggest drawback is going to be 0 private projects in the free version.  If you are developing a free and open source game, this is a non issue, but obviously if you are creating a private commercial game, you will need to create a private project.  Basically this means anyone can see your project, source, resources, etc with the free version.  The remaining differences boil down to disk space availability, number of team member accounts you can create and being able to export an iOS ready project.  Prices range from 15 - 60$ a month.

 

Everything you see today is from the free edition.  Other than iOS functionality, all versions contain the same feature set.

 

The Editor

 So once you’ve logged in and created a developer account, you should see a screen like this one:

Pc3

 

You can either create a new project from scratch, or start with one of the starter kits.  We are going to use a starter kit.  This is a pre-configured mini project to get you going. There are currently two starter projects available, a 3D model viewer and a platformer.  Let’s go with the later kit.  Since this is a free account, we have no options but to make the project Public.

 

Pc4

 

You will now be brought to the project management page.  

Pc5

 

Click the editor button and you will be brought to the guts of PlayCanvas, where you will be doing the majority of your work.  Click this button:

Pc5 5

 

The Editor

 

There are actually two editors currently available, the classic editor and a new beta editor.  You can toggle between them by selecting the Settings gear icon and choosing Use New Editor:

Pc6

 

Here are the two different editors.

 

New Beta Editor

Pc7

 

Classic Editor

Pc8

 

For this post I am going to stick with the standard production editor.

 

The layout has your scene graph/tree on the left hand side, your palette of assets across the bottom, and your properties/components across the right.  In the middle of course is your 3D Scene view.  If you prefer, you can switch to a traditional quad view mode:

Pc9

Obviously in the 3D view you can zoom, orbit and pan the camera, select and edit objects, etc.  You compose a scene by dragging it in from the Pack Explorer.   In addition to the Packs Explorer you see here, there is also an assets and a scripts one as well.

 

Press the Play icon and your game should start.  Below is an embed of the project ( after Publish from the Dashboard ):

 

 

Click the mouse to focus, used arrow keys to navigate and spacebar to jump.

 

So that’s creating an initial project… now let’s take a look at the developer experience.

 

Importing a 3D Model

 

This is always one of the most challenging aspects of working with a 3D engine… getting your content in it.  PlayCanvas makes it pretty simple, but I did run into some small hiccups.

 

First author your 3D object in your modelling app of choice.  Personally I am using Blender.  They supports FBX, COLLADA and 3DS formats, although FBX is highly recommended.  There are a few limits as well.  You need to embed textures in with your model ( or manually add them later, as we will see ), you have a 256 bone and 64K vertices limit per mesh.

 

I am going to create a very simple textured cube in Blender, like so:

Pc10

 

And then export as an FBX using the following settings, the important ones highlighted:

Pc11

 

Now the we have an FBX, we need to import it to PlayCanvas.  Next in the dashboard, go to the assets section and click New Asset:

Pc12

 

Then simply drag the FBX file from finder/explorer to the appropriate spot.

Pc13

 

Now if you look at the palette portion of the editor ( hit the refresh button top right corner of panel ) you should now see your object.  Simply drag one into the scene:

Pc14

 

And tada:

Pc15

 

In my case however, the texture is always missing.  I’ve tried different formatting options, going through FBX exporter and never had any luck.  It is however easily fixed.  Instead import your texture as an asset.  Then in 3D view, click your object so it is selected, click again so Material is selected in the Material Editor, select diffuse and assign your texture:

Pc16

 

I have to assume this is a bug, either in Blender’s FBX import, or the PlayCanvas Import, but it’s extremely simply fixed by manually assigning the material so no big deal.

 

Now you will see your object renders properly in the scene:

Pc17

 

Components

Like most modern popular engines, PlayCanvas is component based.  Basically you add functionality to game entities by attaching components.  Let’s take a look at the main character and how it’s composed.  This can be seen by selecting the Player in the Pack Explorer then looking at the Attribute editor:

Pc18

 

As you can see out Player is composed of a rigidbody, collision and script component.  Additionally you can see it’s got a child entity named model.  Model in turn has a animation and model component attached.  It is through this parenting and composition you make the objects that composed your game.

 

Adding a component to an entity is as simple as right clicking it and choosing Add Component:

Pc20a

 

You can similarly add a New Entity the same way, but instead choosing New Entity.

 

Pc20

 

You can of course create your own components and entities as well, but that goes way beyond what I intend to cover here.  You may have noticed the script component attached to the Player…  that segues nicely to...

 

Coding

As you probably expected, the coding language behind PlayCanvas is JavaScript.  Scripts are attached to entities as a component.  Here for example is code for input in the Player from the platform example:

// Player Input
// Handle input from a keyboard
// Requires:
// - The entity must also have the platform_character_controller script attached

pc.script.create('player_input', function (context) {
    var PlayerInput = function (entity) {
        this.entity = entity;
    };
    
    PlayerInput.LEFT = "left";
    PlayerInput.RIGHT = "right";
    PlayerInput.JUMP = "jump";
    
    PlayerInput.prototype = {
        initialize: function () {
            // Create a pc.input.Controller instance to handle input
            context.controller = new pc.input.Controller(document);
                    
            // Register all keyboard input
            context.controller.registerKeys(PlayerInput.LEFT, [pc.input.KEY_A, pc.input.KEY_Q, pc.input.KEY_LEFT])
            context.controller.registerKeys(PlayerInput.RIGHT, [pc.input.KEY_D, pc.input.KEY_RIGHT])
            context.controller.registerKeys(PlayerInput.JUMP, [pc.input.KEY_W, pc.input.KEY_SPACE, pc.input.KEY_UP])
    
            // Retrieve and store the script instance for the character controller
            this.playerScript = this.entity.script.platform_character_controller;
        },
        
        // Check for left, right or jump and send move commands to the controller script 
        update: function (dt) {
            if ( context.controller.isPressed(PlayerInput.LEFT) ) {
                this.playerScript.moveLeft();
            } else if ( context.controller.isPressed(PlayerInput.RIGHT) ) {
                this.playerScript.moveRight();
            }
            
            if (context.controller.wasPressed(PlayerInput.JUMP)) {
                this.playerScript.jump();
            }
        }
    };

   return PlayerInput;
});

 

As you can see, the code is also very modular in nature as well.  It’s clean and fairly well documented, we will get to that shortly.

 

As I mentioned earlier, there are a couple of approaches to code editing.  You can host your code in a Bitbucket or Github respiratory, edit using your editor of choice then sync it back.  Otherwise you can use the built-in code editor.  Let’s take a look at it:

Pc21

 

It has some of the features you would expect.  There is syntax highlighting, code folding and warning hints.  What however there is not is code completion… that’s a glaring missing feature IMHO.  It’s been requested and hopefully will be added soon.

 

The API is clean enough, the editor works, although for a large scale project you will most certainly want to use your own editor, I would certainly.  Although if they added intellisense/code competition, I would spend a lot more time in the web based editor instead of my own IDE.

 

Documentation

This is an area that can make or break a project.  I will say that PlayCanvas has very solid documentation.  As you saw above, there are two starter projects to get you going, a Platformer and 3D model viewer.

 

The documentation is entirely online including a User Manual, Reference and Tutorials:

Pc22

 

The documentation is clean, professional and for the most part as shown what I needed when I needed it.  The User Manual covers common concepts like importing models, etc…  but could certainly use a bit more depth.  There is a pretty solid collection of tutorials available:

Pc23

 

All told, documentation is a definite plus of this project.  Some quick links if you want to check it out:

User Guide

API Reference

Tutorials

 

The Video Version

 

 

Conclusion

 

PlayCanvas is an impressive 3D engine for those looking to work in HTML5.  The tools are comprehensive, the documentation is quite good, the pricing structure is reasonable and the code seems clean.  Let me know if this is an engine you want to see additional coverage of here on gamefromscratch.com.

Programming

15. April 2015

 

In the previous tutorial we created a new project but didn’t really go anywhere with it.  Instead we were focusing more on learning our way around the Unreal Engine interface.  By the way, with each tutorial I assume you are familiar with the subject matter of the tutorial that came before it.  This tutorial and the previous one combined contain basically the same content as this video tutorial version.  So if you are jumping right in and completely new to Unreal, I would suggest you start there.  If you are confused about something, try the video instead and you might find some clarity…

 

…alright, back to the subject at hand.  Our very first application.  At this point you should have a new “Blank” project created.  If you haven’t got one, head back to the Epic launcher and create one now. We are going to create a very simple application, basically a sprite backdrop, a camera, a blueprint and the text Hello World.  We will not go into much detail on using Sprites, as that is one of the next topics I will cover in much greater detail.

 

Getting Started

 

First, let’s clear up our scene, we want to start completely from scratch, and the “Blank” project isn’t completely blank.  If you look at the Outliner, you will see that a few default objects have been created in our scene.

image

 

We want to SHIFT + Click select them all and then hit the Delete key.  Now we’ve got a completely empty scene.  Let’s put something in it!

 

Adding a Sprite backdrop

 

Now we need to add an image to our scene.  I will cover this process in a bit more detail later on, so if you’ve got some questions, don’t worry, we will probably get to it later.  Just follow along for now.

The first thing we need to do is add a texture to our game.  A texture is simply an image file, although there are a few caveats.  First off, Unreal Engine really prefers textures with an alpha channel.  This means jpg and gif files are very poor format choices.  Additionally, Unreal Engine wants your images to be “power of 2” dimensions.  It’s not a strict requirement but its heavily encouraged.  This means your width or height are 2,4,8,16,32,64,128,256… 2048,4096, etc… pixels in size.

 

The image I used I simply got from Google Image search.  I searched for Game background, set size to 1024x512 like so:

image

 

This is the image I chose at random.  Obviously I am not the owner and you have zero redistribution rights to this image.  Pick or use whatever image you wish, it really doesn’t matter in the end.

 

Now that we have our texture, we need to add it to the game.  Let’s be a bit organized about this and create a folder for our textures.  In the Content Browser, right click Game and select New Folder and name it Textures:

image

 

Your end result should look like:

image

 

Now from Explorer/Finder, simply drag your image on top of the Textures folder in the Content Browser.

It should immediately appear on the right hand side if everything went well:

image

 

Now that we have a Texture, let’s create a Sprite from it.  Again I will cover this later, but think of the difference between a Texture and Sprite as follows.  A Texture is the image file itself, contains the pixel information, etc…  The Sprite is an instance of your texture, or even part of your texture.  It contains positional information, etc… but all the image information comes from the texture.  You can have one texture with many sprites instanced from it.  More importantly, a sprite can be dragged into our scene and a texture cannot!

 

Let’s create a Sprite, it’s super simple.  Simply right click the texture on the right hand side of the Content Browser and select “Create Sprite”.  As you can see, right click menus in the Content Browser are context sensitive, knowing what type of files they are dealing with and presenting different options accordingly.

image

 

This will create a new sprite in the same directory:

image

 

Let’s stay tidy here and create another folder off Game called Sprites.  Then simply drag our newly created sprite to that folder and select Move Here.  This of course is purely optional.  If you want all your stuff in one big mess of a folder, that’s your prerogative!

4

 

Now finally we can create our sprite.  This is as simple as dragging one into the scene!

First lets make sure we are in Front camera perspective:

image

 

Now let’s drag the sprite to the origin.  For the record, this is the origin… the point where the X and Y axis meet:

image

 

You may need to pan the camera around a bit to locate it.  Keep in mind, you hold down the right mouse button to pan the camera.  Now simply drag your sprite from the sprite folder so it’s centered about the origin:

5

 

Well… that doesn’t look very sprite like, does it?  That’s because we are in wireframe mode.  In your view drop down the view menu and instead select Unlit ( or press Alt + 3 ):

image

 

Now your texture should appear in the scene:

image

 

We could actually run our “game” right now.  To run your game, hit the play icon.

image

 

The result will probably look something like a black screen.  You can hold down the right mouse button to rotate the view, eventually you will probably see:

image

 

Yeah…. that’s not what we want.  By the way, hit ESC when you are done playing to return to the editor mode.

 

The problem here is we don’t have a camera defined so when we start we aren’t looking at our background.  There are a couple options here and I will show both.  There is a third option ( the best one ), a Character Controller… we will cover that later on!

 

Now first the easy option.

 

You can set the perspective view.  This is where the view points if there is no camera defined.  Switch instead from Front view to Perspective View:

image

 

Using the camera controls (Right click to orbit, scroll mouse to zoom, middle mouse to pan ) center your image in the view.  With the background sprite selected you can also press f or Right click and choose Focus Selected

image

 

With the image displayed in your 3D View:

image

 

The next time you hit play, you should see the same angle:

image

 

That said, this is useful only for development purposes… in reality what you want is a camera…

 

Creating a Camera

 

So, let’s create one, it’s stupidly easy!

 

First we need to find the Camera object.  In the Modes panel in Place mode, simply type Camera, then drag the resulting object on to the 3D view, somewhere near the origin.  ( I switched back to Front view before doing this, btw )

6

 

With the camera dragged onto the view, now position it somewhat around the origin like so:

image

 

Remember, you can move an object by selecting it, then hitting the W key.  You could also have modified it using the Transform settings in the Details panel:

image

 

Now switch to Top View ( Alt + J )

 

We now want to Rotate and Move our camera so its facing the X axis ( where our sprite is ) and out a little bit.  Remember you can move using W key and rotate using E.  We want it placed something like this:

image

 

You’ll notice as you move the camera, there is a preview window of what the camera currently sees.  Please note, as of right now, this preview only works when the camera is in perspective mode!  I assume this is a bug and will hopefully be fixed in the future.  For a 2D game you would generally switch it to Orthographic ( see below ), but until this bug is fixed, for development it’s often easier to work in perspective mode.


Different Camera Types
If you look at the Camera Settings in the Details panel you will notice there are two different settings for Projection, Perspective and Orthographic.  Which one you choose has a major impact on the options available and the way the camera performs.
image
 
Perspective Camera – the role of a camera is to project a 3D scene into 2D and there is a catch involved.  You see, as things get farther away from us they appear to get smaller.  Obviously though, this is an illusion, that BMW Z3 racing straight at you at 100mph is obviously not changing in size as it gets closer!  A perspective camera however models this behavior, as things get further way they are rendered smaller.  When creating a perspective camera you provide a field of view and an aspect ratio.  The field of view represents the number of degrees to the arc of what is visible to the camera.  The aspect ratio is the ratio of pixels of width to height.
Orthographic Camera – the orthographic camera renders things the same way regardless to their depth within the scene.  Instead of providing a field of view, you instead provide the width of the rectangle the camera should capture.  Generally an orthographic camera is used for 2D games, where you are mostly ignoring the depth component when rendering.

 

Now when we press play things must certainly work, right?  Well… no, not quite.  This is because our camera isn’t set as active.  For this we are going to create our first blueprint.

 

Creating a Blueprint

 

Now let’s create our first Blueprint, a very simple one that is going to set our camera as the active camera.


What is Blueprint?

It used to be that gameplay logic was scripted in UnrealScript while the engine was written and extended in C++.  Then in Unreal Engine 3 a new visual scripting language, Kismet was added.  In Unreal Engine 4, Kismet has been extended and renamed Blueprint while UnrealScript has been taken out to pasture and shot.  At the same time through much magic, C++ has been enabled as a scripting language.
So what exactly is Blueprint?  Well it’s a graph based scripting language where you program by connecting a variety of nodes together in a visual manner.   You drag events, functions, branches etc onto the canvas then connect them together to create programs. Impressively a Blueprint can inherit from a C++ class, and a C++ class can inherit from a Blueprint.  It’s a different way of programming, and certainly a boon for visual thinkers, but it can also be a bit messy for the more code focused developer. 

First we need to create a Blueprint for our level.  This fortunately is very simple, simply drop down the Blueprint menu in the menu bar and select Open Level Blueprint.  The level blueprint is a special blueprint that is available across the entire level.  Every level has a level blueprint, even if its empty.  Ok, let’s edit the level blueprint:

image

 

This will now open up the Blueprint code editor:

image

 

This is a free floating window used to create new blueprints.  As you can see, the over all look and feel is very similar to main Unreal Engine.  The Palette on the right contains a common list of node types that can be dragged onto the surface.  The My Blueprint window on the left has context sensetive information about your blueprint.  In many ways it is similar in functionality to the Details panel in the main interface.  The Event Graph is where you create your actual blueprint.

 

First thing we are going to need something to fire an event to get the ball rolling.  There are a number of events available, these are things that happen that can be responded to in your blueprint, like Tick ( called each frame ).  In our case however, we simply want to run our code when the game starts.  In this case we want to run our code just once when the level is loaded and ready.  The event we want is the Event Begin Play node.  You may notice its in the Palette on the right.  Simply locate the Event Begin Play node and drag it onto the the graph:

7

 

In addition to dragging the node from the Palette, you can also simply right quick and do a quick search for the node by typing it’s name like so:

8

 

In the long run, especially if you are a coder, you will probably find yourself using the second approach far more often…  both have the exact same result.

 

Ok, we now have an event to work with… let’s do something with it.  What we want to do is set the view to our active camera.  This is done with a function called “Set View Target with Blend”.  This is created the same way as shown above, with a small catch.  Simply right click the graph and type Set View and it should show in the list.  However, by default Blueprints try to guess the context you are working in to streamline the search results.  In this case however, we do not want it to do that!  Make sure the Context Sensitive checkbox is not checked, or it will not appear in the results:

image

 

Left click the Set View Target with Blend function and it will be added to the graph.

image

 

Looking at the left hand side of the function you can see that it takes a number of incoming connections, you can think of these like function parameters, although they are often completely optional.  If you hover over the connector you can see the type it expects:

 

image

 

Here you can see that it takes a parameter called Target of type object.  It also takes another parameter called New View Target, also of type object.  Basically these are the new target to use ( our camera ) and the object to update ( our view controller ).  So now we need both of them!

 

First off, lets bring in a reference to our camera.  Go back to the main Unreal Editor and make sure that your camera is selected in the Scene Outliner:

image

 

With the camera selected, right click the graph in the Blueprint editor and the option Add a Reference to CameraActor8 should be available.  This is a quick shortcut to add a reference to the selected scene object.

image

 

Now we want to get the Player Controller.  This is a special class within the game, there is always at least one.  We will actually cover this class in a bit more detail shortly when we create our own.  For now just realize it’s this class that controls the active camera.

image

 

Now our graph should look something like this:

image

 

It’s probably important to note at this point, you can arrange things however you wish on the graph.  Simply left click and drag a node to relocate it.  You can also use the scroll wheel to zoom in and out in the blueprint editor.

 

It’s also worth noting that Nodes are colour coded and these colours have a meaning.  A red node is a starting point for execution of an event.  A Green node is generally a function that “gets” something.  A blue node is a function or event ( if it’s a function there is an F in the top left corner ) that takes input and may or may not have output.

 

So at this stage we now have an event that is going to fire when the game starts playing, we have a function we want to call that sets the view of the selected object, and we have two object references, our camera and our player controller.  Everything we need, now we just have to wire them up!

 

You may notice that the Event Begin Play node has a white play icon on it’s left.  This means that this node executes something.  You may then notice that the Set View Target with Blend has the same icon on it’s left and right.  The left icon indicates that it can be executed, while the right one means it can execute something else.  Connecting these nodes together is how you make your program do something.

 

At the same time you may notice the Get Player Controller and CameraActor8 nodes both have a blue return value connector on their right hand side.  If you are a programmer, you can think of this as the return value from that node.  As mentioned earlier, the Set View Target with Blend function has a number of connectors down the left, these are the inputs it accepts.  As you can see, they are nicely colour coded, so you can see (by default) what works with what.  Now its simply a matter of wiring everything up.  Simple drag the click and drag between pins you wish to connect like so:

9

 

When you make a new Blueprint, or make changes to an existing one, you have to compile, you can tell by the ? mark over the compile button:

image

 

Assumiing everything went ok, you should now see:

image

 

If you want to test the program flow of your new Blueprint, you can hit the Simulate button and you can see how data will be changed as the program executes.

 

Hit the simulate button:

image

 

And you should see:

10

 

Obviously in much more complicated workflows, this simulation is much more useful.

 

Now when we run out game, our camera will be set active when the game starts.  Congratulations, you just created your first blueprint!

 

Click Play and you should see:

image

 

Adding the Hello to Hello World

 

We are almost to the end.  One final thing we need to finish our application is well… Hello World.

 

Adding text to Unreal is extremely easy.  In the Modes panel, enter “text” in the search field and select Text Render:

image

 

Drag one into your scene.  Then in the details, set the Text to Hello World, like so:

image

 

Your scene should now look something like this:

image

 

Now when you run it:

image

 

Congratulations!  We have now finished our first ever Unreal Engine application!

 

There is one last very important thing to discuss before we finish here.  When you added the Hello World text to your game, there is a very good possibility that it didn’t show up when you press play.  This is because of the drawing order in your 2D game.  While 2D games don’t have depth, they do have drawing order and this is important to determine what is drawn on top of what.  In this case the Hello World text needs to be closer to the camera than the background, or the background will be drawn over top of it.  It’s probably easiest to understand this if we look at the scene in 3D:

image

In practical terms, this means that things that need to be drawn over other things need to have a higher value on the Y axis.  In this case our background image is drawn at y=0, while the Hello World text is drawn at Y=70.

Which way is up?

This is an important and difficult to understand concept about Unreal engine, especially when working in 2D.  You are probably used to X being left/right and Y being up/down, but that isn’t the way it works in Unreal, and that is because Unreal is a 3D engine and chose a Z-up coordinate system.  This means that Z represents up and down on your screen, and Y represents depth or in/out if you could put your hand into the screen.  Different applications use different coordinate systems and converting between them is common place.  Often it’s simply a matter or rotating things 90 degrees.

 

Conclusion

 

We covered a fair bit there, I hope that was useful.  We however did not cover Character Controllers, and the reality is, you generally will create a character controller which controls the camera.  We also didn’t cover game modes, we will be covering both of these topics shortly!  So while we didn’t do anything “wrong” in this example, keep in mind you generally will do things slightly differently in the end.

 

Finally, if when you press play you are seeing a circle drawn in front of your scene, don’t worry, this is the default pawn being drawn, another topic we will discuss shortly, and something that is easily removed.

Programming , ,

Month List

DisqusCommentsSummary