Setting Up a Development Environment on macOS

From GTA Network Wiki
Jump to: navigation, search

Setting Up a Development Environment on macOS

Requirements

  • Basic C# programming knowledge

Introduction

This page will be going over how to set up a server and write your first resource on macOS. A resource is a custom script which can be run on the server or the client.

Note: This tutorial is from SnowDog -> https://forum.gtanet.work/index.php?threads/tutorial-setting-up-a-development-environment-on-macos.139/

Installation process

The installation of both applications is automated and install like any other application on macOS. Mono has no graphical interface so you will not see it in Launchpad or your applications after installing.

If you are running OS X El Capitan or later, you will need to run this command in your terminal after installing mono before attempting to use it:

export PATH=/Library/Frameworks/Mono.framework/Versions/Current/bin/:${PATH}

Without going into too much detail, you basically need to perform that command or the mono command in your terminal will not be found when you attempt to run the server executable later.

Setting Up The Project

Before you setup the project in Xamarin Studio, make sure you have an appropriate directory to place your projects in. I created a directory called Projects in my home folder. Also be sure you have extracted the GTANetwork server files somewhere. For this tutorial, my server files will be extracted to ~/Downloads/server.

Mac server browser.png


Setting up Xamarin Studio

Launch Xamarin Studio. 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 "New Solution" button. Click this to create a new project and solution. The project type you need to select is a .NET Library (C#).

Mac xamarin startup.png



On the next screen, it will ask you to name your project and select the location of it. As a practical example, I have named my project StuntServer and placed it in my Projects directory I created earlier.

Mac xamarin project.png


Add references

Now that the project is created you should have an empty class inside of a namespace named after your project. The last thing we need to do before we dive into scripting is referencing the GTANetwork API libraries. You can do this by right-clicking the References section on the left-hand pane of your project and clicking "Edit References..."

Mac xamarin references.png



Select the .NET Assemblies tab and click the "Browse" button. Browse to the folder where you extracted your server files and select GTANetworkServer.exe and GTANetworkShared.dll.

Mac xamarin add ref.png



After they are imported, make sure the checkbox next to both of them is checked. They should both show in the "Selected References" section of the window as shown below. Please ignore the duplicates in my list as they were from another project.

Mac xamarin add ref2.png


Start scripting

Now be sure to place the using lines at the top of your .cs file to access the namespaces of the libraries you just referenced. You also need 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 Game mode.

using System;
using GTANetworkServer;
using GTANetworkShared;

namespace StuntServer
{
    public class Gamemode : Script
    {
        public Gamemode()
        {
        
        }
    }
}




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. We just kept it simple for this example.

using System;
using GTANetworkServer;
using GTANetworkShared;

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



Run the server

There are a lot of different ways you can organize your project and your GTANetwork resources. Personally, I find it easiest to split my project up into different classes within the project, based on functionality, and then compile it to a DLL before placing it into my GTANetwork resource folder. You can also copy over the C# files (.cs) directly from the project into the resource folder and run them that way. Just be sure they are all listed in your resource meta file properly. More information on creating a new resource and metafile can be found here.

For this example, I created a new resource folder and meta file and copied over my finished Gamemode.cs file. Now I can run the server using the mono command in the terminal, and see my "Hello world!" string print.

Mac mono server.png

As you can see, my script worked perfectly and the GTANetworkServer.exe is running flawlessly under macOS.

Conclusion

Setting up a development environment on macOS is fairly simple and very similar to using Visual Studio on Windows, aside from having to run the executable through mono. There are, of course, other ways to go about it such as using a simple text editor and creating .cs files directly. Visual Studio and Xamarin Studio are 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!

Note: This tutorial is from SnowDog -> https://forum.gtanet.work/index.php?threads/tutorial-setting-up-a-development-environment-on-macos.139/ -