# UnrealEnginePython Tutorial

The following tutorial demonstrates the power of UnrealEnginePython to manipulate objects in the running game with Python, i.e. `import unreal_engine`. For more tutorials on how to use UnrealEnginePython (UEPy) see [their docs](https://github.com/deepdrive/UnrealEnginePython/blob/master/README.md).

## Hello world

Here, we'll show how to print to the Unreal Logs, get the ego vehicle (your car), and move the ego vehicle around.

Find your sim binaries by opening up `~/Deepdrive` and sorting by date

![Find sim bin](https://3039641439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lf1Rq6VQn8vh1N1AVDz%2F-LtpYxlly72DZOwIVKwT%2F-LtpYzULcI9Pg-lQHXFt%2Ffind-sim-bin.png?generation=1573930400816168\&alt=media)

Drill down to the Deepdrive binary

![Drill down](https://3039641439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lf1Rq6VQn8vh1N1AVDz%2F-LtpYxlly72DZOwIVKwT%2F-LtpYzUNOXmNgTSDzuhC%2Fsim-bin-drill-down.png?generation=1573930400725975\&alt=media)

Open a terminal, drag the file into the terminal, and press enter to open the sim in the terminal and see the logs.

![Terminal open sim](https://3039641439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lf1Rq6VQn8vh1N1AVDz%2F-LtpYxlly72DZOwIVKwT%2F-LtpYzUPV_quXkLf1RSY%2Fterminal-open-sim.png?generation=1573930400714681\&alt=media)

Once the sim is open and you see the, press `M` to drive the car manually.

Within your binary folder, i.e. something like `deepdrive-sim-linux-3.0.20190528000032` open your `DeepDrive/Content/Scripts` in your favorite editor and create a new Python file named `move_car_tutorial.py` and enter:

```python
print('hello world')
```

![Editor open](https://3039641439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lf1Rq6VQn8vh1N1AVDz%2F-LtpYxlly72DZOwIVKwT%2F-LtpYzURxB34Jsq712i4%2Feditor-open.png?generation=1573930400867348\&alt=media)

Now hit the backtick `` ` `` key to open the Unreal Console and enter

```
py.exec move_car_tutorial.py
```

![Unreal Console Hello World](https://3039641439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lf1Rq6VQn8vh1N1AVDz%2F-LtpYxlly72DZOwIVKwT%2F-LtpYzUTqPtdPUI_w9fu%2Funreal-console-hello-world.png?generation=1573930400932831\&alt=media)

You should then see "hello world" printed to the logs in the terminal:

![Terminal Hello World](https://3039641439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lf1Rq6VQn8vh1N1AVDz%2F-LtpYxlly72DZOwIVKwT%2F-LtpYzUV4UIKIVc8sX9C%2Fterminal-hello-world.png?generation=1573930400649943\&alt=media)

## Get and set the ego's position

Now you know how to run Python within Unreal Engine. Let's do something more interesting!

Paste the following into `move_car_tutorial.py`

```python
import json
from api_methods import Api


api = Api()
ego = api.get_ego_agent()
location = ego.get_actor_location()
ego.set_actor_location(location.x, location.y, location.z + 1e4)  # +100m
```

Now open the Unreal Console again in the simulator with `` ` `` and hit the up arrow to rerun the previous command which should be

```
py.exec move_car_tutorial.py
```

Happy flying!!!

## Introspecting the Unreal API

Often when developing a UEPy script, you won't know the exact name of the object you want to manipulate. Some general methods for introspecting the games internal state are demonstrated with the following script

```python
import json
from api_methods import Api, best_effort_serialize


api = Api()
ego = api.get_ego_agent()

print('Methods -------------------------------')
for m in dir(ego):
    print(m)

print('Properties --------------------------- ')
print(json.dumps(best_effort_serialize(ego, levels=5), indent=2))
```

[Example output](https://gist.githubusercontent.com/crizCraig/b9f9f86dc404a5658a85328e490d585e/raw/111e2e717d06ccd928683a782d7a70009a785a62/gistfile1.txt)

Here you can see the wealth of functionality and information UnrealEnginePython provides, including the `get_actor_location` and `set_actor_location` methods we've just demonstrated. Imagine learning the game's state information with the [input remapping trick](https://arxiv.org/abs/1504.00702)!
