Raised This Month: $32 Target: $400
 8% 

New API and Syntax


Post New Thread Reply   
 
Thread Tools Display Modes
Disowned
Member
Join Date: Oct 2015
Old 01-27-2016 , 01:41   Re: New API and Syntax
Reply With Quote #691

Quote:
Originally Posted by ddhoward View Post
float &value
Derp. That works, thank you.
Disowned is offline
Electr000999
Senior Member
Join Date: Aug 2011
Old 01-27-2016 , 10:24   Re: New API and Syntax
Reply With Quote #692

i have hm.. i dk why i have this error: Plugin encountered error 17: Stack memory leaked by native

for example: i have 2 functions that return char and have two modes for use this:

Code:
void TestExample(bool Mode)
{
    char sMyExampleString[127];
     
    // if i use this i have error: Plugin encountered error 17: Stack memory leaked by native.
    sMyExampleString =  Mode == true ? ReturnChar1() : ReturnChar2(); 
     
      
     // if this ALL FINE.
     if (Mode == true)
     {
           sMyExampleString = ReturnChar1(); 
     }
     else
     {
           sMyExampleString = ReturnChar2(); 
     }
}

char ReturnChar1()
{
       char buffer[127];

       buffer = "Mode 1";
   
       return buffer;
}

char ReturnChar2()
{
       char buffer[127];
   
       buffer = "Mode 0";

       return buffer;
}
i try use Format instead what used in example but no difference.

OR may be structure: "var == true ? true : false;" not work with chars correctly (only int, bool, float)?

Last edited by Electr000999; 01-27-2016 at 10:30.
Electr000999 is offline
Send a message via Skype™ to Electr000999
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-27-2016 , 11:07   Re: New API and Syntax
Reply With Quote #693

Quote:
Originally Posted by Electr000999 View Post
i have hm.. i dk why i have this error: Plugin encountered error 17: Stack memory leaked by native

for example: i have 2 functions that return char and have two modes for use this:

Code:
void TestExample(bool Mode)
{
    char sMyExampleString[127];
     
    // if i use this i have error: Plugin encountered error 17: Stack memory leaked by native.
    sMyExampleString =  Mode == true ? ReturnChar1() : ReturnChar2(); 
     
      
     // if this ALL FINE.
     if (Mode == true)
     {
           sMyExampleString = ReturnChar1(); 
     }
     else
     {
           sMyExampleString = ReturnChar2(); 
     }
}

char ReturnChar1()
{
       char buffer[127];

       buffer = "Mode 1";
   
       return buffer;
}

char ReturnChar2()
{
       char buffer[127];
   
       buffer = "Mode 0";

       return buffer;
}
i try use Format instead what used in example but no difference.

OR may be structure: "var == true ? true : false;" not work with chars correctly (only int, bool, float)?
That wont work, in order to copy strings (char) you need to use strcpy.

