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

Return string through native


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bibu
Veteran Member
Join Date: Sep 2010
Old 06-12-2014 , 14:19   Return string through native
Reply With Quote #1

I found two topics about that and just couldn't get it to work. The string is always empty for me somehow...

It is not a cvar to retrieve. It's individual for each player:


PHP Code:
new szPMsg[32][33]

public 
native_get_user_pmessage(idszBuffer[], iLen)
{
    new 
szBuffer[32]
    
//for testing purpose
    
get_user_name(idszBuffer31)
     new 
len
    len 
format(szBuffer,31,szBuffer)
    return 
len
    
//return szBuffer
    //return szPMsg[id]

__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-12-2014 , 15:01   Re: Return string through native
Reply With Quote #2

You either need to switch to style 0 or you need to properly handle byref parameters as noted in the documentation:

PHP Code:
/* Registers a NATIVE.  When a plugin uses your native (you should distribute a .inc),
 * the handler will be called with two parameters: the calling plugin id, and the
 * number of parameters.
 * If you set style=1, the method of parameter passing is a tad more efficient.
 * Instead of "id, numParams", you label the native exactly as how the parameters
 *  should, in theory, be sent.  Then for each byreference parameter, you call
 *  param_convert(num).  This is theoretically more efficient but quite hacky.
 *  The method was discovered by dJeyL, props to him!
 */
native register_native(const name[], const handler[], style=0); 
__________________
fysiks is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-12-2014 , 15:22   Re: Return string through native
Reply With Quote #3

I think you can also simply return the string pointer with return szBuffer, but I don't clearly understand how this works so I'm not sure.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-12-2014 , 15:46   Re: Return string through native
Reply With Quote #4

Quote:
Originally Posted by Backstabnoob View Post
I think you can also simply return the string pointer with return szBuffer, but I don't clearly understand how this works so I'm not sure.
Strings must be passed byref, there are no pointers. So, the value that you use with the "return" keyword will be the value returned by the function (IIRC).
__________________
fysiks is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-12-2014 , 15:58   Re: Return string through native
Reply With Quote #5

This works so I don't see why a native wouldn't:

PHP Code:
get_user_name2id )
{
   new 
szName32 ]
   
get_user_nameidszNamecharsmaxszName )
   return 
szName
}

client_printidprint_chat"Your name is %s."get_user_name2id ) ) 
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
NiHiLaNTh
Way Past Expiration
Join Date: May 2009
Location: Latvia
Old 06-12-2014 , 16:04   Re: Return string through native
Reply With Quote #6

whats wrong with set_string?
__________________

NiHiLaNTh is offline
Send a message via Skype™ to NiHiLaNTh
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-12-2014 , 16:06   Re: Return string through native
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
Strings must be passed byref, there are no pointers. So, the value that you use with the "return" keyword will be the value returned by the function (IIRC).
There is actually some kind of pointers(not really), but I am afraid it will bunch up in memory since it allocates every time. Actually, i don't know if this works, never tried it, but it should.
In native function do something like:
PHP Code:
public native_return_string()
{
       return 
engfunc(EngFunc_AllocString"test_string");

And in the calling plugin function type something like:
PHP Code:
public calling_function()
{
       new 
pString native_return_string();
       new 
szString[32];
       
engfunc(EngFunc_SzFromIndexpStringszStringcharsmax(szString));

As i said, I don't know if this will allocate more memory every time you use it, and if it does, is there anyway to free the memory allocated with EngFunc_AllocString? Another thing I don't know is how efficient(fast) this will be.
klippy is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-12-2014 , 16:28   Re: Return string through native
Reply With Quote #8

Quote:
Originally Posted by NiHiLaNTh View Post
whats wrong with set_string?
There's nothing wrong with set_string, it just copies the string to the output, although this discussion kinda turned into something different.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-12-2014 , 16:32   Re: Return string through native
Reply With Quote #9

Quote:
Originally Posted by NiHiLaNTh View Post
whats wrong with set_string?
That is only used for style=0 IIRC. He is using style=1.
__________________

Last edited by fysiks; 06-12-2014 at 16:33.
fysiks is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 06-12-2014 , 16:33   Re: Return string through native
Reply With Quote #10

Before using szBuffer, you need to do
Code:
param_convert(2); // 2 being the parameter number.
__________________
hleV 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 13:36.


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