SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 22/06/2015, 03:21 PM   #1
JoshNudock
Huge Clucker
 
JoshNudock's Avatar
 
Join Date: Aug 2014
Posts: 491
Reputation: 14
Question optimize code

I made a code, but want to let him 100 percent functional, the problem is that when I approach a vehicle it checks another vehicle farther than what I desire, sometimes function checks as far even though vehicle one in front of you .. how can I fix it?

PHP Code:
public PlacaVeiculoPerto(playerid)
{
    for( new 
iMAX_VEHICLESi++) {
        new 
Float:Pos[3];
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        if(
IsPlayerInRangeOfPoint(playerid7.0Pos[0], Pos[1], Pos[2])) {
            return 
i;
        }
    }
    return 
0;

__________________
Quote:
[IMG]https://lh3.******usercontent.com/-yRs0SvXCSg8/UfDOh9ZUBhI/AAAAAAAAAAw/14h233Cj-zM/w400-h200-no/bloggif_51f0ce6704cb0.gif[/IMG]
JoshNudock is offline   Reply With Quote
Old 22/06/2015, 03:32 PM   #2
CoaPsyFactor
Gangsta
 
Join Date: Jul 2010
Location: Belgrade, Serbia
Posts: 867
Reputation: 52
Default Re: optimize code

well, you should not return on IsPlayerInRange of point, but instead save in variable distance and vehicle id and continue with loop, when you get to another vehicle that is in radius check distance again and compare it with last one, if it is smaller than update variable with vehicle id and variable with distance to new values.

And after that return vehicle id out of loop.

something like this

PHP Code:
public PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Pos[3];

    for( new 
iMAX_VEHICLESi++) {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);

        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
   
    return 
vehicleid;

__________________
Quote:
Originally Posted by ThaiLuan View Post
Code:
C:\Users\Administrator\Downloads\Viet-RP\gamemodes\3mien.pwn(112016) : fatal error 101: cannot write to file: "disk full"

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


1 Error.
Help me! How to fix error
CoaPsyFactor is offline   Reply With Quote
Old 22/06/2015, 08:33 PM   #3
JoshNudock
Huge Clucker
 
JoshNudock's Avatar
 
Join Date: Aug 2014
Posts: 491
Reputation: 14
Question Re: optimize code

error


Code:
C:\Users\2\Desktop\gamemode\gamemodes\2.pwn(3075) : warning 213: tag mismatch
C:\Users\2\Desktop\gamemode\gamemodes\2.pwn(3075) : warning 213: tag mismatch
C:\Users\2\Desktop\gamemode\gamemodes\2.pwn(3075) : warning 213: tag mismatch
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


3 Warnings.
PHP Code:
stock PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Pos[3];

    for(new 
iMAX_VEHICLESi++)
    {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]); //error <<<<<<<<<<<
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);

        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
    return 
vehicleid;

Please
__________________
Quote:
[IMG]https://lh3.******usercontent.com/-yRs0SvXCSg8/UfDOh9ZUBhI/AAAAAAAAAAw/14h233Cj-zM/w400-h200-no/bloggif_51f0ce6704cb0.gif[/IMG]
JoshNudock is offline   Reply With Quote
Old 22/06/2015, 08:34 PM   #4
Mencent
High-roller
 
Mencent's Avatar
 
Join Date: Mar 2015
Location: Lower Saxony (Niedersachsen)
Posts: 1,313
Reputation: 92
Default AW: optimize code

Hello!

You forgot the Float:-Tag.
PHP Code:
stock PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Float:Pos[3];
    for(new 
iMAX_VEHICLESi++)
    {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);
        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
    return 
vehicleid;



- Mencent
__________________
Best wishes,
Mencent

(Sorry for my bad english, I hope you can understand this!)
Mencent is offline   Reply With Quote
Old 22/06/2015, 08:38 PM   #5
JoshNudock
Huge Clucker
 
JoshNudock's Avatar
 
Join Date: Aug 2014
Posts: 491
Reputation: 14
Thumbs down Re: AW: optimize code

Quote:
Originally Posted by Mencent View Post
Hello!

You forgot the Float:-Tag.
PHP Code:
stock PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Float:Pos[3];
    for(new 
iMAX_VEHICLESi++)
    {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);
        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
    return 
vehicleid;



- Mencent
Tranks!

@topic
Sorry, but it was not that above code that was in error, is the code below that checks the id of the vehicle, and it checks where this really the vehicle, this is the code with the error:

PHP Code:

public PlayerPertoVeiculo(playerid)
{
    static 
Float:Pos[3];
    for( new 
iMAX_VEHICLESi++) {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        if(
IsPlayerInRangeOfPoint(playerid7.0Pos[0], Pos[1], Pos[2])) {
            return 
1;
        }
    }
    return 
0;

__________________
Quote:
[IMG]https://lh3.******usercontent.com/-yRs0SvXCSg8/UfDOh9ZUBhI/AAAAAAAAAAw/14h233Cj-zM/w400-h200-no/bloggif_51f0ce6704cb0.gif[/IMG]
JoshNudock 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
Optimize this code... Baltimore Scripting Help 3 15/11/2014 08:45 PM
Help optimize this code Raefal Scripting Help 8 06/09/2014 09:09 AM
Optimize code Face9000 Scripting Help 4 05/01/2014 11:43 PM
OPTIMIZE CODE!!!!!!!! Nadim Scripting Help 2 01/11/2013 04:43 PM
help me to optimize my code armyoftwo Help Archive 3 14/11/2010 11:19 AM


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


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