AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED] Problem with a native that returns a string (https://forums.alliedmods.net/showthread.php?t=273625)

OciXCrom 10-23-2015 09:35

[SOLVED] Problem with a native that returns a string
 
PHP Code:

new const g_szNotFound[] = "Not Found"

new const g_szModels[][] = {
    
"weapon_mp5navy""Balrog Ethereal""models/iplay_furien/v_ethereal.mdl""models/iplay_furien/p_ethereal.mdl""models/iplay_furien/w_ethereal.mdl""models/w_mp5.mdl"
}

public 
plugin_natives()
{
    
register_library("furien_weapons")
    
register_native("furien_get_weaponname""_furien_get_weaponname")
}

public 
_furien_get_weaponname(szWeapon[])
{
    static 
szName[32]
    
    for(new 
0sizeof(g_szModels) - 5+= 6)
    {
        if(
equali(szWeapong_szModels[i]))
            
formatex(szNamecharsmax(szName), "%s"g_szModels[1])
    }
    
    
formatex(szNamecharsmax(szName), "%s"g_szNotFound)
    return 
szName


The result is this: http://i.imgur.com/13e4iNk.jpg
I'm using furien_get_weaponname("weapon_mp5navy"). I have no idea how I'm getting the last sound that was played, instead of the weapon's name, which in this case is "Balrog Ethereal".

Arkshine 10-23-2015 09:57

Re: [Help ASAP] Problem with a native that returns a string
 
Don't do that, pass string by reference.

OciXCrom 10-23-2015 10:02

Re: [Help ASAP] Problem with a native that returns a string
 
By reference? Could you please explain, since I have no idea what that means?

HamletEagle 10-23-2015 10:38

Re: [Help ASAP] Problem with a native that returns a string
 
Don't return a string, this was said 1000th times already, I am not sure why people keep doing this. It just stupid.

You have some natives called get_string/set_string, search for them. Also the loop looks silly, instead of it check if g_szModels[0] is the name that you get from the native(get_string) and use set_string with g_szModels[1].

Keep in mind that you are working with natives registered with style 0, so they don't pass the parameters. They only pass the plugin index and the param num. You have to use specific natives to get the params value(get_param/get_string etc).

OciXCrom 10-23-2015 12:47

Re: [Help ASAP] Problem with a native that returns a string
 
PHP Code:

public _furien_get_weaponname(szWeapon[], szString)
{
    new 
i
    
    
for(0sizeof(g_szModels) - 3+= 4)
    {
        if(
equali(szWeapong_szModels[i]))
            
set_string(1g_szModels[1], get_param(2))
    }
    
    return 
1
}

native furien_get_weaponname(szWeapon[], szString[], const iMax)

public 
test(id)
{
    new 
szName[32]
    
furien_get_weaponname("weapon_mp5navy"szNamecharsmax(szName))
    
client_print(idprint_chat"%s"szName)


//Edit: The "test" function returns a blank string.

HamletEagle 10-23-2015 15:22

Re: [Help ASAP] Problem with a native that returns a string
 
I told you, you don't have any of the native params passed in the function header(public _furien_get_weaponname(szWeapon[], szString) should be _furien_get_weaponname(PluginIndex, PluginParams)).

So this is wrong:
PHP Code:

if(equali(szWeapong_szModels[i])) 

Use get_string to retrieve the passed string. Also set_string(1,...) should be set_string(2,...), because the second param is your buffer.

But, what do you want to do ? The loop does not make sense. You want to return only the weapon name associated with weapon_* ?

OciXCrom 10-23-2015 15:43

Re: [Help ASAP] Problem with a native that returns a string
 
I want to get the weapon name that corresponds to the given weapon, so if the weapon is "weapon_mp5navy", I would get "Balrog Ethereal". Ignore the other things in the array, because this isn't the full code of the plugin.

So, I changed it like this now:

PHP Code:

public _furien_get_weaponname(PluginIndexPluginParams)
{
    new 
szWeapon[32], i
    get_string
(PluginParamsszWeaponcharsmax(szWeapon))
    
    for(
0sizeof(g_szModels) - 3+= 4)
    {
        if(
equali(szWeapong_szModels[i]))
        {
            
set_string(2g_szModels[1], get_param(2))
            break
        }
    }
    
    return 
1


And the native is:

PHP Code:

native furien_get_weaponname(szWeapon[], szString[], const iMax

The test function:

PHP Code:

public test(id)
{
    new 
szName[32]
    
furien_get_weaponname("weapon_mp5navy"szNamecharsmax(szName))
    
client_print(idprint_chat"The name is %s"szName)


I'm still not getting the desired name.

This is what I'm trying to do - http://i.imgur.com/RS7k4sa.jpg
I made the image with a simple array of strings in the same plugin, but I want to add the name in my other plugin, where all the weapon models are.

HamletEagle 10-23-2015 15:55

Re: [Help ASAP] Problem with a native that returns a string
 
PluginParams is the number of parameters. You pass the weapon name as the first argument, so it should be get_string(1,....).

OciXCrom 10-24-2015 08:26

Re: [Help ASAP] Problem with a native that returns a string
 
It works now. Thank you!


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

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