-
Notifications
You must be signed in to change notification settings - Fork 21
Chapters 2 and 3 grammar reformatting #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: as-guide
Are you sure you want to change the base?
Changes from all commits
9051934
118eadb
e3ed54f
d6cdfcf
019183f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,23 +13,23 @@ In this chapter you will learn about: | |
| - [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. | ||
| > The knowledge in this chapter is crucial to continue further. Data types are an extensive subject, but you don't need to know everything. This chapter's goal is 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! | ||
| > This chapter won't cover every detail of every data type. 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). Please note, however, that the Strata Wiki is a better reference, as some functionality may have been changed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "however, that" can be removed. |
||
|
|
||
| --- | ||
|
|
||
| ## 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. | ||
| Value types are a more "primitive" type, and are only implemented in the backend 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: | ||
| ### Declaration and Assignment | ||
| Value types can be easily assigned and can passed by value to functions. We'll get into that more later. | ||
| To create a value type, you will usually perform a declaration, an assignment, or both at once: | ||
| ```cpp | ||
| int myInt; // Declaration | ||
| myInt = 20; // Assignment | ||
|
|
@@ -42,17 +42,17 @@ You can declare multiple variables of the same type at once: | |
| int myInt1, myInt2, myInt3; | ||
| ``` | ||
|
|
||
| Once declared, variables cannot change their type without redeclaration. This is not allowed: | ||
| Once declared, variables cannot change their type without redeclaration. This, for example, is not allowed: | ||
| ```cpp | ||
| int myInt; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these two lines can be combined |
||
| 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. | ||
| > 2. Do the same using variable initialization. | ||
|
|
||
| ### Auto keyword | ||
| ### 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 | ||
|
|
@@ -63,7 +63,7 @@ auto var = functionThatWillReturnAnObjectWithAVeryLongName(); | |
| 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). | ||
| The `auto` keyword is not recommended in many cases. Using `auto`, you cannot immediately see the data type of a returned object, especially from functions such the one above. We don't know what that function will return. Additionally, there is always a chance the compiler may guess incorrectly. This issue is especially prominent in cases such as integers, where there are multiple ways `1` could be described (e.g. int8/int16, both can describe `1`, as can `bool`). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Instead, do "When using |
||
|
|
||
| --- | ||
|
|
||
|
|
@@ -75,7 +75,7 @@ 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: | ||
| Constants can be useful as a sort of configuration of the script itself. Instead of reusing a statically-defined variable, you can define a global constant in its place. Changing one value will then change everything at once: | ||
| ```cpp | ||
| const int MAX_SIZE = 16; | ||
|
|
||
|
|
@@ -84,7 +84,7 @@ my_func1(mystring, MAX_SIZE); // A function that does something with mystring, b | |
| 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. | ||
| Constants also function as a way to optimize your code. If you know that a variable won't change (or shouldn't change) after its initialization, you should always make it a constant. | ||
| ```cpp | ||
| bool function(string s, float i) { | ||
| const float value = s.length() - i; | ||
|
|
@@ -97,8 +97,8 @@ bool function(string s, float i) { | |
|
|
||
| --- | ||
|
|
||
| ### 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): | ||
| ### Integer Size Reference Table | ||
| The table below shows the minimum and maximum values for each integer subtype. It is not necessary to memorize this chart as you can always refer back to this page when necessary. | ||
| |Type|Short description|Minimum Value|Maximum Value| | ||
| |---|---|---|---| | ||
| |int8| Signed, 8 bits |-128 | 127 | | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,44 +17,44 @@ In this chapter you will learn about: | |
| > Statements, expressons, conditions and the variable scope are the foundation of every program or script. | ||
| --- | ||
|
|
||
| ## Basic statements | ||
| ## Basic Statements | ||
|
|
||
| Your code is like a recipe, and statements are the individual steps. | ||
| If your code is a recipe, the statements are the individual steps. | ||
|
|
||
| Statements are a way to tell the script engine what it needs to do, we already used them in previous chapters, such as the assignment or declaration. | ||
| Statements are a way to tell the script engine what it needs to do. We've already used statements in previous chapters, such as when we performed assignment and declaration. | ||
|
|
||
| ### Expression statements | ||
| ### Expression Statements | ||
| Any expression can be placed on a line as a statement. Examples of expressions include: | ||
| - Function call **()** operator - calls a function (more on functions later), | ||
| - Equals **=** operator - performs assignment, | ||
| - Add **+** operator - adds two values together, | ||
| - Substraction **-** operator - substracts two values from each other, | ||
| - Subtraction **-** operator - subtracts two values from each other, | ||
| - Many more, such as **+=**, **-=**, **++**, etc. are available. More about them can be found in the [expression reference table](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_expressions.html). | ||
|
|
||
| Such statements need to end with the semicolon (`;`): | ||
| Such statements need to end with a semicolon (`;`): | ||
| ```cpp | ||
| b = a + b; | ||
| func(); | ||
| a += b; | ||
| ``` | ||
|
|
||
| AngelScript follows a specific instruction of operation order. Function calls are evaluated first bottom to top, then AS sticks to order of operations as specified in the [Operator Precedence](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_operator_precedence.html) reference. | ||
| AngelScript follows a specific instruction of operation order. First, function calls are evaluated bottom to top. AngelScript then sticks to the order of operations as specified in the [Operator Precedence](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_operator_precedence.html) reference. | ||
|
|
||
| You can force an order of operations by using parenthesis: | ||
| You can force an order of operations by using parentheses: | ||
| ```cpp | ||
| float a = 1 + 1.0 / 2; // This will return 1,5 | ||
| float b = (1 + 1.0) / 2; // This will return 1 | ||
| ``` | ||
|
|
||
| > [!TIP] | ||
| > Order of how operators are evaluated for standard math operators such as **+**, **-**, **\***, **/**, etc. follow the standard Order of Operations. | ||
| > The order of how operators are evaluated for standard math operators such as **+**, **-**, **\***, **/**, etc. follow the standard Order of Operations. | ||
|
|
||
| > ### TASK 1: | ||
| > Given `float a = 4; float b = 5;` Write a program that calculates the number c given by the equation: `c = (a - 2)^2 + (b + 1) / 2`. | ||
| > Given `float a = 4; float b = 5;`, write a program that calculates the value of `c` given by the equation `c = (a - 2)^2 + (b + 1) / 2`. | ||
|
|
||
|
|
||
| ### Comparison operators | ||
| Comparison operators are operators of which expressions evaluate to the `true` or `false` boolean values. An example of a comparsion operator is the equals (**==**) operator, which checks if the two values on boths sides of such operator are equal. Another type of a condition operator is the greater than operator (**>**), which checks if the value on the left side is greater than the value on the right side. For comparison operator reference please check the [expression reference table](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_expressions.html). | ||
| ### Comparison Operators | ||
| Comparison operators are operators that compare two values, such as with the `true` or `false` boolean values. An example of a comparsion operator is the **equals** (**==**) operator, which checks if the values on both sides of the operator are equal. Another example is the **greater than** operator (**>**), which checks if the value on the left side is greater than the value on the right side. For a more comprehensive comparison operator reference, please check the [expression reference table](https://www.angelcode.com/angelscript/sdk/docs/manual/doc_expressions.html). | ||
| ```cpp | ||
| int a = 1; | ||
| int b = 2; | ||
|
|
@@ -64,47 +64,48 @@ b = 1; | |
| bool result = a == b; // Result now stores the information if a and b were equal when it was defined. | ||
| ``` | ||
|
|
||
| ### Logic operators | ||
| ### Logic Operators | ||
| Logic operators are a way to combine comparison expressions in order to achieve one result: | ||
| - NOT - denoted as `!`, evaluates to true, if value is false and vice versa, | ||
| - AND - denoted as `&&`, evaluates to true, if both values are true, | ||
| - OR - denoted as `||`, evaluates to true, if even one of the values is true, else false if both are false, | ||
| - XOR - denoted as `^^`, evaluates to true, if and only if **one** of the values is true. | ||
| - NOT - denoted as `!`. Evaluates to true if value is false and vice versa. | ||
| - AND - denoted as `&&`. Evaluates to true if both values are true. | ||
| - OR - denoted as `||`. Evaluates to true, if even one only of the values is true. Otherwise evaluates to false if both values are false. | ||
| - XOR - denoted as `^^`. Evaluates to True if and only if **one** of the values is True. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True is capitalized here when it shouldn't |
||
|
|
||
| ```cpp | ||
| a && b; // AND | ||
| a && b || c; // A combination of AND and OR, a AND b is going to get evaluated first | ||
| a && (b || c); // You can use parenthesis to specify which operator should get evaluated first, here OR will get evaluated first | ||
| a && b || c; // A combination of AND and OR. a AND b will be evaluated first. | ||
| a && (b || c); // You can use parentheses to specify which operator should get evaluated first. Here, OR will get evaluated first. | ||
| ``` | ||
| > [!NOTE] | ||
| > Although AngelScript supports Python-like logic operator notation (and, or, ...) it is recommended to stick to the C-like notation. This is mainly because AS is much more similar to C in it's syntax, and also not every extension adding AS support for your IDE has support for the Python-like notation. | ||
| > Although AngelScript supports Python-like logic operator notation (and, or, ...) it is recommended to stick to C-like notation. This is mainly because AS is much more similar to C in its syntax. In addition, not every extension adding AS support for your IDE has support for the Python-like notation. | ||
|
|
||
|
|
||
| --- | ||
|
|
||
| ## Conditions | ||
| Conditions are a way to tell the engine to execute specific code only if a specific condition is met. For example, only add numbers if they are positive. | ||
| They should contain an expression that evalues to true or false, and they can be any sort of valid combination of comparison operators and logic operators, etc. | ||
| Conditions are a way to tell the engine to execute specific code only if a specific condition is met. For example, conditions will allow you to tell the engine to only add numbers together if they are positive. | ||
| Conditions should contain an expression that evaluates to true or false. They can be any valid combination of comparison operators and logic operators, etc. | ||
|
|
||
| ### If/else block | ||
| The `if`/`else if`/`else` block is used to run specific code only on certain conditions. The `else if` and `else` are not required, but the block must start with an `if` statement. | ||
| ```cpp | ||
| if ( a > 10 ) { // Condition 1 | ||
| // Run the code in here | ||
| } else if ( a < 10 ) { // Condition 2 | ||
| // If we haven't met condition 1, but have met condition 2, execute the code in here | ||
| // If we haven't met condition 1, but have met condition 2, execute this code | ||
| } else if ( condition3 ) { | ||
| // We haven't met neither condition 1, nor condition 2, but we have met condition 3 | ||
| // We haven't met neither condition 1, nor condition 2, but we have met condition 3, execute this code | ||
| } else { | ||
| // If none of the conditions were met, execute this code | ||
| } | ||
| ``` | ||
|
|
||
| > ### TASK 2: | ||
| > Given two numerical values *a* and *b* (defined statically in your script) prints out an absolute value of their difference. | ||
| > Given two numerical values *a* and *b* (defined statically in your script), print out an absolute value of their difference. | ||
|
|
||
|
|
||
| ### Switch statement | ||
| ### Switch Statement | ||
|
|
||
| The switch statement is a very useful tool for situations where you need to compare to a lot of different cases. It performs the *is equal* operation comparing a value to every value specified in case blocks. It is also much faster than a block of the `if`/`else if`/`else` statements. | ||
| ```cpp | ||
| switch ( value ) { | ||
|
|
@@ -122,22 +123,22 @@ switch ( value ) { | |
| } | ||
| ``` | ||
| > [!CAUTION] | ||
| > Each case requires a `break` statement after finishing code execution. This is because switch behaves more like the `goto` (C++) functionality, meaning that it doesn't stop executing code after finishing a specific case. The `break` statement will tell the code to go out of the `switch` block. This is also why in the upper example `case 2:` and `case 3:` can be used to both execute the same lines of code. | ||
| > Each case requires a `break` statement after finishing code execution. This is because switch statements behave more like the `goto` (C++) functionality, meaning that it doesn't stop executing code after finishing a specific case. The `break` statement will tell the code to go out of the `switch` block. This is also why in the upper example `case 2:` and `case 3:` can be used to both execute the same lines of code. | ||
|
|
||
| > ### TASK 3: | ||
| > Given a string as a value (statically defined), write a program that would simulate a command-like behaviour using the switch statement. If the string is equal to: | ||
| > - "hello" - print "HelloWorld" to the console | ||
| > - "engine" - print "Strata Source" to the console | ||
| > - "name" - print "My name is Chell" to the console | ||
| > - on any other string - print "Command not recognized!" to the console | ||
| > - Any other string - print "Command not recognized!" to the console | ||
|
|
||
|
|
||
| --- | ||
|
|
||
| ## Variable Scope | ||
|
|
||
| Variable Scope determines which data can be accessed from where. In AngelScript the Variable Scope behaves exactly as the one in C++. | ||
| In general the Variable Scope makes programs use less memory by not holding expired (not useful anymore) information inside the memory, as an example: | ||
| Variable Scope determines which data can be accessed from where. In AngelScript, Variable Scope behaves exactly as the one in C++. | ||
| In general, the Variable Scope makes programs use less memory by not holding expired information inside the memory. As an example: | ||
| ```cpp | ||
| int number = 10; | ||
|
|
||
|
|
@@ -150,11 +151,11 @@ void my_func2() { | |
| my_number = 80; // Error, my_number has not been declared | ||
| } | ||
| ``` | ||
| In this case my_number doesn't exist inside *my_func2*, it only exists inside *my_func1* and only for the duration of that function's execution. | ||
| In this case, `my_number` doesn't exist inside *my_func2*, it only exists inside *my_func1* and only for the duration of that function's execution. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. match the ticks used with |
||
|
|
||
| Statements such as `if`, `else`, `for`, `while`, `try`, etc. create local variable scopes each time, meaning that variables declared inside them cannot be accessed outside of them, contrary to how for example Python handles it (where scopes are only created inside functions). | ||
| Statements such as `if`, `else`, `for`, `while`, `try`, etc. create local variable scopes each time, meaning that variables declared inside them cannot be accessed outside of them, contrary to how, for example, Python handles it (where scopes are only created inside functions). | ||
|
|
||
| In AS, global variables are allowed, however: | ||
| In AngelScript, global variables are allowed. However: | ||
|
|
||
| > [!NOTE] | ||
| > From the official documentation: *Variables of primitive types are initialized before variables of non-primitive types. This allows class constructors to access other global variables already with their correct initial value. The exception is if the other global variable also is of a non-primitive type, in which case there is no guarantee which variable is initialized first, which may lead to null-pointer exceptions being thrown during initialization.* | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be dumbed down to just, something like "most of this content is optional but some might be helpful so feel free to read"