# First Steps

Now that you’ve covered the essentials, it’s time to start scripting.

### Fire up a text editor﻿

Feel free to use any text editor you prefer: [Visual Studio Code](https://code.visualstudio.com/), [Notepad++](https://notepad-plus-plus.org/downloads/), or even a simple Notepad.

Local scripts are located here: `<CS2_Directory>/game/csgo/fatality/scripts`. You may notice there's also a `lib` directory, but we’ll get to that later.

Create a new file ending with `.lua`, and begin your work on the script.<br>

{% hint style="info" %}
`.ljbc` format cannot be loaded from local sources.
{% endhint %}

### Writing your first script﻿ <a href="#writing-your-first-script" id="writing-your-first-script"></a>

A typical “Hello world!” example can be a bit trivial, so let’s try something slightly more advanced instead.

```lua
local function onPresentQueue()
    local d = draw.surface;
    d.font = draw.fonts['gui_main'];
    d:AddText(draw.Vec2(50, 50),
        'Hello drawing! My first script speaking.',
         draw.Color.White()
    );
end

events.presentQueue:Add(onPresentQueue);
```

Now, let's break down this example script:

### Defining a callback function﻿ <a href="#defining-a-callback-function" id="defining-a-callback-function"></a>

Most of your scripting will run within [several callbacks](/api/instances/events.md) we provide. Each event has its own signature, so pay attention to the parameters your callback function should accept. `presentQueue` doesn’t provide any parameters, so our function doesn’t need any either.

```lua
local function onPresentQueue()
end
```

{% hint style="info" %}
Defining something `local` is optional, although recommended for clearer scope management.
{% endhint %}

### Accessing drawing layer﻿ <a href="#accessing-drawing-layer" id="accessing-drawing-layer"></a>

With the callback function defined, let’s actually render something on the screen!

To do this, you first need to access the [drawing layer](/api/instances/draw.md). We provide a single drawing layer that’s safe to use within the game thread. Due to how the game functions internally, it’s strongly discouraged to call game functions in other threads. Luckily all of our events run in the game thread.

This setup allows you not only to draw but also to query information on player health or other entities.

To access the layer, simply reference the `surface` field in the `draw` table:

```lua
local d = draw.surface;
```

{% hint style="info" %}
You don't have to store it in a variable, but it would be nicer if you don't have to type out `draw.surface` every time, right?
{% endhint %}

#### Setting a font﻿ <a href="#setting-a-font" id="setting-a-font"></a>

After retrieving the layer, you must set a font object before drawing any text on the screen. This is purely for performance reasons, so you don’t have to pass a heavy font object every time you draw text.

All fonts are stored in [`draw.fonts`](/api/instances/draw.md#fonts). To access a font, either use dot syntax, or treat it like a dictionary:

```lua
d.font = draw.fonts['gui_main'];
```

#### Drawing text﻿ <a href="#drawing-text" id="drawing-text"></a>

With the font set, it’s time to draw some text.

Invoke the [`AddText`](/api/instances/draw/layer.md#addtext) method on the layer. Notice that it’s called using the colon syntax: `obj:fn()`, because it’s a method.

{% hint style="info" %}
You can also invoke methods with a dot syntax, as long as you provide the object in the first argument. Both calls: `obj:fn()` and `obj.fn(obj)` are identical.
{% endhint %}

```lua
d:AddText(draw.Vec2(50, 50),
  'Hello drawing! My first script speaking brev.',
  draw.Color.White()
);
```

#### Registering a callback﻿ <a href="#registering-a-callback" id="registering-a-callback"></a>

Now that you’ve created your first callback, you need to register it so Fatality knows to invoke it. This is done by calling the [`Add`](/api/instances/events/event_t.md#add) method on [`events.presentQueue`](/api/instances/events.md#presentqueue).

```lua
events.presentQueue:add(onPresentQueue);
```

### Result﻿ <a href="#result" id="result"></a>

That's it! If you've done everything correctly, you should see something like this:

<div align="left" data-full-width="false"><figure><img src="/files/wlmxN1E53cdoljusJZ6v" alt=""><figcaption><p>My first lua</p></figcaption></figure></div>

<figure><img src="https://lua.fatality.win/images/fs.png" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lua2.fatality.win/introduction/creating-scripts/first-steps.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
