CodeSim - A Game controlled by Code - DevLog - Part 1
Tags: unity3d, gamedev, devlog
Published: 2020-01-03 11:14:43

# 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](https://colobot.info/). 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:

```python
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](https://en.wikipedia.org/wiki/Logo_(programming_language))

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](https://assetstore.unity.com/packages/3d/environments/urban/simple-town-cartoon-assets-43500) 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](https://thepracticaldev.s3.amazonaws.com/i/yxvuqi16q2drn41hwlrh.png)

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/oy5k98c3t35e10374ik3.png)

# 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](https://thepracticaldev.s3.amazonaws.com/i/k0nh5zvptrdeoaednwyn.png)


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](https://thepracticaldev.s3.amazonaws.com/i/a9utwqxh2yvz3j2q0kxl.gif)

Successfully reaching the end goal looks something like this: 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/z1exnj9387nkq84h832s.gif)


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.