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

Problem to return string from nativ


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kostov
Member
Join Date: Jan 2010
Location: Bulgaria, Sofia
Old 06-28-2012 , 15:34   Problem to return string from nativ
Reply With Quote #1

Hi,

How can I return a string?

PHP Code:
public plugin_init()
{
    
register_cvar("amx_mode_prefix""[Test]");
}

public 
plugin_natives()
{
    
register_native("get_server_prefix""native_get_server_prefix"1);
}

public 
native_get_server_prefix()
{
    new 
iPrefix[64]; 
    
get_cvar_string("amx_mode_prefix"iPrefixsizeof iPrefix 1);
    return 
iPrefix;

PHP Code:
client_print(idprint_chat"%s test messages"get_server_prefix()); 
This does not work when trying to use the function earns error in console.

With what I saw, PAWN can not make a return string, only values?
kostov is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-28-2012 , 15:50   Re: Problem to return string from nativ
Reply With Quote #2

Don't remember if it's possible to return string in natives. Since you tested, I guess not. You can anyway pass it by reference, using set_string. You could also return set_string, so it will return the prefix length.
__________________
Arkshine is offline
kostov
Member
Join Date: Jan 2010
Location: Bulgaria, Sofia
Old 06-28-2012 , 16:13   Re: Problem to return string from nativ
Reply With Quote #3

Quote:
Originally Posted by Arkshine View Post
Don't remember if it's possible to return string in natives. Since you tested, I guess not. You can anyway pass it by reference, using set_string. You could also return set_string, so it will return the prefix length.
I did tests with set_string:
PHP Code:
public native_get_server_prefix()
{
    new 
iPrefix[64]; 
    
get_cvar_string("amx_mode_prefix"iPrefixsizeof iPrefix 1);
    
set_string(1iPrefixget_param(2));

But still not working. Does not make a mistake in the syntax of set_string?

Last edited by kostov; 06-28-2012 at 16:15.
kostov is offline
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 06-28-2012 , 17:14   Re: Problem to return string from nativ
Reply With Quote #4

You can do this too...
PHP Code:
public plugin_init()
{
    
register_cvar("amx_mode_prefix""[Test]");
}

public 
plugin_natives()
{
    
register_native("get_server_prefix""native_get_server_prefix"1);
}

public 
native_get_server_prefixbuffer[ ], len )
{
    
get_cvar_string"amx_mode_prefix"bufferlen );

PHP Code:
new buffer64 ];
get_server_prefixbuffercharsmaxbuffer ) );
client_printidprint_chat"%s test messages"buffer ); 
__________________

Last edited by Neeeeeeeeeel.-; 06-28-2012 at 17:14.
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-28-2012 , 17:29   Re: Problem to return string from nativ
Reply With Quote #5

Style 1 is deprecated, and style 0 just works fine.

Following code is working, and, kostov, you have put style 1 in your first post, and you have implemented native as style 0, so put 0 in register_native line.
PHP Code:
public plugin_natives()
{
    
register_native("get_hostname""get_hostname")
}

