MNScript Explained | Net Library

lybbx

CG Super VIP
Donator
Dec 25, 2020
70
35
91
aha x
Hey everyone still interested in MNScript, here's my little explanation on the Networking library for MNScript that could help those who don't understand or are new to MNScript. This thread doesn't explain every part of MNScript so you're expected to know the basics and logic of the programming language beforehand. If you aren't somewhat experienced, @Hydro has made a couple tutorials explaining the basics for you - you can find the links to the threads here. Anyways, let's move on.

Understanding How Networking Works

In order to make programs that use networking, you'll need to understand how it works. It's not the hardest concept to understand, although you will need to know some keywords.
  • IP Address - Abbreviation for Internet Protocol Address. This is the address of a device in a network and a different address is assigned to all devices connected to a network. An IP is usually displayed as 4 8-bit numbers(a number ranging from 0 - 255) separated by full-stops e.g. 175.132.87.59
  • Protocol - Protocols are described as a set of rules to follow for devices to communicate.
All devices connected to a network are assigned IP addresses, so that a network message has a destination and source address and doesn't go to a random computer instead. When a computer wants to send data to another computer, it has to set the destination of the message as the computer's IP address and also the protocol to send it with. Protocols are typically used to tell what data is being transmitted instead of having data which the other computer doesn't know what to do with. Once you have the destination IP address and the correct protocol which the other device should listening for, the message is sent to the destination computer and the data from one computer should be on another. Computers typically need software to tell what the net message contains and how to use the data.

Hopefully you now have a basic understanding of how data is transmitted through protocols and what IP addresses are and what they are used for.

Implementation in MNScript

Now with a brief understanding of the necessary information to use networking in MNScript, you should be ready to learn how to code a program which sends data from one terminal to another.

To start with, you'll need to memorise the essential steps for computers to communicate, these are relatively simple to grasp.
  1. Find the destination IP address to send the message to
  2. Start forming the message and state what protocol you want to send the message with
  3. Write the data to the message
  4. Send the message to the destination address
  5. Reading the message
Finding the Destination IP

There are a couple ways to find the destination ip address to send data to. Firstly, you could simply ask the owner of the second terminal for their ip address, however this requires contact between the computer owners which is inefficient. A better way to do this is using the built in feature of network binds. A bind in networking is very similar to DNS, which you've probably heard about; it's a way of using readable strings to request the unreadable ip address, this makes it a lot easier to remember the address and to easily reuse a script without having to manually put an address in.

The net library contains 2 methods which use binds to get an ip address, these are Net.BindAddress(string) and Net.IPFromNetworkBind(string). Net.BindAddress(string) is used for the device receiving net messages and allows other PCs to get their IP Address from a string. The parameter it takes is the string you want to associate your ip address with. Net.IPFromNetworkBind(string) is used on the device sending the data and receives the IP from a network bind which you can then send the net message to. The parameter is the bind you want the IP of.


Creating the message

You need to tell the message what protocol it will send on and where the data writing for said message starts. The only method to do this is with Net.Start(number). The parameter is the protocol you want to send this on, this can be any 4 or less digit number, make sure you remember this for the software that handles the incoming data. An example of this is simply 'Net.Start(30);'.

Writing Data in the message

Now that you have a net message and defined what protocol you want it to follow, you can now give it some data to be sent to the other computer. You can only send 3 types of data in MNScript and these are numbers, strings and booleans. When sending multiple pieces of data in one net message i.e 3 strings and 2 bools, you have to remember the order the data is written in, so that when you create a program to read the sent data on the other device you know what string is what bit of information. This is why protocols are important. You can simply write data to the net message by using Net.WriteString(string), Net.WriteNumber(number), and Net.WriteBool(bool). The parameter is just the data you want to send.

Sending the net message

Once you have all the data written in the message, you then actually have to send it to the other IP address. This isn't a difficult task as you gathered the IP in the first step, all you have to do is use the method Net.SendToAddress(string) and for the parameter just put the variable you assigned the IP to. Once this is called, the message is officially sent across the network to the destination you gave it.

Reading the message

Now a message has been sent to the destination computer, but it hasn't been told how to process this data that's arrived to it, so without any software interacting with it's ignored. However, software to detect net messages is possible to create and it can be hard to understand. To read these net messages you need to use the Event library, as this detects incoming net messages to your computer, the specific event you're looking to use is 'IncomingNTM' which supplies you with the protocol and the net message. You're given the parameters NTM and number - NTM is a class in the Net library and has 5 useful methods which you will need. The number is simply the protocol it was sent on.

Inside the IncomingNTM function, you'll need to verify that the incoming net message is the one you want to interact with, so you'd use an if statement to verify the protocol it was sent on by either using the number parameter given to you, or use the NTM given to you and use the method NTM.GetProtocol() to get the same result. Once verified that it's the correct net message, you use the NTM.ReadString()/NTM.ReadNumber()/NTM.ReadBool() to get the data in the NTM. If you have written multiple of a single type(i.e 2 strings), you use NTM.ReadString() again to access the next variable assigned when writing the message. If this is a hard concept to understand, examine the following example code and hopefully you will get your head around it. Finally with reading net messages, the source IP address is actually automatically written without having to write it! To access the source address of the net message, simply use NTM.GetSourceAddress() and it returns a string of the source address.


