DEV Community

loading...
Cover image for CodeSim - A Game controlled by Code - DevLog - Part 1

CodeSim - A Game controlled by Code - DevLog - Part 1

tomvdb profile image Tom Van den Bon ãƒģ4 min read

Game Concept

I love games in which you can code. Be it something simple as a pseudo language or a game that has a lua interpreter embedded into it. A nice example is Colobot. I spent many hours on it.

Recently I starting playing with Unity with the idea of using it for a couple of work projects. Not so much games, but rather interesting interfaces to production equipment. While learning how to use Unity I got the idea that I can easily make my own game that uses code to control it. Thus the concept of CodeSim (temporary name) started.

The basic concept is that you control a object, player, etc in the game using an outside program. The program that controls the game needs to be separate from the game/simulation itself. This allows the player to use his preferred programming language and environment.

I started with this simple example of how you would control the vehicle in CodeSim:

import sys
import codevehicle

codevehicle.ConnectVehicle()
codevehicle.Reset()

try:
    codevehicle.TurnLeft()
    codevehicle.MoveForward(3)
    codevehicle.TurnRight()
    codevehicle.MoveForward(15)
    codevehicle.TurnRight()
    codevehicle.MoveForward(2)
except Exception as e:
    print("Program Failed, Reason: " + str(e))

print("Done")

The core concept is basically similar to Logo

Very basic of-course, but if the underlying game engine could be controlled this way then you can start creating all kinds of fun things/challenges in the game.

Required Game Features

  • A simple game world with a robot/vehicle controlled by the player
  • Some kind of api interface that is simple enough that almost every language can talk to it
  • Real time update of the world as the program runs with a goal to reach, or indicating failure

I'm sure there will be many more features in the future, but for the concept to work I had to have atleast those 3 items

Game World

Unity makes it pretty easy to create a gameworld, I decided on a 3d game world that is simple and I found some nice 3d assets to use in this proof of concept.

Our vehicle started out with simple 3d model of a car and some c# scripts that gave us functions to move it in a turtle like fashion, ie (MoveForward, TurnLeft, etc)

Alt Text

For the first couple of tests I made a basic "end goal" gameobject and some obstacles it needs to avoid.

Alt Text

Game API

To allow our player to control the vehicle in the game we need to create an api of sorts. I decided that sockets are the way to go. When our game starts up it listens on port 13000 on the localhost and waits for any incoming connections. Our current list of commands are:

  • MoveForward(amount of units to move)
    • move the vehicle forward and specify the amount of units to move
  • TurnLeft
    • makes a 90 degree turn to the left
  • TurnRight
    • makes a 90 degree turn to the right
  • Reset
    • Reset the vehicle to starting point

These commands are sent in plaintext to the socket server in the game. To move the vehicle forward 50 units we send the following text

forward,50

The game will respond with one of the following:

OK             # command successfully executed
COMPLETED      # goal block reached (ie. end of level)
DISABLED       # vehicle collided against something and is disabled (required reset)

A simple command set, implemented in a simple way, but enough to test the concept

Watch your program run!

As a first test I implemented a simple python module that will send the commands to the socket server. Using Visual Studio Code is very simple to run the program and see the results in the game:

Alt Text

When your vehicle hits an obstacle it will go in disabled state and need to be reset again before you can send more commands to it:

Alt Text

Successfully reaching the end goal looks something like this:

Alt Text

And with that we have a working proof of concept of a game that can be controlled from your own code outside the game. This is nothing group breaking and other similar games exist. But this a fun way for me to learn to use unity :)

Next Steps

With the basic core working I'm keen to expand on it further. For the next update I'm planning on creating some proper levels and add some more interactions. Maybe a radar that lets you detect obstacles, or action buttons you can drive over the make things happen.

Let me know in the comments if you have other ideas that would work nicely with this concept.

Discussion (0)

pic
Editor guide