Return problem ? I think
#1

I start edit a rp gamemode and i make another cars for police and another factions, but when a player enter a vehicle it's check if the car is a CopCar or another faction....The problem is the function that was in gm(i edit because i made alot of cars and was a waste of time to put all in the row) it's not returning what suppose to

OnPlayerStateChange:
PHP код:
if(IsACopCar(newcar))
        {
            if(
IsACop(playerid)) { }
            else { 
RemovePlayerFromVehicleEx(playerid); SendClientMessage(playerid,COLOR_WHITE"{999999}You are not a Cop."); }
        } 
IsACopCar function:
PHP код:
public IsACopCar(carid)
{
    for(new 
1,j=81ji++)
    {
        if(
carid == Copcar[i])
        {
            return 
1;
        }
        else
            return 
0;
    }
    return 
1;

The cause may be the last return or? Because i don't get it.....If i delete him give the warning of function need to have return value....
Reply
#2

Allow me to de-structure your function

PHP код:
function
{
    
loop start
    
{
        
check if car id is a cop car (loop number 1)
        {
            if 
so stop the loop and the function itself and return 1;
        }
        if 
not
            stop the loop 
and the function itself and return 0;
    }
    return 
1although you wont reach here anyway

And that's what would happen in run-time, you have a loop although it will run only once, completely killing the purpose of the loop.

You can solve this by changing the value of the last return to 0, and removing the else statement and its contents. It'd look like this
PHP код:
function
{
    
loop
    
{
        
check if the car is a cop car
        
{
            if 
so, return 1;
        }
    }
    if 
the loop above did not find any carit did not stop the function, hence this return 0will pass

Reply
#3

Basically, never return inside a loop body unless you want to explicitly exit the loop.

Also, the variable "j" is often used when the number of iterations (the number of times the loop is supposed to run) is determined by a function, so as to not invoke the function again with each iteration. You have a constant here so you have no need for the variable "j". You should be using sizeof, though. This is an operator, not a function, and its value is determined at compile time.
Reply
#4

Why is still everyone talking about a loop? Thats bullshit.

PHP код:
public IsACopCar(carid)
{
    if(
carid 82){return 1;}//If the carid is between 0 and 81, its a cop car.
    
else return 0;

Reply
#5

Quote:
Originally Posted by Troydere
Посмотреть сообщение
Allow me to de-structure your function

PHP код:
function
{
    
loop start
    
{
        
check if car id is a cop car (loop number 1)
        {
            if 
so stop the loop and the function itself and return 1;
        }
        if 
not
            stop the loop 
and the function itself and return 0;
    }
    return 
1although you wont reach here anyway

And that's what would happen in run-time, you have a loop although it will run only once, completely killing the purpose of the loop.

You can solve this by changing the value of the last return to 0, and removing the else statement and its contents. It'd look like this
PHP код:
function
{
    
loop
    
{
        
check if the car is a cop car
        
{
            if 
so, return 1;
        }
    }
    if 
the loop above did not find any carit did not stop the function, hence this return 0will pass

Ohhhh, now i get it , i was thiking i need that else there so it will return 0 but i was wrong , i don't know if you return 1 there will finish the loop and return 1 ..... Now it's clear why it was not worked
Reply
#6

Quote:
Originally Posted by GangstaSunny.
Посмотреть сообщение
Why is still everyone talking about a loop? Thats bullshit.

PHP код:
public IsACopCar(carid)
{
    if(
carid 82){return 1;}//If the carid is between 0 and 81, its a cop car.
    
else return 0;

Because it's looping over array indices and comparing them. It's not comparing vehicleid's directly. Look closer. The above is also a horrible way to do it because it relies on assumptions. Should the list change then the code will break and the compiler won't tell you.
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
Because it's looping over array indices and comparing them. It's not comparing vehicleid's directly. Look closer. The above is also a horrible way to do it because it relies on assumptions. Should the list change then the code will break and the compiler won't tell you.
That's bullshit
This guy can't use sizeof because there is no system for it (or at least he don't tell us) so we have to work with the stuff we got and for that my solution is the best.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)