Setting Up a Development Environment using Visual Studio Code

From GTA Network Wiki
Jump to: navigation, search

Setting Up a Development Environment using Visual Studio Code

Requirements

  • Basic C# programming knowledge

Introduction

This page will be going over how to set up a server and write your first resource on any Operating system that supports Visual Studio Code and .NET Core 2.0

A resource is a custom script which can be run on the server or the client.

Installation process

The installation of both applications is automated and install like any other application on your Operating system.

.NET Core 2.0 has no graphical interface so you will not see it anywhere on your screen after installing.

Setting Up The Project

Before you setup the project in Visual Studio Code, make sure you have an appropriate directory to place your projects in. Also be sure you have extracted the server files somewhere.

For this tutorial, my server files will be extracted to Desktop\server as you can see in the screenshot bellow.

vscstep1.png



Now that we have our server set up, we're now going to create the resource folder to where project will live in.

Which as you can see, under the Desktop\server\bridge\resources directory, I have created my freeroam folder.

vscstep3.png



Setting up Visual Studio Code

Launch Visual Studio Code. You will be greeted by a welcome screen with recent projects listed in the left-hand pane.

At the top of the left-hand pane, there is a "File" button, click it and then click on "Open Folder", navigate to our resources folder in which for this tutorial is Desktop\server\bridge\resources, select the resource folder, which in our case is freeroam and finally click on Select Folder.

vscstep2.png



On the next screen, we're going to create the required files for the resource by writing a simple command in the Console.

In order to open the Terminal (aka Console) in Visual Studio code, we either use a quick combination that is Ctrl+` or by navigating to View and clicking on Integrated Terminal.

Now that we've opened up the Terminal, we're gonna write into the console the following line: dotnet new classlib --framework netcoreapp2.0 and hit the Enter key.

vscstep51.png



Now that we've created our project files, we're gonna proceed and refresh the folder listing.

vscstep6.png



Installing the API

Now that we've set up our resource, all that we need is the API so that we can get the IntelliSense fanciness. therefor, we're gonna install the NuGet package available over here https://www.nuget.org/packages/gtanetwork.api

So in order to do that, we open our Terminal again, and type in the following command: dotnet add package gtanetwork.api and hit the Enter key.

vspackage.png]



Once the package is correctly installed, you should be prompted to either "Restore" or "Close" regarding unresolved dependencies for the project, we're gonna hit Restore and let it do its magic.

vspackageres.png



Once the magic is done executing in the previous step, we will now simply relaunch Visual Studio Code in order to reinitialise the packages and we then should be able to start scripting!

startcoding.png



Start scripting

Now you're ready to start scripting! For this example, we'll use the API to print a "Hello World!" string to our server console. This code is placed in the constructor of our class, which is run when the script is first called. However, for most cases, you will want to call any code you want run when the resource is loaded using the OnResourceStart event.

Be sure to extend the Script class in any class you wish to use the API in. You can do this by adding : Script at the end of any new class as shown below. I also renamed my main class to Gamemode.

We just kept it simple for this example.

using System;
using GTANetworkAPI;

namespace FreeroamServer
{
    public class Gamemode : Script
    {
        public Gamemode()
        {
             API.ConsoleOutput("Hello World!");
        }
    }
}

Conclusion

Setting up a development environment using .NET Core 2.0 and Visual Studio Code is fairly simple, and should be straightforward in every Operating System. There are, of course, other ways to go about it such as using a simple text editor and creating .cs files directly. Visual Studio Code is very nice because Intellisense with the library references is extremely convenient when referencing methods in the API.

If you have any questions, feel free to ask in the thread and I will do my best to help. Have fun scripting!


Special thanks to SnowDog's very friendly and inspiring tutorial at Setting_Up_a_Development_Environment_on_macOS