SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 29/12/2015, 08:23 PM   #1
PatrickGTR
Little Clucker
 
PatrickGTR's Avatar
 
Join Date: Sep 2010
Location: United Kingdom
Posts: 18
Reputation: 53
Default Modular Programming

What is modular programming?
Modular programming is a process of breaking down a code depending on its category to make the code reusable for future projects (avoid re-inventing the wheel) as well as making the code easier to read and track down mistakes. Modular programming comes handy if you are writing big projects, there's no point of modularizing your code if it doesn't consists of 5,000 lines as this can be done in one file but once again I am not your boss therefore this could be done on short codes but I see no point of doing so (personal opinion).


Advantages
  • Code reusability - This makes your life easier, if you're creating a new project that has similar needs your module can come handy and be re-used in this situation and saves you time! (wasting time on creating the code again from scratch).
  • Code readability - Modular programming can make your code much easier to read with few lines in each modules!
  • Ease of debugging and modifying - A modularized code are generally easier to debug than a code that has been done in ONE file. Why is it easier to debug? - Codes are isolated in a module and once a faulty code has been identified, fixing can be very easy. Modifying can come handy if a module has to be used once again! simple modification can be done and the module can be re-used again!
  • Multiple developers - If you are working as a team, each developer can work on a different module and this leads to faster development!
    • Each developer can use their expertise to work on a module! Maths, GUI, commands, arrays, checkpoints, etc...
Disadvantages
  • Variable names - each variable name should have a unique name or else it can cause warning 219: local variable "variable_name" shadows a variable at a preceding level
    • A fix for this is use static to declare the variable as a local variable yet you still need to use a non-generic variable name to avoid conflicts with other modules and released includes.
    • Another fix if a variable is declared locally, you can create a function if it needs to be used throughout the code.
  • Proper documentation - If you are working as a team, modules should be documented thoroughly for other developers to understand the code as well. (This also goes to codes that are going to be released in forums)
  • Hooking a function that is required to be re-used - This can be repetitive and hook should have a unique prefix. Writing the directives can be repetitive but this is why COPY and PASTE exists. Other solution is using y_hooks and including it every module. For y_hooks example see first 2 examples.

Examples
Editor that supports multi-tasking
Notepad++ for PAWN - Slice
Sublime Text for PAWN - Southclaw
Atom for PAWN - Kaperstone

NOTE
You have to plan how you want to lay your code out or else it will be a disaster and you will be lost.


Layout
pawn Code:
- gamemodes
    - main.pwn
        - utils
            - colour.pwn
            - math.pwn
            - string.pwn
        - core
            - player
                - core.pwn
                - account.pwn
            - server
                - core.pwn
                - vehicles.pwn

pawn Code:
#include <a_samp>

//Modules

//Utils
#include "utils/colour.pwn"
#include "utils/math.pwn"
#include "utils/string.pwn"

//Player
#include "core/player/core.pwn"
#include "core/player/account.pwn"

//Server
#include "core/server/core.pwn"
#include "core/server/vehicles.pwn"

OR

pawn Code:
- includes
    - main.pwn
        - utils
            - colour.pwn
            - math.inc
            - string.inc
        - core
            - player
                - core.inc
                - account.inc
            - server
                - core.inc
                - vehicles.inc

pawn Code:
#include <a_samp>

//Utils
#include <utils/color>
#include <utils/math>
#include <utils/string>

//Player
#include <core/player/core>
#include <core/player/account>

//Server
#include <core/server/core>
#include <core/server/vehicles>


How does it work together?

Include is a directive that binds the contents of the module/second file together and insert it into the original file although the IDE doesn't read it this way but the compiler reads it like the code is in one-file.

enums.pwn
pawn Code:
enum E_PLAYER_DATA {
    plyUsername[MAX_PLAYER_NAME],
    plyPassword[65],
    plyKills,
    plyDeaths
}

