SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 04/07/2012, 08:36 PM   #1
MP2
Godfather
 
MP2's Avatar
 
Join Date: Jan 2011
Location: England
Posts: 6,422
Reputation: 973
Default Gamemode Script Layout

Hei.

I've recently started development of a new gamemode/server and while originally the layout was okay, as it's building up more and more code it seems to be working less.

I have this issue where when I want to script something new, for example let's say I want to script some random weapon pickups - I need three things. The array of data, the creation and the OnPlayerPickUpPickup. First thing I do is the array - but I start to go 'WHERE DO I PUT THE ARRAY DO I PUT IT ABOVE ALL THE OTHERS OR AT THE BOTTOM OR A NEW SECTION FOR WEAPON PICKUPS OR WHAT' and yeah.

This is the current roughly layout of my gamemode (FROM MEMORY):

Includes

Defines

Enums/Arrays

Variables

Callbacks

Custom Functions

mySQL query callbacks

Timers

and I guess that's okay, but I seem to be losing track of where everything is.

Also I split my callbacks up in to sections, so like in OnGameModeInit I would do something like

Initial setup (DisableEnterExits and that sort of thing)
mySQL connection
Objects
Pickups
etc.

So yeah, let's discuss layout options. What do you do? What could I do to improve?
I know some people put different systems in different files and #include them, but I think that'd be a bit of a ballache. Instead of saying 'oh I need to add a new weapon to my weapon pickups, I'll just Ctrl+F and find the array and add it in' you'd have to find out what file it's in, find that file in your /include folder then Ctrl+F through that.

I dunno, it would make it more organized I guess. I'm also developing this server with another person (blewert \o/) and we need a way to contribute to the script at the same time. I guess having systems in separate files then including them would make our lives easier.
MP2 is offline   Reply With Quote
Old 04/07/2012, 08:38 PM   #2
Kyle
High-roller
 
Kyle's Avatar
 
Join Date: Mar 2009
Location: United Kingdom
Posts: 2,390
Reputation: 314
Default Re: Gamemode Script Layout

For me, tis:

Includes
Defines
Arrays / Enums
Variables

Then after that anything really, if I had a group chat system I place the stocks near the commands where it's required etc.

Also pop on the IRC and query me please.
Kyle is offline   Reply With Quote
Old 04/07/2012, 09:05 PM   #3
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,996
Reputation: 2647
Default Re: Gamemode Script Layout

I have kind of the same problem where all variable declarations are scrambled. I try to use static locals(*) where possible, but that doesn't always cut it. I also do include lots of stuff, and in fact I created a whole folder structure for it. I also always seem to code with the thought in mind that I might actually release the script one day and that it thus must be somewhat noob friendly.

At the top of my GM I define general settings like #undef MAX_PLAYERS, #define LOCALHOST and stuff like that, followed by my list of files;

Code:
// =============================================================================
// Variable Declarations

#include "../src/def/colors.txt"                    // Color definitions
#include "../src/def/sound_otb.txt"                 // Various sound ids
#include "../src/def/forward.txt"                   // Forward declarations for public functions
#include "../src/def/trucking.txt"                  // Coordinates for trucking pickup and dropoff
#include "../src/def/bcases.txt"                    // Coordinates for briefcase locations
#include "../src/def/unsafepass.txt"                // List of unsafe passwords (used during registration)
#include "../src/def/var/fishlist.txt"              // List of fish and rarity

#include "../src/def/enum/player/flags.txt"         // Player boolean values and stock functions
#include "../src/def/enum/player/timers.txt"        // Variable to store player timers

#include "../src/def/enum/global/textdraw.txt"      // Various general textdraws
#include "../src/def/enum/global/checkpoints.txt"   // Checkpoint definitions
#include "../src/def/enum/global/dialogs.txt"       // Dialog definitions
#include "../src/def/enum/global/skills.txt"        // Skill definitions

#include "../src/func/pinfo.txt"                    // PlayerInfo enum
#include "../src/func/pstats.txt"                   // PlayerStats enum
#include "../src/func/hinfo.txt"                    // HouseInfo enum
#include "../src/func/insurances.txt"               // Insurances enum and stock functions

// =============================================================================

which in turn is followed by random variables.

I'll probably clean it up one day. Not now.


