Variables - Printable Version +- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum) +-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html) +--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html) +---- Forum: Development Articles (https://www.frictionalgames.com/forum/forum-40.html) +---- Thread: Variables (/thread-26129.html) |
Variables - FlawlessHappiness - 09-14-2014 Variables I was asked how variables work, and I'm in the mood for creating a little tutorial, so here you are ^_^ (Also, I hope this hasn't already been covered) 1. What is a variable? Before I start, if there's some clever guy out there, who thinks I'm saying something wrong, do correct me. In Amnesia we work with 4 different variable-types.
Integers Integers are numbers without decimals. Whole numbers. It's simple: 1 is an int. 1.5 is not an int. Integers don't often appear in the script-lines used in Amnesia. They use floats instead. Floats Floats are the opposite. Floats are numbers with decimals, but you can still use whole numbers. 1.5 is a float. 1 is also a float, but will (I assume) be seen as 1.0 A line you've probably used is: PHP Code: FadeOut(float afTime); You probably only write 1 or 2, but the game will see it as 1.0 or 2.0. So, in fact, you could actually have the game fade to black in 1.5 seconds. Booleans Bools are not numbers, but will either be true or false. You have probably used the following script-line. PHP Code: SetPlayerActive(bool abActive); Strings A string is a word. Can consist of letters, but also be numbers. "Hello" is a string. "Over9000" is also a string. "9000" is also a string (I assume). But cannot be transferred into an int or float directly. You'd need something else first. Maybe you've tried to remove a timer before. PHP Code: RemoveTimer(string& asName); 2. So how do I use a variable? Good question. For integers and floats we'll just go through some simple steps.
When you've thought about the first two steps, lets finish it by doing the third. In this tutorial name will be "NAME" and number will be 1. To set a variable
PHP Code: SetLocalVarInt("NAME", 1);
PHP Code: SetLocalVarFloat("NAME", 1.0); That wasn't so hard, was it? We can do the same with strings. Just instead of using a number, we will be using a word. PHP Code: SetLocalVarString("NAME", "WORD"); Notice, how this works the exact same way? Awesome! 3. What can I do with them? For ints and floats, you can do, what numbers do best! Add, subtract, multiply and divide. It's basic math. Say you want the int, "NAME", to be 1 higher. You simply write: PHP Code: AddLocalVarInt("NAME", 1); The int, "NAME", is now 2. To make it 1 lower write: PHP Code: AddLocalVarInt("NAME", -1); The int, "NAME", is now 1, again. QUESTION: Yea, yea, those are all nice things, but how can I use it in my scripts?
Very good question, indeed! Let's look at some basic things. 4. Variables and script-lines What's so awesome about variables is that you can have them stand instead of numbers and words. Before we start! I need you to know a thing that we'll be using all the time!
PHP Code: GetLocalVarInt("NAME")
PHP Code: GetLocalVarFloat("NAME") These lines are important, as they give all the information. We use them in, if-statements. Look at this: PHP Code: if(GetLocalVarInt("NAME") == 1) Now, this can also be used differently. Instead of asking if the variable, "NAME", is something, we can use it in a script-line. In this example we'll be using it in SetEntityActive("ENTITY", false); Look at this: PHP Code: SetEntityActive("Door_"+GetLocalVarInt("NAME"), false); If the variable, "NAME", is 1 then the name of the door will be "Door_1". If the variable, "NAME", is 2 then the name of the door will be "Door_2". This way you only have to write the line once. Then you can just change the variable, as you like. I'm just gonna jump right into the first example now. I hope you follow. 1st example What we're trying to create is that when you touch a script-area it'll show a message. But when you click on a button, it'll show another message. TIP: If you haven't specified an int or float, it'll always be 0 PHP Code: void ClickOnArea(string &in asEntity) When you start the script, it should look like this. https://www.youtube.com/watch?v=59qyY9c0EX8&feature=youtu.be 2nd example For the next example, we'll be using AddLocalVarInt, to keep changing messages, when we have more than 2. What's gonna happen: Click on area => Message 1 appears Click on button. Click on area => Message 2 appears Click on button Click on area => Message 3 appears ... etc. the script is almost the same. It looks like this. TIP: You can use GetLocalVarInt("NAME"), to make the script look for the number of the variable. It's a great way to make everything easier for you, just like it's written above this PHP Code: void ClickOnArea(string &in asEntity) When you start the script, it should look like this: https://www.youtube.com/watch?v=ILuRnqzScAk&feature=youtu.be 3rd example Let's go on to the hardcore stuff. What we want to do, is have a screen-effect go from 0 to a high number, over time. We will be using timers and variables for this one. Please, make sure you know about how timers work, before reading further. How it'll work: When clicking an area, the timer starts. Each time the timer calls, the screen effect will increase it's grip on the player. Here's how the script looks. PHP Code: void ClickOnArea(string &in asEntity) When you start the script, it should look something like this: https://www.youtube.com/watch?v=7-yBFa9qT_0&feature=youtu.be Thank you! For viewing this, so far. If you have any questions, need me to explain something further, or something else, do ask! I might not be able to answer everything. But hey, it's always great to ask. RE: Variables - Mudbill - 09-15-2014 I like using booleans in the script, but there are no dedicated functions (like SetLocalVarBool) because it's unnecessary. An int with 0/1 does the same. But bools are still pretty cool. Now, for a beginner, they probably don't need to worry about it though. Seeing as this is, by the looks of it, a beginners guide to variables, it's fairly good the way you've put it up. On the more technical side, I just wanna mention a few things There are a few other variables that can be used, such as a Double. They're used just like floats, pretty much, but they have a greater range (not that it's needed in 99% of cases). Using the primitive versions is often easier (in my opinion). PHP Code: int intName = 0; So like PHP Code: int number = 1; PHP Code: SetLocalVarInt("number", 1); PHP Code: SetLocalVarInt("number", 1); There are more primitives, but the ones above (except the double) are the important ones. They work the same as how you've explained though. Notice the f and d at the end of the float and double values. They simply state that this value is a float or a double. If you do PHP Code: float f = 1.0; Just some extra info for the curious ones PS: Your (assumptions) seem correct to me. RE: Variables - FlawlessHappiness - 09-15-2014 What is a double? I've never heard of it ^_^ Also, the primitive way works, yes, but when scripting in HPL it'll only work inside the brackets you're in. If you suddenly want to use the same variable as you used in a previous function, then you can't because you used the primitive way, which is now erased from the memory of the script. RE: Variables - Mudbill - 09-15-2014 (09-15-2014, 12:25 AM)FlawlessHappiness Wrote: What is a double? It depends where you declare it. If you add it outside all your blocks, it can be accessed anywhere. PHP Code: int value = 1; If the primitive is declared inside a block, the scope only allows it to be used within that block. Once it's finished, it's erased from memory. I believe the values are static within the file, so changing the value in one block will affect the overall value of the primitive throughout the file. The original value is only initial. PHP Code: int value = 1; Primitives can also be declared as null (no value). If you know you set the variable to a value before you use it, it can help make the code more stable. PHP Code: int value; //declared without a value Null isn't very important to use in your script as a beginner or even intermediate. I don't think I've used it much myself except no value declarations. Any variable can be assigned null if nothing else is specified. Null is often a crash cause if you expect to get a value. Important to note that null is not the same as 0. Null isn't directly present in HPL's script variables though. You can probably lure it in there by doing something like PHP Code: SetLocalVarInt("number", value); Most of this is unnecessary in most cases of HPL scripting. RE: Variables - Romulator - 09-15-2014 Quote:"9000" is also a string (I assume). 9000 is a string by itself if declared that way (as a String, not an Integer). A String itself is simply a collection of chars (characters). A char is a single unicode character, and there are over 107,000 of them. Typically, anything you declare can be declared as a string, but String variables are often used for later referencing in code rather than use in-game. For example, in Justine, a String variable keeps track of which suitor and prisoner is currently in the level and plays the corresponding sound. I would search for the code, but I cannot be bothered The syntax and code example can be found searching for StartCharacterSpeak RE: Variables - FlawlessHappiness - 09-15-2014 Thank you guys ^^ Great clarifications! I still didn't get an answer to what a double is... RE: Variables - Romulator - 09-15-2014 (09-15-2014, 11:46 AM)FlawlessHappiness Wrote: Thank you guys ^^ In short: A big float number of an extremely large range, consisting of 64-bit allowance of digits. In long: Spoiler below!
RE: Variables - FlawlessHappiness - 09-15-2014 Aha! So it won't matter much whether we use floats or doubles, since we're never gonna get into such specific numbers. Thank you! |