Bare minimum example of how to use input to control entities for games #714
BrentFarris
started this conversation in
Guides
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
This is going to be a guide, from scratch, on how to get inputs/controls working in a game. We'll start from the very beginning of creating a project, up through the code for controlling a 2D sprite. This input guide will translate to 3D with minimal effort.
Create a project
Start by setting a name for your project and selecting the folder you want to create it in.
Importing content
Follow the on-screen instructions to import an image file. Start by clicking on the "Content" tab.

Next, click on the "Import content..." button.

After finding the image you want to import from your computer and importing it. You should see it in the content browser.

Putting the sprite on the stage
Go back to the "Stage" tab and then select the "2D" button at the top of the viewport to switch to the 2D view.

Next, drag/drop your image into the view port from the content list at the bottom.

Next, select "Create" from the menu bar, then select "Entity Data"

Next, fill in the name and select the path (in the project) you'd like to save the code.

The code
This should automatically launch your code editor, if not, navigate to the file and open it up in your code editor. You should see something like the following:
Please be sure to review the full source code comments (at the bottom of this section) for a complete picture.
Let's start by adding a
Speedfield to theTestMovestruct. Note that this MUST be a POD (plain ol' data)int,bool,string,float32, etc.. Note that you can also use structures with only those POD types. Don't put pointers or other non-compound simple types in here.After that, we'll add an update to our host Updater. The Updater runs any update functions registered to it in parallel (across threads). Every time you add an update, it will return the handle to that update. This handle will allow you to cancel the update in the future, should you wish to. We do just that when the entity is destroyed by registering to the
OnDestroyevent of the entity.Be careful in the future when creating functions that capture state "willy-nilly" in the future, you may want to be passing weak pointers instead.
Next, we'll setup our update function that is called every frame. Note that you can get the mouse, controller, touch, and stylus the same way we get the keyboard here.
Below is the complete code example:
Setting up the
mainstageReturn to the editor and click "File" then click "Save stage"

You can call your stage whatever you'd like, I tend to call my entry point stage

main.Next, select your entity on the stage and then go to the properties panel on the right. Scroll down to the search box at the bottom to search for the entity data we just created. Click on the entity data to attach it.

Set the speed to something like

10, then create a camera by clicking on "Create" in the menu bar, and selecting "Camera".Update your camera to ensure that it's at the right position, rotation, scale, is orthographic, and is the primary camera.

Testing
Now click "File" and then "Build and run game" to compile and start the game.

At this point, a window should open up, you should see your image, and you should be able to move it around with the W/A/S/D keys.
All reactions