Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
learngs2:variables [2023/02/24 12:11] – twinny | learngs2:variables [2023/02/24 12:47] (current) – twinny | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <- whatjusthappened ^ start ^ functions | + | << whatjusthappened ^ start ^ functions |
===== Understanding Variables ===== | ===== Understanding Variables ===== | ||
- | pages about variables | + | When we used " |
+ | |||
+ | Let's consider a super basic variable | ||
+ | |||
+ | < | ||
+ | this.greeting = "Hello there!" | ||
+ | |||
+ | </ | ||
+ | |||
+ | Here we have created a variable, this.greeting, | ||
+ | |||
+ | This is cool….what does it do? Well, we can now reference the variable we just created: | ||
+ | |||
+ | <code javascript> | ||
+ | function onCreated() { | ||
+ | this.greeting = "Hello there!"; | ||
+ | echo(this.greeting); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | In this function, we have created the variable this.greeting, | ||
+ | |||
+ | < | ||
+ | Weapon/ | ||
+ | Hello there! | ||
+ | |||
+ | </ | ||
+ | |||
+ | Success! We can store a lot of different data types inside a variable. Some examples include: | ||
+ | |||
+ | <code javascript> | ||
+ | function onCreated() { | ||
+ | this.string = "Hello there!"; | ||
+ | this.integer = 3; | ||
+ | this.float = 3.14; | ||
+ | this.array = {1, | ||
+ | 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. | ||
+ | temp. - Refers to temporary objects in the current scope. For example, if we created a temp.greeting in function onCreated(), | ||
+ | player. | ||
+ | client. | ||
+ | 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 | ||
+ | |||
+ | </ | ||
+ | < | ||