Attemp to call string

Started by CopyPaste, Jul 17, 2015, 12:46 PM

Previous topic - Next topic

MatheuS

This was in Warchiefs simply replayed.
Quote from: Thijn on Jul 17, 2015, 07:26 PMPerma-Banned CopyPaste. He and Ksna are the same person. So for some reason he does come up with the right script.
If you can do it yourself, or copy it from somewhere else. Great! Just don't come here with another account and answer your own question like that.
S.L.C. gave you the reason why it wasn't working, and you did nothing with that answer.




Quote from: MatheuS on Jul 17, 2015, 06:54 PMGetPlayer Function:
..
That code makes no sense.

if ( plr ) { .. }
else { plr = FindPlayer( plr ); }

When is that ever going to work? Sure if you actually specify plr the function works, but that else makes no sense at all.
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Kratos_


Quote from: Thijn on Jul 17, 2015, 07:26 PMThat code makes no sense.

if ( plr ) { .. }
else { plr = FindPlayer( plr ); }

When is that ever going to work?

That function will work for sure . The parameter is defined as identifier "plr" which we generally refer to instance . Although being custom , "plr" is symbolized as the "instance holder" due to intensive use by most of the VC:MP scripters while specifying to "target instance" . This myth ends here .

Because , our purpose through "GetPlayer" function is to get the target "instance" . So , it is quite clear that passed argument will not be an instance & indeed would be either player's name or his id . Now , the check if( plr ) at the beginning of the function is useless since we're attempting to check whether an argument to "plr" is passed ? While the fact is that if no argument is passed then it will itself throw error "Wrong Number Of Parameters" & function execution will be seized .

Thereafter we're checking whether the passed argument is a "numeric string" . If then we're converting this string to integer & passing this as an argument to function FindPlayer which will attempts to check whether there is an valid player instance corresponding to this . This checking will be based on id . Thereafter we're assigning the result to "plr" which would now contain either an instance or null .
Actually this could be considered as identical to compound assignment ( aka short-hand ) :-

function IsEven( num )
{
   num %= 2;
   num == 0 ? print("Even") : print("Not even");
}

Thereafter if "plr" got an instance then we're attempting to return it otherwise returning false . The other check would be on the basis of name & not id .

function GetPlayer( plr )
{
    // checking if the argument to plr is passed [Useless]
    if ( plr )
    {
        // checking if the passed argument is a numeric string
        if ( IsNum( plr ) )
        {
            // 1. converting numeric string "plr" to actual integer
            // 2. passing this as an argument to FindPlayer
            // 3. "plr" now contain either an instance or null instead of id
           
            plr = FindPlayer( plr.tointeger() );
           
            // 1.checking whether an instance has been received ?
            // 2. returning the resultant instance
           
            if ( plr ) return plr;
           
            // otherwise returning false
            else return false;
        }
    // if the passed argument isn't numeric string
    else
        {     
            // 1. passing the string (player's name) to FindPlayer
            // 2. "plr" now contain either an instance or null instead of name
           
            plr = FindPlayer( plr );
           
            // 1.checking whether an instance has been received ?
            // 2. returning the resultant instance
           
            if ( plr ) return plr;
           
            // otherwise returning false
            else return false;
        }
    }
    else return false; // [Useless]
}

Failure case scenario : This function will work in most cases but still struggle at something . For example , if you're passing it a number then check will be made on the basis of id & not player's name . What if someone joined the server with nick 123456 . If we'll write /kick 1234 , then it will return invalid player . Because , as I said earlier , it will recognize 1234 as an id & then attempts to check it . Truth is that there is no such id actually exist . This could easily be fixed by making an additional check once checking through id has been failed or checking via name before id . However I consider it a bit overkill since numeric names look absurd . So , I prefer
using this :-

local plr = IsNum(text) ? FindPlayer(text.tointeger() ) : FindPlayer(text);
And for the number lovers this one :-

if( IsNum(player.Name) ) KickPlayer( player );
In the middle of chaos , lies opportunity.