AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Methodmaps on global variables (https://forums.alliedmods.net/showthread.php?t=281061)

ReFlexPoison 03-31-2016 23:39

Methodmaps on global variables
 
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.



Neuro Toxin 04-01-2016 00:07

Re: Methodmaps on global variables
 
Have a look at this....

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

It doesn't really relate to your question

Merudo 04-01-2016 00:45

Re: Methodmaps on global variables
 
This
Code:

if(g_hMenuDataPack[iClient] != null)
    {
        delete g_hMenuDataPack[iClient];
        g_hMenuDataPack[iClient] = null;
    }

Is equivalent to simply writing delete g_hMenuDataPack[iClient];

Merudo 04-01-2016 00:54

Re: Methodmaps on global variables
 
You need to use ResetPack(g_hMenuDataPack[iClient]) instead of .Reset()

Bacardi 04-01-2016 05:35

Re: Methodmaps on global variables
 
Code:

g_hMenuDataPack[iClient].Reset(false); // or true to clear data
Quote:

Originally Posted by ReFlexPoison (Post 2407212)
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.




Merudo 04-01-2016 12:30

Re: Methodmaps on global variables
 
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.

ReFlexPoison 04-01-2016 15:23

Re: Methodmaps on global variables
 
Quote:

Originally Posted by Bacardi (Post 2407266)
Code:

g_hMenuDataPack[iClient].Reset(false); // or true to clear data

Thanks for the quick response! Works well now.

Powerlord 04-01-2016 15:28

Re: Methodmaps on global variables
 
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 (Post 2407352)
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.

Bacardi 04-01-2016 15:37

Re: Methodmaps on global variables
 
Quote:

Originally Posted by Powerlord (Post 2407394)
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


All times are GMT -4. The time now is 06:47.

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