new plyData[MAX_PLAYERS][E_PLAYER_DATA];

main.pwn
pawn Code:
#include <a_samp>

#include "core/player/enums.pwn"

new
    resetPData[E_PLAYER_DATA];

public OnPlayerConnect(playerid) {
    plyData[playerid] = resetPData; //sets all the value to 0.

    //Load everything here.

    return 1;
}

public OnPlayerDisconnect(playerid, reason) {
   
    //save everything here.

    plyData[playerid] = resetPData; //sets all the value to 0.
    return 1;
}

What actually happens
pawn Code:
#include <a_samp>

//this is enums.pwn
enum E_PLAYER_DATA {
    plyUsername[MAX_PLAYER_NAME],
    plyPassword[65],
    plyKills,
    plyDeaths
}

new plyData[MAX_PLAYERS][E_PLAYER_DATA];

new
    resetPData[E_PLAYER_DATA];

public OnPlayerConnect(playerid) {
    plyData[playerid] = resetPData; //sets all the value to 0.

    //Load everything here.

    return 1;
}

public OnPlayerDisconnect(playerid, reason) {
   
    //save everything here.

    plyData[playerid] = resetPData; //sets all the value to 0.
    return 1;
}

How does hooking work?
The modules should be done in a sequence in order for it to function properly. For example you are trying to open a database in main.pwn and you want to create a table of accounts in accounts.pwn to be able to do that. You
have to hook OnGameModeInit before every module and y_hooks (if you're using y_hooks). Example shown below.

main.pwn
pawn Code:
#include <a_samp>

/* -
    How it happens behind the console
    --------------------------------------
    * OnGameModeInitEx - first
        * hooks of OnGameModeInit - second
    --------------------------------------
    "OnGameModeInitEx" will be called first to avoid crashes and confusion within the hooks.
    Everything that you have to do regarding OnGameModeInit
    You have to place them in "OnGameModeInitEx" to make sure other hooks will not mess up on start up.
*/


public OnGameModeInit()
{

    OnGameModeInitEx();

    #if defined main_OnGameModeInit
        return main_OnGameModeInit();
    #else
        return 1;
    #endif
}
#if defined _ALS_OnGameModeInit
    #undef OnGameModeInit
#else
    #define _ALS_OnGameModeInit
#endif
 
#define OnGameModeInit main_OnGameModeInit
#if defined main_OnGameModeInit
    forward main_OnGameModeInit();
#endif

#include <YSI\y_hooks> //The y_hooks hook of OnGameModeInit will be called after the hook in main.pwn is called.
 
OnGameModeInitEx()
{
    db_open("server.db");
}

//We don't need to hook this because OnGameModeExit() hooks will be called first therefore the main OnGameModeExit() will be called last and this is what we need.
public OnGameModeExit()
{
    db_close(DB:0);
    return 1;
}


What happens behind?
pawn Code:
- hook of OnGameModeInit() calling OnGameModeInitEx() in main.pwn - This will be called first all the time!
- hook of OnGameModeInit() in different modules.
    - The hooks are called depending on the suquence of the module.

    For example
    - main.pwn
    - players.pwn
    - servers.pwn
    - checkpoints.pwn

    - OnGameModeInit() hook will be called first in main.pwn
    - OnGameModeInit() hook will be called second in players after main.pwn
    - OnGameModeInit() hook will be called third in servers after players.pwn
    - OnGameModeInit() hook will be called fourth in checkpoints after servers.pwn

Last edited by PatrickGTR; 30/12/2015 at 01:14 PM.
PatrickGTR is offline   Reply With Quote
Old 29/12/2015, 08:27 PM   #2
CrazyChoco
Gangsta
 
Join Date: Feb 2012
Posts: 896
Reputation: 35
Default Re: Modular Programming

This is good. It would suit anyone who is considering the programming industry.
CrazyChoco is offline   Reply With Quote
Old 29/12/2015, 09:03 PM   #3
Sasino97
Banned
 
Join Date: Dec 2010
Location: Soon Banned...
Posts: 1,396
Reputation: 127
Default Re: Modular Programming

Good tutorial
It would be a nice thing if people started to modular program, there are lots of good sà-mp scripters who still don't understand the advantage of dividing their project into modules, ànd thry publish huge gamemodes with 20000 lines of code
Sasino97 is offline   Reply With Quote
Old 29/12/2015, 09:17 PM   #4
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,854
Reputation: 1292
Default Re: Modular Programming

This only explains the concept not the implementation 1-star from me because people will get to the end then ask "okay what do I do now?" it's like you left out 75 percent of the tutorial. It's a great start nice and neat but needs some effort to be useful.
Pottus is offline   Reply With Quote
Old 29/12/2015, 09:28 PM   #5
Dayvison_
High-roller
 
Dayvison_'s Avatar
 
Join Date: Nov 2013
Posts: 1,318
Reputation: 200
Default Re: Modular Programming

PBP compiler by Slice, is a very good tool for this.
__________________


Dayvison_ is offline   Reply With Quote
Old 30/12/2015, 09:13 AM   #6
VincenzoDrift
Little Clucker
 
VincenzoDrift's Avatar
 
Join Date: Jun 2014
Posts: 47
Reputation: 3
Default Re: Modular Programming

Good tutorial +rep
__________________
Sorry for my bad english
VincenzoDrift is offline   Reply With Quote
Old 30/12/2015, 01:15 PM   #7
PatrickGTR
Little Clucker
 
PatrickGTR's Avatar
 
Join Date: Sep 2010
Location: United Kingdom
Posts: 18
Reputation: 53
Default Re: Modular Programming

Updated, explained how the modules binds together and how the hooking works in-order depending on module order.
PatrickGTR is offline   Reply With Quote
Old 30/12/2015, 01:22 PM   #8
[XST]O_x
High-roller
 
[XST]O_x's Avatar
 
Join Date: Aug 2009
Posts: 1,953
Reputation: 90
Default Re: Modular Programming

This is great. I've been trying to understand how to break down a gamemode into several filterscripts instead of one large, 20k+ lines script. Thank you very much.
[XST]O_x is offline   Reply With Quote
Old 30/12/2015, 01:32 PM   #9
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 695
Reputation: 121
Default Re: Modular Programming

Okay this was actually really great, but I tried it and well ... obviously failed xD
I got an error not recognizing my main enum (PlayerI), If I move my enums to an include file it will be solved right ?
Oh and another question, is it better if I break down my enums into different modules or should I just put them all in one big file called header ? (because either way I should include all headers first right ?)
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 30/12/2015, 02:47 PM   #10
PatrickGTR
Little Clucker
 
PatrickGTR's Avatar
 
Join Date: Sep 2010
Location: United Kingdom
Posts: 18
Reputation: 53
Default Re: Modular Programming

Quote:
Originally Posted by PrO.GameR View Post
Okay this was actually really great, but I tried it and well ... obviously failed xD
I got an error not recognizing my main enum (PlayerI), If I move my enums to an include file it will be solved right ?
Oh and another question, is it better if I break down my enums into different modules or should I just put them all in one big file called header ? (because either way I should include all headers first right ?)
As I said your modules should be in order and its your choice, your the coder and you choose the layout that's more comfortable to you.

For example

pawn Code:
- declarations
- macros
- functions

- server

- player
PatrickGTR is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Ajuda] Programação modular SepZ Português/Portuguese 3 31/08/2015 03:14 AM
[Ajuda] Script Modular Holl Português/Portuguese 9 11/08/2015 07:06 PM
Modular scripting Marricio Scripting Help 0 03/08/2013 09:28 PM
[Help] Pawn IDE for modular programming Yuripe Scripting Help 4 12/09/2012 03:18 PM


All times are GMT. The time now is 09:11 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.