RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Hello World x 2

Talow

Sorceror
This will be a simple tutorial. The goal is simply to teach you how to output information.

Please note Hello World is not useful to send to anyone. This is just a common first step that many languages use as a first program to write. Reasons this is useful:
  • You can display all kinds of information
  • Debug your code to find and fix problems
  • Instruct a user how to use something (for instance using provoke, or inscription skills)
X 2. We will be repeating this tutorial two times to get the output in two way.

1: In the console.
Build the class. This is the foundation of nearly every script you write.
Code:
using System;
using Server;
 
namespace Server.HelloWorld
{
    public class Hello
    {
       
    }
}

That's the start of the code that we will build the rest of this on. We are now going to go over a method that is checked by the core for when the server starts up. This function is entitled Initialize. The method gets fired after the core compiles everything, I THINK and maybe someone can confirm for me that it also is fired after the world loads. We will be placing this function inside of our class "Hello".

Code:
public static void Initialize()
{
}

Now we get to the reason we are here, writing out the information to the console. There are two easy ways we can do this. We'll cover both:
  • Console.Write - Writes out the information. Makes no return after, unless you tell it to with excepted new line characters "\r\n"
  • Console.WriteLine - Writes out the information and makes a return after.
I prefer the WriteLine approach. This makes it easy to keep things separated and easier to read, the reason behind that is if you forget to format the Write, or format it badly, then it can output and you won't know where one thing ends and the next begins.

This code belongs in the "Initialize" Method.
Code:
Console.Write("Hello");
Console.Write(" World \r\n"); //please note the space is the
//first character in the quotes.
 
Console.WriteLine("Hello World");

You can see here that we are using a string, we don't need a variable to do this but we could use one instead if you wanted to. Once you start up the server this will print, on the console, Hello World, it will do so twice. The first time is done with the two Write methods. The second one is done with the WriteLine method.

2: In the game.
Code:
using System;
using Server;
using Server.Mobiles;
 
namespace Server.HelloWorld
{
    public class HelloGame
    {
        Initialize()
        {
        }
    }
}

We already covered this, the only difference is the class name and the extra using. We will also be using the "Initialize" method, but we will be using it to set up an event listener. this way we know when someone logs in. Once someone logs in we'll send them a message. Inside of the Initialize method lets put in our listener.

Code:
EventSink.Login += new LoginEventHandler( OnLogin );

So this tells the EventSkink.Login to add to it's existing listeners. It adds a listener of the type "LoginEventHandler". When it's fired, it will pass to the your method "OnLogin". It's really that easy but now we need that method, and it should be inside of the "HelloGame" class.

Code:
private static void  OnLogin( LoginEventArgs e )
{
}

So here we have the method and you can see that we get information with the method when it's called. The information we get are the arguments that come with "LoginEventArgs". We don't have to use that information, but we do need to accept the information. We will however be using that information to obtain the Mobile that has just logged in, inside of the OnLogin Method.

Code:
Mobile m = e.Mobile;

Here we took the arguments that we obtained from the Login event and found the mobile that has logged in. We'll now check to be sure there is a mobile assigned to this and then we'll send out the message to that mobile, right after we set the mobile inside of the OnLogin method.

Code:
if(m != null)
{
    m.SendMessage("Hello World");
}

The SendMessage is apart of Mobile and send a message to the mobile in the bottom left of the classic client, kind of like when you get the message about your skill or stat gain. Again here we use a string we create for the SendMessage's argument, but again you can get a variable in there too.
 

Talow

Sorceror
It would seem that I missed an important part in my tutorial, not for the tutorial itself but the code to make it work right. Vor showed me my mistake and now I'm going to show you:

Code:
using System;
using Server;
using Server.Mobiles;
 
namespace Server.HelloWorld
{
    public class HelloGame
    {
        public static void Initialize()
        {
        }
    }
}

I just simply forgot to put public static void in front of the initialize method.
 
Top