(*) Example of static locals:
Code:
public OnPlayerUpdate(playerid)
{
	if(IsPlayerNPC(playerid)) return 1;

	// ----- OnPlayerHealthChange -----
	static
		Float:oldHealth[MAX_PLAYERS];
		
    GetPlayerHealth(playerid, PlayerStats[playerid][pHealth]);

	if(PlayerStats[playerid][pHealth] != oldHealth[playerid])
	{
	    OnPlayerHealthChange(playerid, PlayerStats[playerid][pHealth], oldHealth[playerid]);
		oldHealth[playerid] = PlayerStats[playerid][pHealth];
		return 1;
	}
    // -----
    return 1;
}
Works absolutely fine.
__________________
Vince is offline   Reply With Quote
Old 04/07/2012, 10:30 PM   #4
ReneG
High-roller
 
Join Date: Oct 2011
Location: Sublime Text 2
Posts: 1,981
Reputation: 337
Default Re: Gamemode Script Layout

Call me an idiot, but I go for the same layout as the Godfather.
  1. Includes
  2. Defines
  3. Public function forwards
  4. Enums/Arrays
  5. Variables
  6. All default SA-MP callbacks
  7. All custom functions.
  8. Timers
  9. Commands
All SQL code goes into an include file. Weird, but I edit the SQL include with Notepad++, CTRL+S, and compile. Different than using Pawno, and having it all cluttered in your mode.
ReneG is offline   Reply With Quote
Old 05/07/2012, 02:23 AM   #5
TheChaoz
High-roller
 
TheChaoz's Avatar
 
Join Date: Dec 2009
Location: Argentina
Posts: 4,657
Reputation: 239
Default Re: Gamemode Script Layout

Normaly for small scritps I tend to do something like this:
Code:
Includes
Macros
Vars/Arrays
Callbacks
(Commands if using ZCMD)
Functions
But on my GM I use includes for each system, so I do it this way:
Code:
Includes (#Incldue ""../Data/Systems/System_Name/Header"
Callbacks

Each system include header looks like this:
Code:
#include "../Data/Systems/System_Name/Arrays"//includes macros, enums, arrays, variables
#include "../Data/Systems/System_Name/Functions"
#include "../Data/Systems/System_Name/Callbacks"
#include "../Data/Systems/System_Name/Commands"
#include "../Data/Systems/System_Name/Dialogs"

It's quite simple to find bugs or change things from a system.
__________________
TheChaoz is offline   Reply With Quote
Old 05/07/2012, 02:41 AM   #6
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,723
Reputation: 3073
Default Re: Gamemode Script Layout

OK, I'm intrigued, those of you who arrange your code by declaration type instead of function, why do you think this is a good way of doing it? And this is an honest question not sarcasm! The fact that a vehicle enum looks a little bit like a player enum doesn't mean that they're related in any way, so why put them together? That seems to me like organising a library by the colour of the book covers instead of the book topics!

As for having separate directories for systems and separate files for the different declarations in those systems - that's getting closer to a good layout but still means you can't use "static" to restrict access to data. Think of "static" as data private to a system - you want all of the system to be able to see that data, but you don't want any other system to see that data because they could start adjusting it in bad way. Instead you just provide a clean API to interact with that data any everyone is happy.

I'm not sure how this is even someone any programmer should wonder:

Number of major open source projects who "organise" their code in one massive file sorted by keyword: 0
Number of major open source projects who organise their code by splitting code up in to separate chunks of related functionality: All of them!

That alone should tell you something. I really suggest you start reading outside of SA:MP - you might learn something! There is a free book out called "The architecture of open source projects volume 2" - look it up. Also, just any generic reading on any generic programming will VERY quickly get on to how to split up code by function.

Finally, why do you think there are separate includes for player functions and object functions? Do you think that maybe says anything about the internal structure of the server's code? And I'm not giving ANYTHING away by saying that because any programmer with more than 10 minutes experience would realise that a large program like the server would split those functions up in that way.
Y_Less is offline   Reply With Quote
Old 05/07/2012, 03:13 AM   #7
MP2
Godfather
 
MP2's Avatar
 
Join Date: Jan 2011
Location: England
Posts: 6,422
Reputation: 973
Default Re: Gamemode Script Layout

The general reason I and others put arrays and enums etc. above callbacks is because (obviously) the callbacks can't use them if they're declared after them. Most of them get used in OnGameModeInit so it wouldn't make sense to have them at the bottom of the script.

For example my weapon pickup array - it needs to be above OnGameModeInit because they get created there.

I do agree splitting up systems is important - I'm just not totally sure how to do it. I'm sort of put off by the fact of having a million files when I could just have one.

Also, say you scripted something 2 years ago and it's in some file in some directory in some other directory - if the compiler gives an error it's going to say the error is in your GM, not which file. How are you meant to remember what file the script is in?

I should also point out I'm co-developing this with another person - which makes it even harder to organize stuff.

I am definately going to split many systems and include them, for example the housing system, ammunation, bank robberies and stuff like that.
MP2 is offline   Reply With Quote
Old 05/07/2012, 03:51 AM   #8
[ABK]Antonio
High-roller
 
[ABK]Antonio's Avatar
 
Join Date: Sep 2011
Location: ID, USA
Posts: 1,239
Reputation: 185
Default Re: Gamemode Script Layout

Meh, it doesn't really matter what anyone's opinion on this is aside from your own. Though I'm still going to throw mine out there :P

I organize like Y_Less said no one does. The reason for this...I find it much easier. If I forget the name of the function I just made...variable or anything else - I won't have to search everywhere for it (with "splitting code up in to separate chunks of related functionality" you can lose things very easily. even if they're related).