public 
get_hostname(  )
{
    new 
szHostName[64]
    
get_pcvar_string(hostnameszHostNamecharsmax(szHostName))
    
set_string(1szHostNameget_param(2))

__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 06-28-2012 at 17:48.
ConnorMcLeod is offline
kostov
Member
Join Date: Jan 2010
Location: Bulgaria, Sofia
Old 06-29-2012 , 04:05   Re: Problem to return string from nativ
Reply With Quote #6

The error is gone but not earning a prefix:

PHP Code:
public plugin_natives()
{
    
register_native("get_server_prefix""native_get_server_prefix");
}

public 
native_get_server_prefix()
{
    new 
iPrefix[64]; 
    
get_pcvar_string(iGlobalPrefixiPrefixcharsmax(iPrefix));
    
set_string(1iPrefixget_param(2));

And:

Code:
Kostov : /prefix
 test messages
That does not earn the specified prefix from cvar?
kostov is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-30-2012 , 13:44   Re: Problem to return string from nativ
Reply With Quote #7

Show your test code.
__________________
fysiks is offline
kostov
Member
Join Date: Jan 2010
Location: Bulgaria, Sofia
Old 07-01-2012 , 06:52   Re: Problem to return string from nativ
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
Show your test code.
Main Plugin:
PHP Code:
#include <amxmodx>

new iGlobalPrefix;

public 
plugin_init()
{
    
register_plugin("Test Code""0.1""kostov");
    
    
// Global prefix
    
iGlobalPrefix register_cvar("amx_mode_prefix""[Prefix]");
}

public 
plugin_natives() 

    
register_library("prefix");
    
register_native("get_server_prefix""native_get_server_prefix"); 


public 
native_get_server_prefix() 

    new 
iPrefix[64];  
    
get_pcvar_string(iGlobalPrefixiPrefixsizeof iPrefix 1); 
    
set_string(1iPrefixget_param(2)); 

Main library
PHP Code:
#if defined _prefix_included
  #endinput
#endif
#define _prefix_included

#if !defined charsmax
    #define charsmax(%1) sizeof(%1)-1
#endif

native get_server_prefix(); 
Test Plugin:
PHP Code:
#include <amxmodx>
#include <prefix>

public plugin_init()
{
    
register_plugin("Test Plugin""0.1""kostov");
    
    
register_clcmd("say /prefix""TestFunc");
}

public 
TestFunc(id)
{
    
client_print(idprint_chat"%s test message"get_server_prefix());

And result:
Code:
Kostov : /prefix
 test messages
kostov is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-01-2012 , 07:01   Re: Problem to return string from nativ
Reply With Quote #9

You have to retrieve strings byref :

PHP Code:
native get_server_prefix(prefix[], len); 
PHP Code:
public TestFunc(id)
{
    new 
szPrefix[64]
    
get_server_prefix(szPrefixcharsmax(szPrefix))
    
client_print(idprint_chat"%s test message"get_server_prefix());

If your native is only to retrieve cvar, don't use a fake native, use directly get_pcvar_string(), result is the same.

In the main plugin :
PHP Code:
new iGlobalPrefix;

public 
plugin_init()
{
    
register_plugin("Test Code""0.1""kostov");
    
    
// Global prefix
    
iGlobalPrefix register_cvar("amx_mode_prefix""[Prefix]");

In other plugins :
PHP Code:
new iGlobalPrefix;

public 
plugin_init()
{
    
register_plugin("Test Code""0.1""kostov");
    
    
// Global prefix
    
iGlobalPrefix get_cvar_pointer("amx_mode_prefix");

And in all plugins you can put that function :
PHP Code:
get_global_prefix()
{
    new 
prefix[64];
    
get_pcvar_string(iGlobalPrefixprefixcharsmax(prefix));
    return 
prefix;

Or in .inc file :

PHP Code:
get_global_prefix()
{
    new 
prefix[64];
    
get_pcvar_string(iGlobalPrefixprefixcharsmax(prefix));
    return 
prefix;

And use it like :

PHP Code:
client_print(idprint_chat"%s This is a test"get_global_prefix


Other way, you can put in the .inc file :
Code:
#if defined _prefix_included
  #endinput
#endif
#define _prefix_included

#if !defined charsmax
    #define charsmax(%1) ( sizeof(%1)-1 )
#endif

stock get_server_prefix()
{
	static pcvarPrefix;
	if( !pcvarPrefix )
	{
		static amx_mode_prefix[] = "amx_mode_prefix";
		pcvarPrefix = get_cvar_pointer(amx_mode_prefix);
		if( !pcvarPrefix )
		{
			pcvarPrefix = register_cvar(amx_mode_prefix, "[Prefix]");
		}
	}

	new szPrefix[64];
	get_pcvar_string(pcvarPrefix, szPrefix, charsmax(szPrefix));
	return szPrefix;
}
And in plugin use :

PHP Code:
client_print(idprint_chat"%s This is a test"get_server_prefix()) 
Then you don't need a main plugin.
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 07-01-2012 at 07:08.
ConnorMcLeod is offline
kostov
Member
Join Date: Jan 2010
Location: Bulgaria, Sofia
Old 07-01-2012 , 07:24   Re: Problem to return string from nativ
Reply With Quote #10

Thank you ConnorMcLeod. It works with the library
kostov 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 08:58.


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