SA-MP Forums

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

Thread Tools Display Modes
Old 17/01/2017, 09:16 AM   #1
Spam Machine
Vince's Avatar
Join Date: Sep 2007
Location: Belgium
Posts: 10,977
Reputation: 2649
Default Maintenance best practices: coordinates

Maintenance best practices

Perhaps I will continue with a series on best practices, perhaps I will not. Time will tell.

What and why

This isn't a tutorial about how to get coordinates. It is merely a set of what I think are "best practices" in regards to coordinates. I set myself one rule: I only write a set of coordinates ONCE and I don't copy it anywhere else. Why? Maintenance! Consider this example:

PHP Code:
/* cases */

case 7SetPlayerCheckpoint(playerid1013.452542.5413.433.0);

/* cases */


    1000 more lines of code 


// stuff

I see this very often. Now, should I want to move the checkpoint to another location I would have to edit those coordinates in two, or possibly even more places. This is cumbersome and - if I don't meticulously replace every single instance - prone to logic errors. The gamemode will still compile and all, but it won't work in-game. This is frustrating and hard to debug.


There are multiple viable solutions and there isn't really "one solution to rule them all" (in a LOTR reference) because it all depends on what you're doing. If you're creating a race you would go differently about it than if you were creating some checkpoints in stores. What is important is that you stick to the rule: only write a set of coordinates ONCE.

Single set

If you only have one or a few sets of coordinates a possible solution might be to simply put a definition at the top of your script:

PHP Code:
#define MY_LOCATION 1013.45, 2542.54, 13.43
// OR
#define MY_LOCATION_X (1013.45)
#define MY_LOCATION_Y (2542.54)
#define MY_LOCATION_Z (13.43) 
Multiple sets

Frequently you will be using an array.

PHP Code:
new const Float:gRaceCheckpoints[][3] = {
2063.741644.50,  8.68},
2603.641581.00,  3.50}

gCurrentVisibleCheckpoint[MAX_PLAYERS] = {-1, ...}; 
If a checkpoint is enabled for a player, change the variable accordingly. For example, if I set the visible checkpoint to 2 then I know that the player's visible checkpoint is at 1013.45, 2542.54, 13.43 (array indices start at 0). This is incredibly useful when creating a checkpoint sequence, like a race. Or a sweeper job, or what have you. If using race checkpoints (as opposed to normal checkpoints) you can even point the arrow in the right direction by pulling the coordinates from the next checkpoint from the array (careful: exclude the last checkpoint).

Streamer plugin

If you are using the streamer plugin (many people are) then use it to its fullest. Locations (and more) for all entities are stored in memory and can be retrieved with creative use of the data manipulation natives. Also liberally use the STREAMER_EXTRA_ID attribute to store extra vital information. In some cases you might be able to create entities (checkpoint, pickup, object, ...) straight from dynamic storage without bothering to assign the entity id to a variable. If the entity belongs to, say, a faction then I can store the (SQL) factionID in the STREAMER_EXTRA_ID attribute as it is created. As the player interacts with the entity (enters the checkpoint, picks up a pickup, shoots an object, ...) I can get the factionID for that entity from memory and use it to query the database directly.
Vince is offline   Reply With Quote
Old 17/01/2017, 11:09 AM   #2
Huge Clucker
Killa[DGZ]'s Avatar
Join Date: Feb 2010
Location: Australia
Posts: 387
Reputation: 85
Default Re: Maintenance best practices: coordinates

This is really good vince and I agree with you as I've always done this myself, I've also always used defines for everything I possibly can at the top of every script I make as it really does make it easily maintainable later on without the need to search through hundreds of thousands of lines of code and edit each case specifically throughout the script, plus they don't use memory like const defines do.
Killa[DGZ] is offline   Reply With Quote
Old 17/01/2017, 02:40 PM   #3
Big Clucker
PeanutButter's Avatar
Join Date: May 2015
Location: Belgium
Posts: 97
Reputation: 32
Default Re: Maintenance best practices: coordinates

Perfect timing, just when I needed it!
PeanutButter is offline   Reply With Quote
Old 18/01/2017, 04:32 AM   #4
vassilis's Avatar
Join Date: Nov 2009
Location: Greece,Athens
Posts: 1,943
Reputation: 360
Default Re: Maintenance best practices: coordinates

I think one more chapter explaining even deeper how could we use streamer plugin for data manipulation would make tutorial perfect. Generally, it's a proper tutorial as any other you had made. Cheers.
vassilis is offline   Reply With Quote
Old 18/01/2017, 10:43 AM   #5
Logic_'s Avatar
Join Date: Jun 2015
Location: 666
Posts: 1,490
Reputation: 268
Default Re: Maintenance best practices: coordinates

Good tutorial Vince, this is very good +Rep.
Premium quality and reasonable pricing game Server hosting
Logic_ is offline   Reply With Quote
Old 21/01/2017, 10:43 PM   #6
Luis-'s Avatar
Join Date: Jan 2010
Location: England
Posts: 4,074
Reputation: 378
Default Re: Maintenance best practices: coordinates

Awesome. Always love reading your tutorials.
Luis- is online now   Reply With Quote
Old 21/01/2017, 11:19 PM   #7
Misiur's Avatar
Join Date: Jul 2009
Location: Poland
Posts: 2,535
Reputation: 552
Default Re: Maintenance best practices: coordinates

Short and concise. Maybe at least this will keep newbies and noobs alike attention spans to read this
Misiur is offline   Reply With Quote

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
Checking if object coordinates equal to certain coordinates Jonesy96 Scripting Help 7 05/12/2013 06:00 PM
[Help] maintenance rate _NosiK_ Help Archive 2 14/09/2010 10:59 AM

All times are GMT. The time now is 10:08 PM.

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