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

Methodmaps on global variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 03-31-2016 , 23:39   Methodmaps on global variables
Reply With Quote #1

I'm probably missing something pretty simple. I'm getting used to the new syntax and have been running into an issue with global variables, specifically with datapacks. I'm getting some errors.

PHP Code:
DataPack g_hMenuDataPack[MAXPLAYERS 1];
func1(int iClient)
{
    if(
g_hMenuDataPack[iClient] != null)
    {
        
delete g_hMenuDataPack[iClient];
        
g_hMenuDataPack[iClient] = null;
    }

    
g_hMenuDataPack[iClient] = new DataPack();
    
g_hMenuDataPack[iClient].WriteString(strPerk);
    
g_hMenuDataPack[iClient].WriteCell(iPrice);
    
g_hMenuDataPack[iClient].WriteCell(iDuration);
}

func2(int iClient)
{
    
g_hMenuDataPack[iClient].Reset(); // This is where I get the "invalid datapack handle" error.


Last edited by ReFlexPoison; 03-31-2016 at 23:55.
ReFlexPoison is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 04-01-2016 , 00:07   Re: Methodmaps on global variables
Reply With Quote #2

Have a look at this....

https://forums.alliedmods.net/showthread.php?t=270519

It doesn't really relate to your question
__________________

Last edited by Neuro Toxin; 04-01-2016 at 00:12.
Neuro Toxin is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 04-01-2016 , 00:45   Re: Methodmaps on global variables
Reply With Quote #3

This
Code:
if(g_hMenuDataPack[iClient] != null)
    {
        delete g_hMenuDataPack[iClient];
        g_hMenuDataPack[iClient] = null;
    }
Is equivalent to simply writing delete g_hMenuDataPack[iClient];
__________________

Last edited by Merudo; 04-01-2016 at 00:46.
Merudo is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 04-01-2016 , 00:54   Re: Methodmaps on global variables
Reply With Quote #4

You need to use ResetPack(g_hMenuDataPack[iClient]) instead of .Reset()
__________________

Last edited by Merudo; 04-01-2016 at 01:11.
Merudo is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-01-2016 , 05:35   Re: Methodmaps on global variables
Reply With Quote #5

Code:
g_hMenuDataPack[iClient].Reset(false); // or true to clear data
Quote:
Originally Posted by ReFlexPoison View Post
I'm probably missing something pretty simple. I'm getting used to the new syntax and have been running into an issue with global variables, specifically with datapacks. I'm getting some errors.

PHP Code:
DataPack g_hMenuDataPack[MAXPLAYERS 1];
func1(int iClient)
{
    if(
g_hMenuDataPack[iClient] != null)
    {
        
delete g_hMenuDataPack[iClient];
        
g_hMenuDataPack[iClient] = null;
    }

    
g_hMenuDataPack[iClient] = new DataPack();
    
g_hMenuDataPack[iClient].WriteString(strPerk);
    
g_hMenuDataPack[iClient].WriteCell(iPrice);
    
g_hMenuDataPack[iClient].WriteCell(iDuration);
}

func2(int iClient)
{
    
g_hMenuDataPack[iClient].Reset(); // This is where I get the "invalid datapack handle" error.

__________________
Do not Private Message @me
Bacardi is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 04-01-2016 , 12:30   Re: Methodmaps on global variables
Reply With Quote #6

Looks like a compiler bug to me.

g_hMenuDataPack[iClient].Reset() should return "error 092: number of arguments does not match definition", or .Reset() should have "false" as default argument.
__________________
Merudo is offline
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 04-01-2016 , 15:23   Re: Methodmaps on global variables
Reply With Quote #7

Quote:
Originally Posted by Bacardi View Post
Code:
g_hMenuDataPack[iClient].Reset(false); // or true to clear data
Thanks for the quick response! Works well now.
ReFlexPoison is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-01-2016 , 15:28   Re: Methodmaps on global variables
Reply With Quote #8

Judging from the error you're seeing, g_hMenuDataPack[iClient] is null when you're trying to call Reset on it. Which would happen if you called func2 for a client before calling func1 for a client.

Quote:
Originally Posted by Merudo View Post
Looks like a compiler bug to me.

g_hMenuDataPack[iClient].Reset() should return "error 092: number of arguments does not match definition", or .Reset() should have "false" as default argument.
false is the default for Reset. In fact, the DataPack MethodMap tells Reset to use the ResetPack function rather than having its own implementation. Or at least that's what SourceMod 1.7 does, I haven't checked 1.8.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 04-01-2016 at 15:29.
Powerlord is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-01-2016 , 15:37   Re: Methodmaps on global variables
Reply With Quote #9

Quote:
Originally Posted by Powerlord View Post
Judging from the error you're seeing, g_hMenuDataPack[iClient] is null when you're trying to call Reset on it. Which would happen if you called func2 for a client before calling func1 for a client.



false is the default for Reset. In fact, the DataPack MethodMap tells Reset to use the ResetPack function rather than having its own implementation. Or at least that's what SourceMod 1.7 does, I haven't checked 1.8.
...I have just tested SourcePawn Compiler 1.7.3-dev+5302
But that error still appear even in same callback, when global variable is array. *Reset works when give parameter true or false
PHP Code:
DataPack g_hMenuDataPack[MAXPLAYERS 1];
func1(int iClient)
{
    if(
g_hMenuDataPack[iClient] != null)
    {
        
delete g_hMenuDataPack[iClient];
        
g_hMenuDataPack[iClient] = null;
    }

    
g_hMenuDataPack[iClient] = new DataPack();
    
g_hMenuDataPack[iClient].WriteString("asd");
    
g_hMenuDataPack[iClient].WriteCell(123);
    
g_hMenuDataPack[iClient].WriteCell(123);
    
g_hMenuDataPack[iClient].Reset(); // Error

But non-array works fine.
PHP Code:
DataPack g_hMenuDataPack;
func1(int iClient)
{
    if(
g_hMenuDataPack != null)
    {
        
delete g_hMenuDataPack;
        
g_hMenuDataPack null;
    }

    
g_hMenuDataPack = new DataPack();
    
g_hMenuDataPack.WriteString("asd");
    
g_hMenuDataPack.WriteCell(123);
    
g_hMenuDataPack.WriteCell(123);
    
g_hMenuDataPack.Reset(); //Work


*edit
maybe it not get default bool value from ResetPack function from methodmap or something
__________________
Do not Private Message @me

Last edited by Bacardi; 04-01-2016 at 15:39.
Bacardi 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 23:51.


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