AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problem to return string from nativ (https://forums.alliedmods.net/showthread.php?t=188630)

kostov 06-28-2012 15:34

Problem to return string from nativ
 
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?

Arkshine 06-28-2012 15:50

Re: Problem to return string from nativ
 
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.

kostov 06-28-2012 16:13

Re: Problem to return string from nativ
 
Quote:

Originally Posted by Arkshine (Post 1738357)
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?

Neeeeeeeeeel.- 06-28-2012 17:14

Re: Problem to return string from nativ
 
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 ); 


ConnorMcLeod 06-28-2012 17:29

Re: Problem to return string from nativ
 
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))



kostov 06-29-2012 04:05

Re: Problem to return string from nativ
 
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?

fysiks 06-30-2012 13:44

Re: Problem to return string from nativ
 
Show your test code.

kostov 07-01-2012 06:52

Re: Problem to return string from nativ
 
Quote:

Originally Posted by fysiks (Post 1739821)
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


ConnorMcLeod 07-01-2012 07:01

Re: Problem to return string from nativ
 
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.

kostov 07-01-2012 07:24

Re: Problem to return string from nativ
 
Thank you ConnorMcLeod. It works with the library


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

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