Simple Example Code

Hopefully after reading that text, you're able to comprehend the net library and have a decent understanding of how it works. I've written a bit of example code which should help you fully get your head around how to implement it into your own programs.
Code:
// Simple Online Messaging Program

// All the libraries we need
using System;
using Console;
using Net;
using Event;
using String;
using Player;

Player owner = System.GetUser();
Net.BindAddress(owner.GetName()); // Gets the ingame username, not the maxnet account and creates a bind to associate with

function SendNTM(string cmd)
{
  
    string[] message = String.Split(cmd, "-"); // means you have to type message-user-Hello World
    string prefix = "message";
    if(String.Lower(message[1]) == prefix) // if the first word of the command is message
    {
      
        string dest = message[2]; // Gets second element in the array(2nd word) which should be the username

        StringResult dest_exists = Net.IPFromNetworkBind(dest);
        if(dest_exists.GetResult()) // If the bind links to an address
        {
            dest = dest_exists.GetString(); // Get Destination IP linked to the network bind
            Net.Start(72); // Creates a message with protocol 72
            Net.WriteString(owner.GetName()); // Writes a string to the message with the username of the sender
            Net.WriteString(message[3]); // Writes the actual data wanting to be send to the message
            Net.SendToAddress(dest); // Sends the net message to the address

            Console.WriteLine("Message Sent To " .. dest .. "!");
    }

    }
}
function IncomingNTM(number ptc, NTM ntm)
{
    if(ptc == 72)
    {
        string username = ntm.ReadString();
        string message = ntm.ReadString();

        Console.WriteLine(username .. ": " .. message);
    }
}

// Adds a listener for Incoming Net Messages
Event.AddListener("IncomingNTM", "NetListener", "IncomingNTM");

// Adds a listener for console commands
Event.AddListener("ConsoleCommand", "SendMessage", "SendNTM");

// An Infinite loop to tell if any events are occuring
while(true)
{
    Event.Process();
}

// I havent tested this, so be observant for any mistakes

Thanks for reading guys, if you find any mistakes DM me on discord, or if you have any queries just reply to this thread. this was written at 4AM then 11PM the following day, that's why my writing is so shit, promise
 
Last edited:
  • Like
Reactions: Janus and Zeus

RapperJm

Civil Gamers Expert
Feb 5, 2021
39
14
91
The basement.
youtu.be
Hey everyone still interested in MNScript, here's my little explanation on the Networking library for MNScript that could help those who don't understand or are new to MNScript. This thread doesn't explain every part of MNScript so you're expected to know the basics and logic of the programming language beforehand. If you aren't somewhat experienced, @Hydro has made a couple tutorials explaining the basics for you - you can find the links to the threads here. Anyways, let's move on.

Understanding How Networking Works

In order to make programs that use networking, you'll need to understand how it works. It's not the hardest concept to understand, although you will need to know some keywords.
  • IP Address - Abbreviation for Internet Protocol Address. This is the address of a device in a network and a different address is assigned to all devices connected to a network. An IP is usually displayed as 4 8-bit numbers(a number ranging from 0 - 255) separated by full-stops e.g. 175.132.87.59
  • Protocol - Protocols are described as a set of rules to follow for devices to communicate.
All devices connected to a network are assigned IP addresses, so that a network message has a destination and source address and doesn't go to a random computer instead. When a computer wants to send data to another computer, it has to set the destination of the message as the computer's IP address and also the protocol to send it with. Protocols are typically used to tell what data is being transmitted instead of having data which the other computer doesn't know what to do with. Once you have the destination IP address and the correct protocol which the other device should listening for, the message is sent to the destination computer and the data from one computer should be on another. Computers typically need software to tell what the net message contains and how to use the data.

Hopefully you now have a basic understanding of how data is transmitted through protocols and what IP addresses are and what they are used for.

Implementation in MNScript

Now with a brief understanding of the necessary information to use networking in MNScript, you should be ready to learn how to code a program which sends data from one terminal to another.

To start with, you'll need to memorise the essential steps for computers to communicate, these are relatively simple to grasp.
  1. Find the destination IP address to send the message to
  2. Start forming the message and state what protocol you want to send the message with
  3. Write the data to the message
  4. Send the message to the destination address
  5. Reading the message
Finding the Destination IP

There are a couple ways to find the destination ip address to send data to. Firstly, you could simply ask the owner of the second terminal for their ip address, however this requires contact between the computer owners which is inefficient. A better way to do this is using the built in feature of network binds. A bind in networking is very similar to DNS, which you've probably heard about; it's a way of using readable strings to request the unreadable ip address, this makes it a lot easier to remember the address and to easily reuse a script without having to manually put an address in.

