[Tutorial] Creating a simple random message system!
#1

Okay so this is kind of my first tutorial so I am not so sure how things work out but I will try my best to help you guys understand the idea/concept of random messaging, so here we go.


Okay so normally at first we need a callback to place our code, callbacks are like: OnGameModeInit, OnPlayerConnect, OnPlayerDisconnect,OnPlayerUpdate... and probably most things that start with 'public' and has a forward (also known as a prototype in C or C++ / Pawno programming language is based on C/C++) for more illustration like
pawn Код:
forward GoingToCallThisFor(playerid);
public GoingToCallThisFor(playerid)
{
     // Your codes here
}
Side note: some people will say the callbacks like OnPlayerConnect doesn't have a forward, do I have to add that?! No you don't, the a_samp include already has it.


So we will start our work in the OnGameModeInit callback as I think it's better/fits the concept.

pawn Код:
public OnGameModeInit()
{
    return 1;
}
So alright you guys would be like okay if I just go into that callback and add my message that I want to send (*using SendClientMessageToAll function* - I'll explain what a function is later on) it will just send messages to the player connected/online but come on the server/script just went on who would be able to read this?

It's one easy pie to handle and believe this can be the most easiest thing to do a tutorial about and yet it's pretty much used in most servers.

Before I go deeper, a function is a line of code that you make it do something for the player or like creating a vehicle, you can use for example:
pawn Код:
SetPlayerHealth(/*the playerid id you want*/ 0, /*amount*/ 100);
There, you made function/adjusted something with that line of code.


So yeah I kept going on callbacks/function and probably you guys will be like come on just hurry up, alright then let me 'start' the tutorial. We are going to use a function called SetTimer
pawn Код:
SetTimer(the function NAME to be done, time before it gets done (In milliseconds), repeating case(1 = true = yes / 0 = false = no repeating)
Okay so you guys probably noticed that I wrote 'NAME' in capitals, well because normally in scripting/coding, name refers to a string mostly. A string is like "asdomsaoida" / cutting it up: you have to put the callback you want to execute in " " quotation marks for the pawno to look for it.

pawn Код:
OnGameModeInit()
{
     SetTimer("RandomMessage", 30000, true);
     return 1;
}
And now, we are going to create our own callback that needs to be executed. Be-aware that when creating a new callback you don't put it under another callback like:
pawn Код:
public OnGameModeInit()
{
     public OnPlayerConnect(playerid)
     {
        return 1;
     }
    return 1;
}
^ This is totally wrong.

Okay so where do we place it? Well you place it between other callbacks (if you wish to add your callbacks at the end of samp callbacks as you can recognize the created ones it's okay too)

pawn Код:
public OnGameModeInit()
{
    SetTimer("RandomMessage", 30000, true); // Setting some time so players would connect
//      And then it will send the message to all after 30000 MILLIseconds
    return 1;
}
public OnGameModeExit()
{
    return 1;
}
public RandomMessage()
{
    SendClientMessageToAll(-1, "Hello and welcome to my server!"); // The code that will be executed when
// the 30000 miliseconds pass.
    return 1;
}
Okay so guys I believe you got the concept of sending messages to the player in the most simplest way I can actually explain, it's pretty much easy guys and I will be soon making more tutorials in the future I just hope this one was helpful enough for anyone who needs help. If you want to how to add additional messages without all of them be sent together at a time, create new timer, new callback and so on, but not too much in order not to consume alot of resources.

Small note:A forward/prototype of the function has to be the same name WITH the same parameters but has a semi-colon " ; " at the end, while the public/callback itself where code executes, doesn't have a semi-colon.

Best regards,
Matt Tucker.

Reply


Messages In This Thread
Creating a simple random message system! - by MattTucker - 21.04.2014, 15:39
Re: Creating a simple random message system! - by Mckarlis - 21.04.2014, 16:06
Re: Creating a simple random message system! - by MattTucker - 21.04.2014, 16:07
Re: Creating a simple random message system! - by Vince - 21.04.2014, 17:34
Re: Creating a simple random message system! - by NaClchemistryK - 22.04.2014, 08:48

Forum Jump:


Users browsing this thread: 1 Guest(s)