Try this:
PHP Code:
void TestExample(bool Mode)
{
    
char sMyExampleString[16];   // You dont need 127 to print "Mode x"
      
     // if this ALL FINE.
     
if (Mode == true)
     {
           
ReturnChar1(sMyExampleStringsizeof sMyExampleString
     }
     else
     {
           
ReturnChar2(sMyExampleStringsizeof sMyExampleString)
     }
}

int ReturnChar1(char strint len)
{
       static 
char buffer[] = "Mode 1";       
   
       return 
strcopy(strlenbuffer);
}

int ReturnChar2(char strint len)
{
       static 
char buffer[] = "Mode 0";       
   
       return 
strcopy(strlenbuffer);


Last edited by TheDS1337; 01-27-2016 at 11:09.
TheDS1337 is offline
Godis
Senior Member
Join Date: Jan 2014
Old 01-27-2016 , 12:15   Re: New API and Syntax
Reply With Quote #694

Spoiler


Actually, this is very much possible:

PHP Code:
void TestExample(bool mode)
{
    
char str[32];
    
    if(
mode)
    {
        
str GetString1();
    }
    else
    {
        
str GetString2();
    }
    
    
PrintToChatAll(str);
}

char[] GetString1() // Notice the return type, 'char[]' and not 'char'. This indicates that it should return an array of characters instead of just one.
{
    return 
"string1";
}

char[] GetString2()
{
    return 
"string2";

Natives cannot return arrays, but within a plugin it works just fine.

EDIT: If we would've had 'real' strings, we would see a lot more of this, since natives would be able to return them.

We would also be able to do this:

PHP Code:
string str "part1, " "part2, ";
PrintToChatAll(str "part3."); 

Last edited by Godis; 01-27-2016 at 12:31.
Godis is offline
Gormarim
Junior Member
Join Date: Jun 2015
Old 02-05-2016 , 06:41   Re: New API and Syntax
Reply With Quote #695

Sorry if it was answered before, but could somebody explain me why this code works:
PHP Code:
ArrayList array = new ArrayList(11);
array.
Set(0, new ArrayList(11));

PrintToServer("%d"GetArrayCell(view_as<ArrayList>(array.Get(0)), 0)); 
While this one doesn't:
PHP Code:
ArrayList array = new ArrayList(11);
array.
Set(0, new ArrayList(11));

PrintToServer("%d", (view_as<ArrayList>(array.Get(0))).Get(0)); 
[SM] Native "ArrayList.Get" reported: Invalid Handle 0 (error: 4)

EDIT:
PHP Code:
PrintToServer("%d", (view_as<ArrayList>(array.Get(0))).Length); 
Though this works perfectly fine

Last edited by Gormarim; 02-05-2016 at 06:49.
Gormarim is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 02-05-2016 , 06:59   Re: New API and Syntax
Reply With Quote #696

Make sure you're using the very latest 1.8 snapshot compiler.
__________________
asherkin is offline
Gormarim
Junior Member
Join Date: Jun 2015
Old 02-05-2016 , 07:28   Re: New API and Syntax
Reply With Quote #697

Asherkin, no errors with latest build, thanks.
Gormarim is offline
Electr000999
Senior Member
Join Date: Aug 2011
Old 02-09-2016 , 09:52   Re: New API and Syntax
Reply With Quote #698

in documentation error: https://sm.alliedmods.net/new-api/sd...nd/NormalSHook
this:
typedef NormalSHook = function Action(int clients[64], int &numClients, char sample[], int &entity, int &channel, float &volume, int &level, int &pitch, int &flags)
should be:
typedef NormalSHook = function Action(int clients[64], int &numClients, char[] sample, int &entity, int &channel, float &volume, int &level, int &pitch, int &flags)
Electr000999 is offline
Send a message via Skype™ to Electr000999
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 02-10-2016 , 09:43   Re: New API and Syntax
Reply With Quote #699

No, the sample is actually of type
Code:
char sample[PLATFORM_MAX_PATH]
It's missing the constant as well as its value in the display.
I'd guess we can do better, dvander?

Quote:
Originally Posted by Electr000999 View Post
in documentation error: https://sm.alliedmods.net/new-api/sd...nd/NormalSHook
this:
typedef NormalSHook = function Action(int clients[64], int &numClients, char sample[], int &entity, int &channel, float &volume, int &level, int &pitch, int &flags)
should be:
typedef NormalSHook = function Action(int clients[64], int &numClients, char[] sample, int &entity, int &channel, float &volume, int &level, int &pitch, int &flags)
__________________
Peace-Maker is offline
BAILOPAN
Join Date: Jan 2004
Old 02-10-2016 , 14:48   Re: New API and Syntax
Reply With Quote #700

It's more complicated than that unfortunately. You'd end up seeing something like "char blah[255]" which is not quite what you want to see. You want "blah[PLATFORM_MAX_PATH]". We have a similar problem with default arguments.

The docgen tool is three steps:
(1) Do a full parse/type check of the .inc.
(2) Export .json of where every comment begins and ends in the .inc.
(3) Use the .json and .inc file to populate a MySQL database.

So we could expand on step 2. For example, "char blah[PLATFORM_MAX_PATH]" could be recorded as having its type at indices (10, 26] and step 3 would need to just cut that string out of the file instead of pretty-printing.
__________________
egg
BAILOPAN is offline
Reply


Thread Tools
Display Modes

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 00:32.


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