[Tutorial] Method to save script memory
#1

Hello everyone, have a nice day!

Let me write something I came up today, and though I could share it, pardon if such thing is already in use and so on, but here it is.


Lets say you use /freeze cmd, usually you would use

new Freezed[MAX_PLAYERS];

and later Freezed[givenplayerid] = 1; and so on, to check if player is freezed or no.
Which would be 500 cells by default, but thing is, you will never freeze all 500 players at once, so here is my solution.


You make new Freezed[30] = { -1, ...}; where 30 is your thoughs how much players could be freezed at once, as I said you will never freeze all 500 at once, even 100 is less then 500.


and make two custom functions

pawn Код:
IsFreezed(playerid)
{
    for(new i; i < sizeof(Freezed); i++)
    {
        if(playerid == Freezed[i]) { return 1; } // make a loop to check if player is freezed in any slot
    }
    return 0; // If Loop doesnt find playerid, means player was never freezed
}
SetFreezed(playerid)
{
    for(new i; i < sizeof(Freezed); i++)
    {
        if(playerid == Freezed[i]) { Freezed[i] = -1; return 0; } //Unfreeze if playerid was ferezed and reset freeze status
        else { Freezed[i] = playerid; return 1; } // Else set freezed
    }
    return 0;
}
And to keep it tracked at OnPlayerConnect you can add if(IsFreezed(playerid)) { SetFreezed(playerid); } so you reset a slot.


you can also use if(SetFreezed(playerid)) to check if there is any free slot.

This same method you can use for vehicles, to check some stats which won't be used for all cars, such as cargo load for truckers, or Trunk content as bikes doesn't have trunks.
Reply
#2

I don't even use an array for freezing people, but sure. Like what if your character is stupid but "Freezed[playerid]" is set to 0? You did an animation that bugged or something and can't come move. Then you have to waste time freezing yourself. Just saying. :P

Anyways nice tutorial.
Reply
#3

Yep that way works too!

Good tutorial.
Reply
#4

Quote:
Originally Posted by Hansrutger
Посмотреть сообщение
I don't even use an array for freezing people, but sure. Like what if your character is stupid but "Freezed[playerid]" is set to 0? You did an animation that bugged or something and can't come move. Then you have to waste time freezing yourself. Just saying. :P

Anyways nice tutorial.
Freeze isn't best place where to use it, but for example, I made that there is water level in the firetrucks, and I wont set water level for each vehicle, if I got only some 10 firetrucks, so I set water level only for those 10 slots and later get water level from assigned slot.
Reply
#5

Or just
#undef MAX_PLAYERS
#define MAX_PLAYERS 50
// here put your max players

at the beginning of the script
Reply
#6

Quote:
Originally Posted by Rock
Посмотреть сообщение
Or just
#undef MAX_PLAYERS
#define MAX_PLAYERS 50
// here put your max players
If your max players is 50, you don't need to save memory anyways, and yet again, are you planning to freeze all 50 ?
If you got 50 players , you can set that Freeze to 10.
Reply
#7

good tutorial
Reply
#8

Quote:
Originally Posted by Rock
Посмотреть сообщение
Or just
#undef MAX_PLAYERS
#define MAX_PLAYERS 50
// here put your max players

at the end of the script
Read again tutorial, you didn't understand what he wanted to explain.
Reply
#9

Quote:
Originally Posted by ikey07
Посмотреть сообщение
If your max players is 50, you don't need to save memory anyways, and yet again, are you planning to freeze all 50 ?
If you got 50 players , you can set that Freeze to 10.
If I want to freeze all of them and there are more than 10 people connected what do I do? Change the value in the script and gmx the hole server just to freeze 2 more people?

Come on guys, this can't be your primary concern about saving memory, concentrate more on other parts of the code which require real optimisation instead of losing time with MAX_PLAYERS.

Quote:
Originally Posted by StreetGT
Посмотреть сообщение
Read again tutorial, you didn't understand what he wanted to explain.
I understand clearly, and I assume you already read this reply with my opinion.
Reply
#10

How you use this tutorial its up to you, as I said for vehicles this can be really useful as all vehicles will never be everything, I run a server with avarage 60 players online for 3 years, and I have never needed to freeze more then 15 players at once.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)