learngs2:variables

When we used “Hello, World!” in the echo() function earlier, we used what is called a Literal String . It's a static value that will not change. It's also not stored anywhere: it will be executed in the echo() function and then it will dissapear. Sometimes we want to store such data. This is where variables come into play.

Let's consider a super basic variable

this.greeting = "Hello there!"

Here we have created a variable, this.greeting, and we have placed the “Hello there!” literal string inside it.

This is cool….what does it do? Well, we can now reference the variable we just created:

function onCreated() {
  this.greeting = "Hello there!";
  echo(this.greeting);
}

In this function, we have created the variable this.greeting, we have placed 'Hello there!' inside that variable and now, we have passed this.greeting inside the echo() function. Knowing all this, we should expect to see 'Hello world!' appear in RC if we created this weapon:

Weapon/GUI-script Personal/Twinny/Test added/updated by Twinny
Hello there!

Success! We can store a lot of different data types inside a variable. Some examples include:

function onCreated() {
  this.string = "Hello there!";
  this.integer = 3;
  this.float = 3.14;
  this.array = {1,2,3,4};
  this.object = player;
}

The numbers and strings should make sense. We also have arrays and objects at the bottom but don't worry if you don't understand what there are for now: we will come to them!

You may have noticed all variables so far have started with this. This creates the 'scope for the variable which defines where it exists and potentially for how long. There is a few we can use:

this.    - Refers to the current object (e.g. if we are writing this in a weapon, we are created a variable that exists and is accessible in the weapon)
thiso.   - Refers
temp.    - Refers to temporary objects in the current scope. For example, if we created a temp.greeting in function onCreated(), it would only exist so long as onCreated is executed. It will be deleted straight after and recreated the next time onCreated is called
player.  - Refers to the player object current in scope. If you have a weapon and fire the weapon, your player would be in scope and accessible via this
client.  - Lets you access a players' client flags. These are special attributes stored inside a player that survive between reconnects
clientr. - These are very similar to client. flags but they are different in that they can only be read and not written from clientside: we will come back to this!
server. and serverr. - These are variables which are stored in the server itself. We do not need to focus on these for now

This is likely very confusing but again, it will make sense the more you learn and practise with the concepts coming in these lessons! For now, we can move on

  • learngs2/variables.txt
  • Last modified: 2023/02/24 12:47
  • by twinny