| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
hpl3:community:scripting:angelscript_tutorial [2018/04/13 17:21] abion47 [Variable Types] |
hpl3:community:scripting:angelscript_tutorial [2018/04/15 16:37] (current) abion47 [Variable Types] |
||
|---|---|---|---|
| Line 411: | Line 411: | ||
| There is one more built-in type for AngelScript, and it has nothing to do with numbers: | There is one more built-in type for AngelScript, and it has nothing to do with numbers: | ||
| - | ^Type Name ^Possible Values ^ Default | | + | ^Type Name ^Possible Values ^Default | |
| |bool |true, false |false | | |bool |true, false |false | | ||
| Line 426: | Line 426: | ||
| ^Type Name ^Possible Values ^Default | | ^Type Name ^Possible Values ^Default | | ||
| - | |[[:hpl3:community:scripting:classes:tstring|tString]] |"Hello SOMA" | | | + | |[[:hpl3:community:scripting:classes:tstring|tString]] |"Hello SOMA" |<font inherit/inherit;;#FF0000;;inherit><Empty></font> | |
| + | |||
| + | In HPL3's AngelScript, a `tString` is a specific flavor of a string, which is a sequence of characters, letters, and numbers. It doesn't have a fixed length, so it can be anything from `"a"` to `"Greetings, sirs, my what a lovely fine evening we have here. Might you fancy a cup of tea?"`. | ||
| + | |||
| + | The most common way to use a `tString` is to store some kind of word or label, such as when you want to display a message on the screen that the player will read. There are some handy functions that you can use with a `tString`: | ||
| + | |||
| + | ^Function Name ^Description ^Example Usage | | ||
| + | |length |Gets the number of characters represented by the string. | <html>int stringLength = tStringValue.length();</html> | | ||
| + | |resize |Resizes the string to be the specified length. (Removes characters or adds "null" characters as necessary.) | <html>tString resizedString = originalString.resize(5);</html> | | ||
| + | |||
| + | === Arrays === | ||
| + | |||
| + | In addition to variables that store a single value, there is also a type of variable that holds ''many'' values. These variables are called '''arrays'''. | ||
| + | |||
| + | Some languages allow you to create arrays that can store anything, but in AngelScript, arrays can only hold a single type. This type is chosen when the array is first declared: | ||
| + | |||
| + | <code=c++> | ||
| + | array<int> intArray; | ||
| + | array<bool> boolArray; | ||
| + | array<tString> stringArray; | ||
| + | </code> | ||
| + | |||
| + | In the above example, the type name inside the angled brackets is the type of value that array can store. For example, the `intArray` can only store integers, and the `stringArray` can only hold string values. | ||
| That's all well and good, but how do you get at the values in the array? That is done using something called //indexer notation//. Let me show you how it works: | That's all well and good, but how do you get at the values in the array? That is done using something called //indexer notation//. Let me show you how it works: | ||
| - | <code =c++> | + | |
| + | <code=c++> | ||
| lightLevels[0] = 5.0; | lightLevels[0] = 5.0; | ||
| float lightLevel = lightLevels[0]; | float lightLevel = lightLevels[0]; | ||
| Line 444: | Line 467: | ||
| ^Function Name ^Description ^Example Usage | | ^Function Name ^Description ^Example Usage | | ||
| |length |Gets the number of values stored in the array. | | | |length |Gets the number of values stored in the array. | | | ||
| - | + | |push_back |Adds a value to the end of the array. (If indices 0-4 contain values, the new value is stored at index 5.) | | | |
| - | | | + | |resize |Resizes the array to t | | |
| - | + | ||
| - | |push_back |Adds a value to the end of the array. (If indices 0-4 are used, the value is stored at index 5.) | | + | |
| - | + | ||
| - | | | + | |
| - | + | ||
| - | |resize |Resizes the array to the specified length, adding or removing values as necessary. | | + | |
| - | + | ||
| - | | | + | |
| ---- | ---- | ||