Client Login Portal   News Support
  • 2
  •   Request Server
  •   Control Panel
  •   Services
     Ultra-Host - Portal
    Ultra-Host - Portal
    Dear Guest! You are not registered or logged in! Click here to register or click here to login.

    You are not registered or logged in! Click here to register or click here to login.

    [TUTORIAL]Server optimisation (Read 46 times)
    Posted: February 3, 2020 16:51:19
    Offline blacklife.
    Premium User

    Posts: 8
    Private message
    [TUTORIAL]Server optimisation
    Hello! I've been searching for server optimisations and I was not content about the topics i just found out. So, there are some simple methods to optimise your script.
    1. If you are using samp timers you should definitely use y_timers. Ysi timers will reduce server's lag if you have a lot of timers and they will be more stable. There are 2 types of timers:
    Task: are called constantly at a given period.
    How to define them? There is an example:
    task SomeFunction[1000]() { and your code } . The tasks don't need any calling, they will begin on gamemode/sever start and are running until the server stops.
    ptask SomePlayerFunction[1000](playerid) { and your code } . You don't have to use anymore SetTimerEx.
    Timers: they need a calling.
    How to define them? An example:
    timer SomeTimer[500](playerid) { your code } . If you want that timer to run you have to call it. The are some ways to do that:
    repeat: the timer will repeat(exactly as you do with SetTimer("blabla", 1000, true)) until you stop it. Example: repeat SomeTimer();
    defer: this calls the function once after the time defined. Example: defer SomeTimer();
    normal call: Just use the function where you need so. For example: SomeTimer();
    How to stop a timer? Simply use stop: if you have defined your timer (new Timer:MyTimer) you can stop it on gamemodeexit or where you want the timer to stop like that: stop MyTimer; If you want to make a timer and verify it to see if it's active you should do that:
    new Timer:MyTimer[MAX_PLAYERS] = {Timer:-1, ...}; To check if the timer is active just use this condition: if(MyTimer[playerid] != Timer:-1).
    This check if the timer is active and will return true if so. To stop it you can do that: if(MyTimer[playerid] != Timer:-1) { stop MyTimer[playerid]; MyTimer[playerid] = Timer:-1; }
    To activate MyTimer to use a function I can simply do that: MyTimer[playerid] = repeat SomeTimer(playerid);
    You can edit repeating interval for one calling if you want so. For example i want SomeTimer to run at 2000 miliseconds instead of 1000. So, i wil use: repeat SomeTimer [2 * 1000]();
    I highly reccomend using TASKS ONLY for GLOBAL timers and timers for PLAYER timers.

    2. Define MAX_PLAYERS to your server's slots. How to do that? Simply, at the beginning of your gamemode use:
    #undef MAX_PLAYERS
    #define MAX_PLAYERS MySlots(for example, 50)

    3.Use foreach instead of MAX_PLAYERS or GetPlayerPoolSize loops. How to do that? Include foreach at the top of your gamemode(#include foreach) and use it where your are doing a loop with MAX_PLAYERS. How to do that? instead of for(new i; i< MAX_PLAYERS; i++) use foreach(new i: Player).

    4.Use GetVehiclePoolSize/Iterators instead of MAX_VEHICLES loops. How to do that? Instead of for(new i; i<MAX_VEHICLES; i++) use for(new i = 0, j = GetVehiclePoolSize(); i < j; i++) or create an iterator. You have to include y_iterate at the top of your gamemode(#include y_iterate) and y_hooks(#include y_hooks) so you can use them. To create an iterator you simply use: new IteratorerverVehicles[MAX_VEHICLES]. This will add an iterator named ServerVehicles with max vehicle containing of 2000(or your max_vehicles value). To add vehicles in the iterator just use: Iter_Add(ServerVehicles, vehicleid); to remove: Iter_Remove(ServerVehicles, vehicleid).
    Iterator functions:
    Add: this will add objects in the iterator. Iter_Add(itername, iterobject);
    Remove: this will remove an object in the iterator. Iter_Remove(itername, iterobject);
    Free: checks if there is a free object slot in the iterator(if i have 200 vehicles in the iterator and i will add one more my vehicle id will be 201, so less than 2000).
    Contains: checks if there is an specific object in the iterator. It's good for checking before deleting something from the iterator. Iter_Contains(itername, iterobject);
    Count: counts the objects in the iterator. Iter_Count(itername);

    5. Use lower strings. If you have a text of 128 chars don't make a string of 200. Use it as 128. You have some chars limits in chat/message. Check this:

    That's all I have in mind for now. Also, you could use C# or C++ to script your gamemode for scratch, but that's for another tutorial. I hope i helped you!

    Posted: February 3, 2020 17:58:14
    Offline SebastianOV

    Posts: 1
    Private message


    - Online servers: 1,491
    - Registered users: 116,703
    - Uptime: 7 years and 14 days.

    © Ultra-Host v9.1.1 (2013-2020) - All Rights Reserved.