The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 906 - File: showthread.php PHP 7.2.24-0ubuntu0.18.04.17 (Linux)
File Line Function
/showthread.php 906 errorHandler->error



Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#1
Variables

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.
  • Integer (Referred to as int)
  • Float
  • Boolean (Referred to as bool)
  • String


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: (Select All)
FadeOut(float afTime); 
Note that 'float' is for how long it takes for the screen to go black.
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: (Select All)
SetPlayerActive(bool abActive); 
Note that 'bool' here refers to either be true or false

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: (Select All)
RemoveTimer(stringasName); 
Note that 'string' here refers to the name of a timer.


2. So how do I use a variable?
Good question.

For integers and floats we'll just go through some simple steps.

  1. Give the variable a name
  2. Give the variable a number
  3. Set it in a script-line

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
  • for ints use
PHP Code: (Select All)
SetLocalVarInt("NAME"1); 
  • for floats use
PHP Code: (Select All)
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: (Select All)
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: (Select All)
AddLocalVarInt("NAME"1); 

The int, "NAME", is now 2.

To make it 1 lower write:

PHP Code: (Select All)
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!
  • for ints
PHP Code: (Select All)
GetLocalVarInt("NAME"
  • for floats
PHP Code: (Select All)
GetLocalVarFloat("NAME"

These lines are important, as they give all the information.
We use them in, if-statements.

Look at this:
PHP Code: (Select All)
if(GetLocalVarInt("NAME") == 1)
{
//DO SOMETHING

We're simply saying: If the variable, "NAME", is 1, then do something!


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: (Select All)
SetEntityActive("Door_"+GetLocalVarInt("NAME"), false); 
What we're doing here is, where taking the name "Door_" and then we're adding the variable, "NAME", to the name.

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: (Select All)
void ClickOnArea(string &in asEntity)
{
if(
GetLocalVarInt("MessageVariable") == 0//If variable is 0, show first message
{
SetMessage("Messages""Message_1"0);
}

if(
GetLocalVarInt("MessageVariable") == 1//If variable is 1, show second message
{
SetMessage("Messages""Message_2"0);
}
}

void ClickOnButton(string &in asEntity)
{
if(
GetLocalVarInt("MessageVariable") == 0//If the variable is 0, set it to 1.
{
SetLocalVarInt("MessageVariable"1);
}


When you start the script, it should look like this.
https://www.youtube.com/watch?v=59qyY9c0...e=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: (Select All)
void ClickOnArea(string &in asEntity)
{
SetMessage("Messages""Message_"+GetLocalVarInt("MessageVariable"), 0);
}

void ClickOnButton(string &in asEntity)
{
AddLocalVarInt("MessageVariable"1); //This adds 1 to MessageVariable

if(GetLocalVarInt("MessageVariable") == 6//Check, if the variable is 6. If it is, then set it to 0
{
SetLocalVarInt("MessageVariable"0);
}


When you start the script, it should look like this:
https://www.youtube.com/watch?v=ILuRnqzS...e=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: (Select All)
void ClickOnArea(string &in asEntity)
{
AddTimer("EffectTimer_1"1"EffectTimer");
}

void EffectTimer(string &in asTimer)
{
AddLocalVarInt("ScreenEffectVariable"1); //Add 1 to the variable

if(GetLocalVarInt("ScreenEffectVariable") == 30//if the variable is 30, stop!
{
return;
}

FadeImageTrailTo(GetLocalVarFloat("ScreenEffectVariable")/1010); 
//Set screen effect to the number of the variable, divided by 10, because we need a small number.

AddTimer("EffectTimer_1"0.1"EffectTimer"); //Loop the timer, by calling itself in 0.1 seconds


When you start the script, it should look something like this:
https://www.youtube.com/watch?v=7-yBFa9q...e=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.

Trying is the first step to success.
(This post was last modified: 09-14-2014, 10:54 PM by FlawlessHappiness.)
09-14-2014, 10:25 PM
Find


Messages In This Thread
Variables - by FlawlessHappiness - 09-14-2014, 10:25 PM
RE: Variables - by Mudbill - 09-15-2014, 12:17 AM
RE: Variables - by FlawlessHappiness - 09-15-2014, 12:25 AM
RE: Variables - by Mudbill - 09-15-2014, 07:32 AM
RE: Variables - by Romulator - 09-15-2014, 08:16 AM
RE: Variables - by FlawlessHappiness - 09-15-2014, 11:46 AM
RE: Variables - by Romulator - 09-15-2014, 12:21 PM
RE: Variables - by FlawlessHappiness - 09-15-2014, 12:47 PM



Users browsing this thread: 1 Guest(s)