[Include] script_compatiblity.inc - Make scripts run for both filterscripts and gamemode without extra defines.
#1

script_compatibility.inc
Version - 1.0.1
Last update : 2nd of February, 2017
Introduction

This include enables your script to be easily compatible with gamemodes or filterscripts without any need of extra defines. It is mainly created for the includes that requires (#define FILTERSCRIPT) to make it run on filterscripts. Using this include, you no longer need to have such defines. Those includes using this doesn't require (FILTERSCRIPT) to be defined above your filterscript.

This include replaces the use of these callbacks : OnFilterScriptInit, OnFilterScriptExit, OnGameModeInit and OnGameModeExit. Instead, you'll need to use only the two callbacks that this include provides : OnScriptInit and OnScriptExit.


By using this include, you don't even have to define an entry point (main) on your gamemode which allows you to easily switch over filterscripts and gamemodes.

NOTE: You must define OnScriptInit and OnScriptExit in case if you're using this include. Simply remove OnFilterScriptInit, OnFilterScriptExit, OnGameModeInit and OnGameModeExit.
NOTE 2: Even though it replaces the above mentioned callbacks, you can still use them but it's totally unnecessary.
NOTE 3: If you're using YSI, you don't need this include since YSI library provides these callbacks already.

Functions & Callbacks

This is what the include provides currently:
pawn Код:
Callbacks:
public OnScriptInit(); (replaces OnFilterScriptInit and OnGameModeInit)
public OnScriptExit(); (replaces OnFilterScriptExit and OnGameModeExit)

Function:
native GetInitMode();
//Returns SCRIPT_MODE_FS if running as filterscript.
//Returns SCRIPT_MODE_GM if running as gamemode.

Constants by their default values:

SCRIPT_MODE_FS = 1
SCRIPT_MODE_GM = 2
Usage

Suppose you have an include that requires FILTERSCRIPT to be defined to know it should run for filterscript or not.
pawn Код:
//without using script_compatibility
#include <a_samp>


#if defined FILTERSCRIPT
public OnFilterScriptInit() {

    //codes
    //hooks
    return 1;
}

public OnFilterScriptExit() {

    //codes
    //hooks
    return 1;
}

//hooking these

#else //if FILTERSCRIPT is not defined
public OnGameModeInit() {

    //codes
    //hooks
    return 1;
}

public OnGameModeExit() {

    //codes
    //hooks
    return 1;
}

//hooks
#endif
By using this include, the above code simply becomes:
pawn Код:
#include <a_samp>
#include <script_compatibility>

public OnScriptInit() {

    //codes
    //just hook OnScriptInit like a usual callback.
    return 1;
}

public OnScriptExit() {

    //codes
    //just hook OnScriptExit like a usual callback.
    return 1;
}
I've also posted an example along with download, feel free to run it either as gamemode or filterscript. It runs showing whether it's running as FS or GM.


Changelog

script_compatibility - v1.0.1:

- Callbacks provided by this include are now optional. So there won't be any warnings if one among them is left unused.

script_compatibility - v1.0:

- Initial release.


Download

Github : https://github.com/Lordzy/script_compatibility
Raw source : https://raw.githubusercontent.com/Lo...patibility.inc
Example : https://raw.githubusercontent.com/Lo...mpleFSorGM.pwn
Reply
#2

A simpler method (as I think)

However good job
Reply
#3

Quote:
Originally Posted by OstGot
Посмотреть сообщение
That method can also be used but it seems to be a hassle when it comes to callbacks that requires return values according to filterscript or gamemode. You'll have to define such callbacks twice - for filterscript and for gamemode since the state used isn't retrieved.
Reply
#4

This looks kinda good, I'll try it out later and share my experience !
Reply
#5

Won't work right. OnGameModeInit is called in both fitlerscripts and gamemodes.

FS:
pawn Код:
#include a_samp

main() return print("FS - Main");
public OnGameModeInit() return print("FS - GameModeInit");
public OnFilterScriptInit() return print("FS - FilterScriptInit");
GM:
pawn Код:
#include a_samp

main() return print("GM - Main");
public OnGameModeInit() return print("GM - GameModeInit");
public OnFilterScriptInit() return print("GM - FilterScriptInit");
Log:
Quote:

----------
Loaded log file: "server_log.txt".
----------

SA-MP Dedicated Server
----------------------
v0.3.7-R2, ©2005-2015 SA-MP Team


Server Plugins
--------------
Loading plugin: crashdetect
CrashDetect v4.15.1 is OK.
Loaded.
Loaded 1 plugins.


Started server on port: 7777, with maxplayers: 50 lanmode is OFF.


Filterscripts
---------------
Loading filterscript 'fsgm.amx'...
FS - FilterScriptInit
Loaded 1 filterscripts.

GM - GameModeInit
FS - GameModeInit
GM - Main
Number of vehicle models: 0
reloadfs fsgm
Filterscript 'fsgm.amx' unloaded.
FS - FilterScriptInit
Filterscript 'fsgm.amx' loaded.
gmx
GM - GameModeInit
FS - GameModeInit
GM - Main
Number of vehicle models: 0


EDIT: Ah, didn't catch the purpose of "if(!L_INIT_MODE)". Since FilterScriptInit is called first it will know it's an FS.
Reply
#6

Cool, but IMO this is flexible but I'd still use the original way.

Good job though.
Reply
#7

Good job there, you're the Lordz
Reply
#8

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Won't work right. OnGameModeInit is called in both fitlerscripts and gamemodes.

FS:
pawn Код:
#include a_samp

main() return print("FS - Main");
public OnGameModeInit() return print("FS - GameModeInit");
public OnFilterScriptInit() return print("FS - FilterScriptInit");
GM:
pawn Код:
#include a_samp

main() return print("GM - Main");
public OnGameModeInit() return print("GM - GameModeInit");
public OnFilterScriptInit() return print("GM - FilterScriptInit");
Log:



EDIT: Ah, didn't catch the purpose of "if(!L_INIT_MODE)". Since FilterScriptInit is called first it will know it's an FS.
I wouldn't suggest anyone to use the default init/exit functions once they're using this. The main purpose of this include is to make it compatible and avoid collision.

Quote:
Originally Posted by Kar
Посмотреть сообщение
Cool, but IMO this is flexible but I'd still use the original way.

Good job though.
By "original way" if you're referring to those PAWN directive checks, I wouldn't suggest that. Because most of the users would forget to define FILTERSCRIPT and end up having no idea why their scripts won't work properly. I would at least recommend to have a boolean variable to handle the init/exit on includes in case if this include isn't being used. It helps a lot like seriously - I got back to SAMP after an year and I wanted to test my own include (L_SAM) in a filterscript. I kept on wondering why it didn't work properly and after hours, I realized that it wasn't being initialized properly since I had no FILTERSCRIPT defined.
Reply
#9

Include updated - v1.0.1 (optional update)

Fixed error popping out when any of the callbacks provided by the include isn't defined. The errors were meant on purpose as a reminder to the ones using it earlier. Since there's a possibility that it's function could be used without it's callbacks, I felt that it should be fixed.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)