I do go like most of you...sort of


Includes
Macros
Enums
Arrays
Vars
Main
ongamemodeinit
onplayerconnect
..
onplayerdisconnect
ongamemodeexit
Commands
Custom functions (not publics (those are in a separate place))
Custom functions (publics (not just timers, things for cross server stuff go here too))
Save File stuff (I always put it at the bottom (aside from forwards), just for easier access)
Forwards

I also comment above the beginning of them - keywords. Though I'm not an open source kind of guy.
__________________

ABKClan.com
[ABK]Antonio is offline   Reply With Quote
Old 05/07/2012, 04:36 AM   #9
TheChaoz
High-roller
 
TheChaoz's Avatar
 
Join Date: Dec 2009
Location: Argentina
Posts: 4,657
Reputation: 239
Default Respuesta: Gamemode Script Layout

Quote:
Originally Posted by Y_Less View Post
OK, I'm intrigued, those of you who arrange your code by declaration type instead of function, why do you think this is a good way of doing it? And this is an honest question not sarcasm! The fact that a vehicle enum looks a little bit like a player enum doesn't mean that they're related in any way, so why put them together? That seems to me like organising a library by the colour of the book covers instead of the book topics!

As for having separate directories for systems and separate files for the different declarations in those systems - that's getting closer to a good layout but still means you can't use "static" to restrict access to data. Think of "static" as data private to a system - you want all of the system to be able to see that data, but you don't want any other system to see that data because they could start adjusting it in bad way. Instead you just provide a clean API to interact with that data any everyone is happy.

I'm not sure how this is even someone any programmer should wonder:

Number of major open source projects who "organise" their code in one massive file sorted by keyword: 0
Number of major open source projects who organise their code by splitting code up in to separate chunks of related functionality: All of them!

That alone should tell you something. I really suggest you start reading outside of SA:MP - you might learn something! There is a free book out called "The architecture of open source projects volume 2" - look it up. Also, just any generic reading on any generic programming will VERY quickly get on to how to split up code by function.

Finally, why do you think there are separate includes for player functions and object functions? Do you think that maybe says anything about the internal structure of the server's code? And I'm not giving ANYTHING away by saying that because any programmer with more than 10 minutes experience would realise that a large program like the server would split those functions up in that way.
Well that's a nice question you got there, but I can only answer for myself and not for others, so here I go.

In small scripts I tend to organize by "types" and not by "topic" as it's much easyer for me to find things, Ex: I got a 2000k lines file, and I need to remember how I did name an enum object, as I have all of them togeter it's a way faster. May you're right, enums may be for players/vehicles/special types but it's like when you (or at least me) organize your books at university/school, I organize them for years, not by each subject (may when I finish it i'll organize them by subject, but not for now).

And about the systems, I do need them go be conected with each other, not all of them but most of them, that's why I do it this way.
__________________
TheChaoz is offline   Reply With Quote
Old 05/07/2012, 07:53 AM   #10
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,996
Reputation: 2647
Default Re: Gamemode Script Layout

Quote:
Originally Posted by MP2 View Post
Also, say you scripted something 2 years ago and it's in some file in some directory in some other directory - if the compiler gives an error it's going to say the error is in your GM, not which file. How are you meant to remember what file the script is in?
The compiler actually does tell you what file the error is in.
__________________
Vince 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
[Tool/Web/Other] SA:MP Web Layout DJGama101 Tools and Files 2 17/12/2011 12:47 PM
[Tool/Web/Other] My SA-MP Web layout gamer931215 Tools and Files 43 24/11/2011 09:14 AM
Website Layout Norn Everything and Nothing 17 08/03/2010 12:27 PM
[Question]How do i convert my 0.2X Gamemode Script into a 0.3 script? OGganza Help Archive 11 22/10/2009 05:55 PM


All times are GMT. The time now is 02:38 AM.


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