AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved About ThrowNativeError (https://forums.alliedmods.net/showthread.php?t=336352)

MAGNAT2645 02-13-2022 11:55

About ThrowNativeError
 
Is it fine to ThrowNativeError in a function which is called by a native?
Like here

I want to create a client validation stock which will throw native errors so i can get rid of such lines in all of my natives
Code:

    if ( !IsClientIndex( iClient ) )
        return ThrowNativeError( SP_ERROR_NATIVE, "Invalid client index %i", iClient );

    if ( !IsClientInGame( iClient ) )
        return ThrowNativeError( SP_ERROR_NATIVE, "Client %i is not in game", iClient );

Also, is it necessary to return after throwing an error?
Code:

return ThrowNativeError();
(I assume that ThrowNativeError just returns 0)
I saw some natives do it, some not.
Just a little confused

Benoist3012 02-16-2022 10:46

Re: About ThrowNativeError
 
Although not marked as such, ThrowNativeError doesn't return, so sending back its returned value (which it will never return) is unnecessary.

I'm not exactly sure why it was given a return type and not void, but my best guess is simply that it was made to mimic its C++ equivalent, and also likely because native callbacks require you to return an int (and now any). Marking ThrowNativeError's return value as an int allows you to write
PHP Code:

return ThrowNativeError(...); 

instead of
PHP Code:

ThrowNativeError(...)
return 
0/-1/1

Which, i guess could look weird for anyone trying to understand what's going on inside a plugin.

Either way, just remember that this function doesn't "return" since it always throws an error.

Bacardi 02-16-2022 12:02

Re: About ThrowNativeError
 
C++ from SourceMod
https://github.com/alliedmodders/sou....cpp#L129-L149

*edit
ThrowNativeError (and ThrowError) will throw error and abort code, you won't get return value. My guess.

MAGNAT2645 02-17-2022 08:03

Re: About ThrowNativeError
 
I thought so. But anyway thanks for the answers.
But i'm still not sure about using ThrowNativeError inside non-native functions which are called by natives.
Like this:
Code:

stock void ThrowNativeErrorIfBadClient(int client, bool inGame = true) {
    if ( client < 1 || client > MaxClients )
        ThrowNativeError( SP_ERROR_NATIVE, "Invalid client index %i", client );

    if ( inGame && !IsClientInGame( client ) )
        ThrowNativeError( SP_ERROR_NATIVE, "Client %i is not in game", client );
}

int NTV_DoSomething(Handle plugin, int numParams) {
    int iClient = GetNativeCell( 1 );
    ThrowNativeErrorIfBadClient( iClient );

    // ...
}


Silvers 02-17-2022 10:53

Re: About ThrowNativeError
 
That works fine: https://github.com/SilvDev/Left4DHoo...hooks.sp#L5106

MAGNAT2645 02-17-2022 11:58

Re: About ThrowNativeError
 
Okay then, thanks.


All times are GMT -4. The time now is 22:49.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.