Understanding Javascript
Well. We're not here to teach you how to code in Javascript. This is something that you need to do on your own time. However, we'll at the very least link a few videos for you to check out to help you get a better understanding of Javascript.
It is highly recommended that you physically follow along with these videos.
Understanding VSCode
Now inside of VSCode you should see your entire file library on the left-hand side and should see two different startup.js
files. We'll be working mainly in the server
file as this controls what happens to a player.
Using Intellisense
Open server/startup.js
and take a look at the code block you wrote earlier in this tutorial.
/// <reference types="@altv/types-server" />
import * as alt from 'alt-server';
alt.log('Hello from server');
Start by typing alt.on
anywhere below the imports
.
You should see an auto-completion box with information
As you can see you will see a handful of icons, text, and information. However, if you are a new developer none of this will make any sense to you. I'd like you to refer to the image below to help you understand what is going on a bit better.
Functions
If you are new to development and you do not know what a function is. They are very simple and easy to understand. Functions
are what we call to execute a block of code
. Functions
may also take
what we call parameters
.
function sayHello() {
alt.log('I am saying hello');
}
sayHello(); // This executes the above function.
function sayMyName(someName, someAge, someHairColor) {
alt.log(someName);
alt.log(someAge);
alt.log(`Hair Color is: ${someHairColor}`); // String Interpolation
}
sayMyName('stuyk', 27, 'Brown');
Properties
Properties
are single variables that can have their values read
or set
. However, this is dependent on the property.
alt.log(alt.globalDimension); // This is reading the alt.globalDimension prop.
alt.globalDimension = 5; // This is setting the alt.globalDimension prop.
Classes
A class
almost always starts with new
. Classes are basically an object
with a bunch of functions
and properties
that are accessible from within the class. A Player
, Vehicle
, ColShape
, etc. are all classes.
const vehicle = new alt.Vehicle('infernus', 0, 0, 0, 0, 0, 0);
Enums
Enums are mostly unique to TypeScript
but we can leverage those types in Javascript by extracting values from there. When you type something like alt.ExplosionType.Barrel
you can hover over it to get the value that it's meant to be. In this case it's 27
.
Written by Stuyk & Alexa