Building a gamemode with includes only?
#1

Hello.
I'm trying to create a gamemode, and instead of writing all the script in the gamemode's .pwn file itself, what I'm doing is leaving that .pwn file almost blank, creating my own includes with ALS callback hooks, and then adding those includes in the .pwn file. For example I have an include for the player classes, another for the vehicle spawns, another for the gang zones, another for weapons, another for detecting certain cheats...
I'm doing this because I find several advantages in it:
  1. I can have my code more organized because different things are each one in its own file, instead of all of them mixed up.
  2. It allows me to more a modular design, like a Lego, in which I can add or remove certain features by just adding or removing an include, instead of having to dive in an ocean of hundreds of lines. It's more plug and play like.
  3. If I want to change something of a certain feature I only have to edit an include, and I can locate the necessary code lines more easily in a small file than in a whole gamemode.
  4. If someday I want to create another gamemode, but import some features of my current one, I just have to add the necessary includes.
However, while this way is easier to work, my question is: does it have a negative impact on the script's performance?
Reply
#2

It will probably be slower yes, because CallLocalFunction also takes time to execute.
Reply
#3

No it wont, if you include some code it is extactly the same if you would copy paste the code into your mode

Maybe you check out this thread Gamemode Script Layout
Reply
#4

My question comes specially for the following: some of my includes have others of them inside, so it could be like adding an include more than once in the gamemode. For example, I have an include for the player classes that has the following:

pawn Код:
#if defined _classselection_included
    #endinput
#endif
#define _classselection_included

#include <a_samp>

new stock bool:inclassselection[MAX_PLAYERS];
    // True when the player is in the class selection screen, and false when he's not.

public OnGameModeInit()
{
    gclasel_HasCB[0] = funcidx("clasel_OnPlayerRequestClass") != -1;
    gclasel_HasCB[1] = funcidx("clasel_OnPlayerSpawn") != -1;
    if (funcidx("clasel_OnGameModeInit") != -1)
    {
        return CallLocalFunction("clasel_OnGameModeInit", "");
    }
    return 1;
}

#if defined _ALS_OnGameModeInit
    #undef OnGameModeInit
#else
    #define _ALS_OnGameModeInit
#endif
#define OnGameModeInit clasel_OnGameModeInit
forward clasel_OnGameModeInit();

public OnPlayerRequestClass(playerid,  classid)
{
    inclassselection[playerid] = true;
    if (gclasel_HasCB[0])
    {
        return CallLocalFunction("clasel_OnPlayerRequestClass", "ii",playerid,  classid);
    }
    return 1;
}
#if defined _ALS_OnPlayerRequestClass
    #undef OnPlayerRequestClass
#else
    #define _ALS_OnPlayerRequestClass
#endif
#define OnPlayerRequestClass clasel_OnPlayerRequestClass
forward clasel_OnPlayerRequestClass(playerid,  classid);

public OnPlayerSpawn(playerid)
{
    inclassselection[playerid] = false;
    if (gclasel_HasCB[1])
    {
        return CallLocalFunction("clasel_OnPlayerSpawn", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerSpawn
    #undef OnPlayerSpawn
#else
    #define _ALS_OnPlayerSpawn
#endif
#define OnPlayerSpawn clasel_OnPlayerSpawn
forward clasel_OnPlayerSpawn(playerid);
and I have another include that has the following:

pawn Код:
#if defined _gryfunctions_included
    #endinput
#endif
#define _gryfunctions_included

#include <a_samp>
#include <classselection>

stock killplayer(playerid)
{
    if(inclassselection[playerid] == false)
        // Array from the classselection include. This line means that the player is NOT in the
        //  class selection screen.
    {
        SetPlayerHealth(playerid, 0.0);
        return 1;
    }
    else
        // The player is in the class selection screen.
    {
        return 0;
    }
}

stock slap(playerid, height)
{
    if(inclassselection[playerid] == false)
        // The player is NOT in the class selection screen.
    {
        new Float:x, Float:y, Float:z;
        GetPlayerPos(playerid, x, y, z);
        SetPlayerPos( playerid, x, y, (z + height) );
        SetCameraBehindPlayer(playerid);
            // I add this line just in case the player is slapped to a great height.
        return 1;
    }
    else
        // The player is in the class selection screen.
    {
        return 0;
    }
}
As you can see, the gryfunctions include has the classselection include inside. So if now I open the .pwn file of my gamemode and I add the following:

pawn Код:
#include <a_samp>
#include <classselection>
#include <gryfunctions>
it will be like having the classselection include twice. And since this include has ALS callback hooks, will those hooked callbacks be called twice too?
Reply
#5

These wont be included twice since the compiler creates per include one define "_inc_" + filename.
Extactly the same as you do in the first 4 lines (you can remove these).

The only thing you need to look after is that none of your includes use the same name.
Reply
#6

Ok, thank to all of you for your replies.
Reply
#7

Quote:
Originally Posted by ******
Посмотреть сообщение
Anything takes time to execute, the difference is tiny. However, I have written but not yet released, a new hook method that is FASTER than even standard function calls!
Lol, that impressed me!
Reply
#8

Quote:
Originally Posted by ******
Посмотреть сообщение
Nice, thank you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)