The net library contains 2 methods which use binds to get an ip address, these are Net.BindAddress(string) and Net.IPFromNetworkBind(string). Net.BindAddress(string) is used for the device receiving net messages and allows other PCs to get their IP Address from a string. The parameter it takes is the string you want to associate your ip address with. Net.IPFromNetworkBind(string) is used on the device sending the data and receives the IP from a network bind which you can then send the net message to. The parameter is the bind you want the IP of.


Creating the message

You need to tell the message what protocol it will send on and where the data writing for said message starts. The only method to do this is with Net.Start(number). The parameter is the protocol you want to send this on, this can be any 4 or less digit number, make sure you remember this for the software that handles the incoming data. An example of this is simply 'Net.Start(30);'.

Writing Data in the message

Now that you have a net message and defined what protocol you want it to follow, you can now give it some data to be sent to the other computer. You can only send 3 types of data in MNScript and these are numbers, strings and booleans. When sending multiple pieces of data in one net message i.e 3 strings and 2 bools, you have to remember the order the data is written in, so that when you create a program to read the sent data on the other device you know what string is what bit of information. This is why protocols are important. You can simply write data to the net message by using Net.WriteString(string), Net.WriteNumber(number), and Net.WriteBool(bool). The parameter is just the data you want to send.

Sending the net message

Once you have all the data written in the message, you then actually have to send it to the other IP address. This isn't a difficult task as you gathered the IP in the first step, all you have to do is use the method Net.SendToAddress(string) and for the parameter just put the variable you assigned the IP to. Once this is called, the message is officially sent across the network to the destination you gave it.

Reading the message

Now a message has been sent to the destination computer, but it hasn't been told how to process this data that's arrived to it, so without any software interacting with it's ignored. However, software to detect net messages is possible to create and it can be hard to understand. To read these net messages you need to use the Event library, as this detects incoming net messages to your computer, the specific event you're looking to use is 'IncomingNTM' which supplies you with the protocol and the net message. You're given the parameters NTM and number - NTM is a class in the Net library and has 5 useful methods which you will need. The number is simply the protocol it was sent on.

Inside the IncomingNTM function, you'll need to verify that the incoming net message is the one you want to interact with, so you'd use an if statement to verify the protocol it was sent on by either using the number parameter given to you, or use the NTM given to you and use the method NTM.GetProtocol() to get the same result. Once verified that it's the correct net message, you use the NTM.ReadString()/NTM.ReadNumber()/NTM.ReadBool() to get the data in the NTM. If you have written multiple of a single type(i.e 2 strings), you use NTM.ReadString() again to access the next variable assigned when writing the message. If this is a hard concept to understand, examine the following example code and hopefully you will get your head around it. Finally with reading net messages, the source IP address is actually automatically written without having to write it! To access the source address of the net message, simply use NTM.GetSourceAddress() and it returns a string of the source address.


Simple Example Code

Hopefully after reading that text, you're able to comprehend the net library and have a decent understanding of how it works. I've written a bit of example code which should help you fully get your head around how to implement it into your own programs.
Code:
// Simple Online Messaging Program

// All the libraries we need
using System;
using Console;
using Net;
using Event;
using String;
using Player;

Player owner = System.GetUser();
Net.BindAddress(owner.GetName()); // Gets the ingame username, not the maxnet account and creates a bind to associate with

function SendNTM(string cmd)
{
  
    string[] message = String.Split(cmd, "-"); // means you have to type message-user-Hello World
    string prefix = "message";
    if(String.Lower(message[1]) == prefix) // if the first word of the command is message
    {
      
        string dest = message[2]; // Gets second element in the array(2nd word) which should be the username

        StringResult dest_exists = Net.IPFromNetworkBind(dest);
        if(dest_exists.GetResult()) // If the bind links to an address
        {
            dest = dest_result.GetString(); // Get Destination IP linked to the network bind
            Net.Start(72); // Creates a message with protocol 72
            Net.WriteString(owner.GetName()); // Writes a string to the message with the username of the sender
            Net.WriteString(message[3]); // Writes the actual data wanting to be send to the message
            Net.SendToAddress(dest); // Sends the net message to the address

            Console.WriteLine("Message Sent To " .. dest .. "!");
    }

    }
}
function IncomingNTM(number ptc, NTM ntm)
{
    if(ptc == 72)
    {
        string username = ntm.ReadString();
        string message = ntm.ReadString();

        Console.WriteLine(username .. ": " .. message);
    }
}

// Adds a listener for Incoming Net Messages
Event.AddListener("IncomingNTM", "NetListener", "IncomingNTM");

// Adds a listener for console commands
Event.AddListener("ConsoleCommand", "SendMessage", "SendNTM");

// An Infinite loop to tell if any events are occuring
while(true)
{
    Event.Process();
}

// I havent tested this, so be observant for any mistakes

Thanks for reading guys, if you find any mistakes DM me on discord, or if you have any queries just reply to this thread. this was written at 4AM then 11PM the following day, that's why my writing is so shit, promise
Thanks but I just don't know how to code ?