Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/angelscript/game/meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Game",
"type": "angelscript",
"weight": 0
"weight": 1
}
110 changes: 110 additions & 0 deletions docs/angelscript/guide/chapter1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Chapter 1 - Introduction
weight: 0
---

# Chapter 1 - Introduction

## What will you learn in this chapter
In this chapter you will learn about:
- [AngelScript as a programming language](#angelscript),
- [Purpose of AngelScript in Strata Source](#what-can-you-do-with-angelscript),
- [Client - Server model of the engine](#client---server-model),
- [How to load code in the game](#loading-code),
- [Writing your own Hello World program](#your-first-script),
- [Testing out your own code](#how-to-test-out-your-code-in-a-basic-way)
- [Additional tips that might help you](#additional-tips).

> [!TIP]
> In each chapter, you can easily navigate the page by clicking links in the "What will you learn in this chapter" section.

> [!CAUTION]
> This guide assumes you have basic skills of programming in languages like Python, C/C++, Squirrel (VScript), etc.
> It is recommended you already have *some* experience in programming, although this guide aims to be as begineer's friendly as possible.
> Basic concepts will **not** be taught.

> [!NOTE]
> It is recommended that while reading this guide, you will try out the things you have learned. This guide will include example tasks for you to do as a practice.

---

## AngelScript
The [AngelScript's home page](https://www.angelcode.com/angelscript/) describes AngelScript as:
> The AngelCode Scripting Library, or AngelScript as it is also known, is an extremely flexible cross-platform scripting library designed to allow applications to extend their functionality through external scripts.

Besides that, you can treat AngelScript as some sort of hybrid between C++ and Python. In some areas it behaves like C++, for example it is statically typed, meaning that when you are declaring a variable you also have to declare it's types, and it also implements it's own version of pointers (called handles); it also aims to help out users in writing code, whether by dissallowing certain operations or by assuming. More on that will be explained in later parts of the guide.

Its use cases vary, it is much closer to pure engine code than VScript, meaning that you can for example program in custom entities, or custom commands.

Its official documentation can be found here: [AS Official Docs](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_script.html).


## What can you do with AngelScript
This question is not properly asked, because AngelScript will allow you to do mostly anything you want, however it's main task in Strata Source is to allow users to create custom entities, create custom commands, bind to in-game events, and more.

While VScript (mainly) sits between entities and handles the interactions between them, AngelScripts sits in a level above, where AS can be treated as entities *themselves*.

---

## AngelScript in Strata Source

### Client - Server model
Before we get into writing your first script, there is one more thing you need to know. Most engines including Source in general, operate on the client-server model. Meaning that client code is separate from the server code, even on singleplayer. When you start a map in singleplayer you essentially create a one-player server that runs beside the client. This is very important because AS code can be loaded on both. Some functionality will only be available on the server (like entities) and some functionality will only be available on the client (like Panorama/UI).


### Loading code
Your first question might be: Where do I place the files containing my code?
The answer is pretty simple, each file that contains your code should be ended with the **.as** extension and be placed in the **code/** folder of a respective `Game` searchpath. Example locations would be `p2ce/custom/my-addon/code/<files>` or just `p2ce/code/<files>` (the latter not being recommended).

You can name your files however you'd like. You can create custom directories, you can loosely place files, it all depends on what you're trying to achieve; in the long run it doesn't matter. What does matter are the "starting points" of sorts. The engine will not attempt to load any files besides 3 files placed **directly** in the **code/** folder:

1. `init.as` - Will get loaded by server and client.
2. `sv_init.as` - Will only get loaded by the server.
3. `cl_init.as` - Will only get loaded by the client.

### IDE and testing environment
It is suggested to use Visual Studio Code with the [AngelScript Language Server (sashi0034.angel-lsp)](https://marketplace.visualstudio.com/items?itemName=sashi0034.angel-lsp) extension. From there you can open the `code/` folder of your choice as a project and develop there.
The engine compiles scripts on every map load (you can use the `reload` command to recompile the scripts).

### Your first script
Now, you should be ready to write your very first and own program that will print a Hello World message to the console. You might not know everything in the code below but don't get dissappointed! You should place this code into `cl_init.as` as it is a client command.

```cpp
[ClientCommand("HelloWorld", "")]
void MyCommand(const CommandArgs@ args) {
Msg("Hello world from AngelScript!\n"); // You can place your own text here!
}
```

Now, the only thing left is to launch up the game, open the console and execute the *HelloWorld* command.

> ### TASK 1:
> Run the HelloWorld program mentioned above.

## How to test out your code in a basic way
For now you need to know how to run your code so that you will be able to solve tasks given to you with this guide.
In `sv_init.as` include:
```cpp
[ServerCommand("CodeTest", "")]
void CodeTest(const Command@ args) {
// Here you can put your code
}
```

The code in this function will run whenever you run the `CodeTest` command in game. Remember to `reload` to see the changes!
The `Msg(string)` function will serve you as a way to view your variables (like print or cout), just do `Msg(a)` where a is your variable and a will get printed to the console!
Remember to add `"\n"` to the input of Msg (or just call `Msg("\n");` after your message), otherwise everything will print in one line!

> [!BUG]
> Some types such as `int` cannot be directly converted to string, and so you won't be able to put them directly into Msg().
> > [!TIP]
> > In order to avoid that problem you can append to an empty string, just do `"" + a` and in most cases this will work: `Msg("" + a);`

### Compilation errors
Most of times scripts will report errors before they are ran, on map load. This is why if you don't see your functionality (like when a command is not there in the console), scroll up and check the error. Additionally you can use the first tip in the [tip section](#additional-tips) and then use `reload`.


## Additional Tips

> [!TIP]
> Reading console output can be tiresome as much more is happening in the console than just the script system. However, there is an easy way to just listen to the script system output. You can run `con_filter_text scriptsys; con_filter_enable 1` filter out anything that is not the script system.
116 changes: 116 additions & 0 deletions docs/angelscript/guide/chapter2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
title: Chapter 2 - Value Types, Declaration & Assignment
weight: 1
---

# Chapter 2 - Value Types, Declaration & Assignment

## What will you learn in this chapter
In this chapter you will learn about:
- [Value Types](#value-types),
- [Declaration and assignment of value types](#value-types),
- [Auto keyword](#auto-keyword),
- [Constants and the const keyword](#constants),
- [Integer size reference table](#integer-size-reference-table).

> Unfortunately, in this chapter you won't learn anything really interesting, but this knowledge is crucial to continue further. Data types in general are a very extensive subject, but you don't need to know everything. This chapter is supposed to teach you how to handle value types in your script.

> [!NOTE]
> This chapter won't cover every detail about any of data types, it is recommended you visit the [Data Types Section](../game/type) of the wiki for more information.
> Alternatively, you can find references on the [AS Official Documentation](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_datatypes.html), however please note that Strata's wiki will be the most representative, some functionality might have been changed!

---

## Value Types
Value types are the more "primitive" types, and are only implemented in the backend by the Strata Team inside the engine itself. These types include: `int`, `string`, `bool`, `float`, `double`, etc.

> [!WARNING]
> It is assumed you already know about these data types from other languages (mainly C++). This subsection will only provide information relevant to AngelScript itself.

### Declaration and assignment
Value types can easily get assigned and can be passed by value to functions (more on that later).
To create a value type you usually perform a declaration and an assignment, or both at once:
```cpp
int myInt; // Declaration
myInt = 20; // Assignment

string myString = "Hey!"; // Initialization
```

You can declare multiple variables of the same type at once:
```cpp
int myInt1, myInt2, myInt3;
```

Once declared, variables cannot change their type without redeclaration. This is not allowed:
```cpp
int myInt;
myInt = 3.2; // myInt is of type int, not float/double!
```

> ### TASK 1:
> 1. Create a program that will declare and assign variables of types `string`, `int`, `bool`, `double`, and then print them out to the console.
> 2. Do the same but use variable initialization.

### Auto keyword
Although not recommended, the `auto` keyword will make the compiler automatically determine the data type of the variable:
```cpp
auto i = 1; // Will set type of i to integer
auto s = "My string"; // Will set type s to string
auto var = functionThatWillReturnAnObjectWithAVeryLongName();

// Handles (described in later chapters) can also be declared with auto
auto@ handle = @obj;
```

The `auto` keyword is not recommended for several cases. The main one of them is that you cannot immediately see the data type of a returned object especially from functions, like the one above. We don't know what that function will return. Another reason is that sometimes the compiler might guess wrong, especially in cases like integers, where you have multiple ways that `1` could have been described (e.g. int8/int16, both can describe `1`, even `bool` can).

---

### Constants
Constant variables are variables that cannot change over the lifetime of the [variable scope](chapter3) they are created in.
You can define a constant variable using the `const` keyword:
```cpp
const int size = 31;
const auto st = "string"; // const also works with the auto keyword
```

Constants can be useful as a sort of configuration of the script itself. If you reuse a statically defined value you can instead define a global constant and then changing one value will change everything at once:
```cpp
const int MAX_SIZE = 16;

string mystring = "lorem ipsum";
my_func1(mystring, MAX_SIZE); // A function that does something with mystring, but also needs to have additional information
my_func2(mystring, MAX_SIZE) // Another function that does something else with mystring, but it also needs the same additional information
```

Constants are also a way to optimize your code. If you know that a variable won't change (or shouldn't change) after it's initialization, always make it a constant.
```cpp
bool function(string s, float i) {
const float value = s.length() - i;
return i > value;
}
```

> ### TASK 2:
> Write a program that initializes a constant variable with the `auto` keyword, and then tries to change it after. Observe the compilation error in the console.

---

### Integer size reference table
The table below shows the minimum and maximum values for each integer subtype (don't worry about remembering this, just remember that it exists here):
|Type|Short description|Minimum Value|Maximum Value|
|---|---|---|---|
|int8| Signed, 8 bits |-128 | 127 |
|int16| Signed, 16 bits |-32,768 | 32,767 |
|int| Signed, 32 bits |-2,147,483,648 | 2,147,483,647 |
|int64| Signed, 64 bits |-9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
|uint8| Unsigned, 8 bits, also represents characters (char) | 0 | 255 |
|uint16| Unsigned, 16 bits | 0 | 65,535 |
|uint| Unsigned, 32 bits | 0 | 4,294,967,295 |
|uint64| Unsigned, 64 bits | 0 | 18,446,744,073,709,551,615 |

> [!TIP]
> The official AngelScript documentation mentions that the scripting engine has been mostly optimized for 32 bit datatypes (int/uint). Using these is recommended for the most part (unless you are dealing with numbers that don't fit into int/uint).


Loading