open.mp forum
split pawn codes - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: General (https://forum.open.mp/forumdisplay.php?fid=19)
--- Forum: Programming (https://forum.open.mp/forumdisplay.php?fid=56)
--- Thread: split pawn codes (/showthread.php?tid=2192)



split pawn codes - redex - 2021-07-28

hi.

i am working on a sa-mp gamemode project, its going to take about  code line, i was wondering is there any way to split the code, for exp put the JOBS related codes to another file and include it , so if i need to edit something about jobs i dont need to check hell a lot code lines to fix a problem!



how can i do that? is it good?

can it cause bad effects like lags or some other bad effects to server?

if u know anything please let me know, thanks!


RE: split pawn codes - Kwarde - 2021-07-28

Well off course! And you absolutely should do that! You're also already doing it in fact when including includes.. Thus use #include to include different files.

#include <someInclude> would attempt to include "PATH_TO_INCLUDES_DIRECTORY/someInclude.inc"

#include "someInclude.inc" or #include "someInclude" would attempt to include "PATH_RELATIVE_FILE/someInclude.inc" (you could use different extensions. Eg. gamemode sf-cnr uses .pwn for all includes -- don't, though. ".inc" is the correct file extension)



For example:



./gamemodes/gamemode.pwn



Code:
#include <a_samp>



#include "src/assembly.inc"



public OnGameModeInit()

{

??? print("Final OnGameModeInit() call!");

??? return 1;

}



./gamemodes/src/assembly.inc

Code:
#include "src/init.inc"

#include "src/server/mysql.inc"



./gamemodes/src/init.inc

Code:
#include <YSI_Coding\y_hooks>



hook OnGameModeInit()

{

??? print("First OnGameModeInit() call! Start loading gamemode");

?? return 1;

}



./gamemodes/src/server/mysql.inc

Code:
#include <YSI_Coding\y_hooks>



hook OnGameModeInit()

{

??? print("Second OnGameModeInit() call! Setting up MySQL connection");

??? return 1;

}

Etc..



There are probably 'rules' around as how to name files (eg. using 'header.inc' and 'impl.inc' files) but I'm too noob for that :-)

Here are a few gamemodes that do use multiple files in case you need examples.?

https://github.com/PatrickGTR/gta-open

https://github.com/MaxAndolini/NG-RP

https://github.com/zeelorenc/sf-cnr



Ultimately this will all be combined into one file (by the pre-processor) - including the plugins. It has no effect on the performance of the server.

If you're curious and want to see that for yourself, compile with option "-l" (either put #pragma option -l in your script (do use the latest compiler) or append it to pawncc args).

This will show the output of the stage after the pre-processor's output and before compiling it to assembler code/binary file (amx) in a .asm file. This for example (note that this doesn't make much sense):



./script.pwn

Code:
#include "myInclude.inc"



#pragma option -l



#define MY_CONSTANT 5



main()

{

??? print("MY_CONSTANT IS "#MY_CONSTANT);

}



./myInclude.inc

Code:
native print(const string[]);

Would output something like this: (without "#file" and "#line" outputs: when using -l it adds that to the script to show what's where)

Code:
native print(const string[]);



main()

{

??? print("MY_CONSTANT IS "#5);

}



RE: split pawn codes - Pinch - 2021-07-28

https://github.com/Southclaws/sampctl/wiki/Modern-Pawn

https://github.com/TradeWars/gamemode/blob/master/docs/Style-Guide.md


RE: split pawn codes - redex - 2021-07-29

thank u guys it was really helpful 귪