Raised This Month: $51 Target: $400
 12% 

Passing DataPack to a native?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CliptonHeist
Senior Member
Join Date: Feb 2016
Old 02-06-2019 , 02:31   Passing DataPack to a native?
Reply With Quote #1

Is it possible to pass a DataPack to a native and use it with something like DataPack pack = view_as<DataPack>(GetNativeCell(1));
CliptonHeist is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 02-06-2019 , 04:14   Re: Passing DataPack to a native?
Reply With Quote #2

Yes, It is. All as you described. If you want to know why it works so, read more about Handles concept.
Kailo is offline
CliptonHeist
Senior Member
Join Date: Feb 2016
Old 02-06-2019 , 05:58   Re: Passing DataPack to a native?
Reply With Quote #3

Hmm I'm not sure that I'm doing it right then.

Here's the native declaration:
PHP Code:
native void SBPP_BanPlayer(int iAdminint iTargetint iTime, const char[] sReasonHandle pack null); 
Implementation:
PHP Code:
public Native_SBBanPlayer(Handle pluginint numParams)
{
    
int client GetNativeCell(1);
    
int target GetNativeCell(2);
    
int time GetNativeCell(3);
    
DataPack pack view_as<DataPack>(GetNativeCell(5));
    
pack.Reset();
    
char reason[128];
    
GetNativeString(4reason128);

    if (
reason[0] == '\0')
        
strcopy(reasonsizeof(reason), "Banned by SourceBans");

    if (
client && IsClientInGame(client))
    {
        
AdminId aid GetUserAdmin(client);
        if (
aid == INVALID_ADMIN_ID)
        {
            
ThrowNativeError(SP_ERROR_NATIVE"Ban Error: Player is not an admin.");
            return 
0;
        }

        if (!
GetAdminFlag(aidAdmin_Ban))
        {
            
ThrowNativeError(SP_ERROR_NATIVE"Ban Error: Player does not have BAN flag.");
            return 
0;
        }
    }

    
PrepareBan(clienttargettimereasonsizeof(reason), pack);
    return 
true;

When pack.Reset() is called I get this error:
Quote:
L 02/06/2019 - 10:52:35: [SM] Exception reported: Invalid data pack handle 12f00f4 (error 5)
L 02/06/2019 - 10:52:35: [SM] Blaming: sbpp_main.smx
L 02/06/2019 - 10:52:35: [SM] Call stack trace:
L 02/06/2019 - 10:52:35: [SM] [0] DataPack.Reset
L 02/06/2019 - 10:52:35: [SM] [1] Line 2291, sbpp_main.sp::Native_SBBanPlayer
L 02/06/2019 - 10:52:35: [SM] [3] SBPP_BanPlayer
L 02/06/2019 - 10:52:35: [SM] [4] Line 168, disconnect_ban.sp::SQL_SelectSeconds
And this is the function that calls the native:
Spoiler


Is there anything wrong with what I'm doing here?
CliptonHeist is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-06-2019 , 12:30   Re: Passing DataPack to a native?
Reply With Quote #4

Not sure if this is causing the issue, but I would change this:

PHP Code:
DataPack pack view_as<DataPack>(GetNativeCell(5)); 
to this
PHP Code:
DataPack pack null;
if (
numParams >= 5) {
    
pack view_as<DataPack>(GetNativeCell(5));
    
pack.Reset();

__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 02-06-2019 , 13:42   Re: Passing DataPack to a native?
Reply With Quote #5

Powerlord, optional argument not assume change of params count. I think these changes will be overkill.

CliptonHeist, Here is only one thing that i see for now, you don't have check for null handle.
And in this case you definitely pass null. You don't set param -> you default param value -> pass null.
Kailo is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 02-06-2019 , 13:52   Re: Passing DataPack to a native?
Reply With Quote #6

https://sm.alliedmods.net/new-api/handles/CloneHandle
Mitchell is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 20:00.


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