SA-MP Forums

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

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

Maintenance best practices
Coordinates

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:
switch(random(10))
{
    
/* cases */

    
case 7SetPlayerCheckpoint(playerid1013.452542.5413.433.0);

    
/* cases */
}

/* 

    1000 more lines of code 

*/

if(IsPlayerInRangeOfPoint(playerid3.01013.452542.5413.43))
{
    
// 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.

Solutions

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] = {
    {
1580.721580.6611.45},
    {
2063.741644.50,  8.68},
    {
1013.452542.5413.43},
    {
1761.642590.4416.04},
    {
2603.641581.00,  3.50}
}

new 
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
Killa[DGZ]
Huge Clucker
 
Killa[DGZ]'s Avatar
 
Join Date: Feb 2010
Location: Australia
Posts: 393
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
PeanutButter
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!
__________________
MY TUTORIALS
MY INCLUDES
PeanutButter is offline   Reply With Quote
Old 18/01/2017, 04:32 AM   #4
vassilis
High-roller
 
vassilis's Avatar
 
Join Date: Nov 2009
Location: Greece,Athens
Posts: 1,942
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_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Location: Oblivion (#RIPX)
Posts: 1,474
Reputation: 263
Default Re: Maintenance best practices: coordinates

Good tutorial Vince, this is very good +Rep.
__________________
RIP XXXTentacion [1998-2018], Fredo [1990-2018]

My Releases: Click me!

Past experiences:
Ex- Developer of COD: Black Ops 3 [2017 - 2018], Palomino Creek RP [2018], NEG [2017], Zombieland [2015] and Evolution Prison RP [2017]

Working on: Catalyst TDM (Redefining TDM)
Logic_ is offline   Reply With Quote
Old 21/01/2017, 10:43 PM   #6
Luis-
High-roller
 
Luis-'s Avatar
 
Join Date: Jan 2010
Location: England
Posts: 4,078
Reputation: 378
Default Re: Maintenance best practices: coordinates

Awesome. Always love reading your tutorials.
Luis- is offline   Reply With Quote
Old 21/01/2017, 11:19 PM   #7
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,539
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
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
Checking if object coordinates equal to certain coordinates Jonesy96 Scripting Help 7 05/12/2013 06:00 PM
LS Road Maintenance leosmile Screenshots and Videos 2 23/02/2013 09:02 AM
[Help] maintenance rate _NosiK_ Help Archive 2 14/09/2010 10:59 AM
Maintenance Service vytas_lt Screenshots and Videos 0 26/09/2009 08:19 PM


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


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