AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved natives usage (not sure about title) (https://forums.alliedmods.net/showthread.php?t=332907)

kww 06-07-2021 14:53

natives usage (not sure about title)
 
hi men! i'm making menu in that i will add items from other plugins with natives (just as in ZP).
i want to add some free items and some paid.
If cost = -1 then it [cost] will not be shown in menu. but when i trying to add free item like
Code:

var = register_item("Free scout")
it shows me random number. if i adding item like this:
Code:

var = register_item("Free AK", -1)
it works perfectly.

How are natives works? What i have in my include file:
Code:

native register_item(const name[], cost = -1)
The way i hide item cost:
Code:

if(item_cost == -1) {
        formatex(szText, charsmax(szText), "%s", item_name)
} else {
        formatex(szText, charsmax(szText), "%s \r[\y$\w%i\r]", item_name, item_cost)
}


YankoNL 06-07-2021 15:05

Re: natives usage (not sure about title)
 
When you register and Item there should be always a name and price because it needs that info as shown:
PHP Code:

native register_item(const name[], cost = -1

If you set only the name, plugin will get confused because you left 'cost' empty.
If you set only price, the plugin will get confused again because you left 'name' empty.

kww 06-07-2021 15:14

Re: natives usage (not sure about title)
 
Quote:

Originally Posted by YankoNL (Post 2749184)
When you register and Item there should be always a name and price because it needs that info as shown:
PHP Code:

native register_item(const name[], cost = -1

If you set only the name, plugin will get confused because you left 'cost' empty.
If you set only price, the plugin will get confused again because you left 'name' empty.

So why I must set price in native if i can't use it as a default value?

CrazY. 06-07-2021 19:08

Re: natives usage (not sure about title)
 
What you did is completely valid, there should be some other problem in your code. If you're not willing to post the source, debug the code.

kww 06-07-2021 23:57

Re: natives usage (not sure about title)
 
Quote:

Originally Posted by CrazY. (Post 2749203)
What you did is completely valid, there should be some other problem in your code. If you're not willing to post the source, debug the code.

Should i post the whole code or just a part where i registering items? What exactly are u need?

Celena Luna 06-08-2021 03:20

Re: natives usage (not sure about title)
 
Quote:

Originally Posted by kww (Post 2749223)
Should i post the whole code or just a part where i registering items? What exactly are u need?

Post the native of the plugin that created the register_item native
or best, that whole plugin

kww 06-08-2021 04:21

Re: natives usage (not sure about title)
 
ok here it is:
PHP Code:

#include <amxmodx>
#include <cstrike>
#include <fakemeta>

#define PLUGIN_NAME "plugin name okay"

// Game vars
new g_arrays_created // to prevent stuff from being registered before initializing arrays

// Extra Items vars
new Array:ItemName
new Array:ItemCost
new g_extraitems_count // loaded extra items counter

// Some forward handlers
new g_extraItemSelectedg_result

public plugin_init() {
    
register_plugin(PLUGIN_NAME"stolen from ZP by MeRcyLeZZ""kww")
    
    
register_clcmd("menu""menu_main"ADMIN_ALL_)
    
register_clcmd("say /menu""menu_main"ADMIN_ALL_)
    
register_clcmd("say_team /menu""menu_main"ADMIN_ALL_)
    
    
g_extraItemSelected CreateMultiForward("item_selected"ET_CONTINUEFP_CELLFP_CELL)
}

public 
plugin_precache() {    
    
ItemName ArrayCreate(321)
    
ItemCost ArrayCreate(11)
    
    
// Allow registering stuff now
    
g_arrays_created true
}

public 
plugin_end() {
    
//Destroy arrays to free up memory
    
ArrayDestroy(ItemName)
    
ArrayDestroy(ItemCost)
    
    
g_arrays_created false
}

public 
plugin_natives()
    
register_native("register_item""native_register_item"1)

// Native: zp_register_extra_item
public native_register_item(const name[], cost) {
    
// Strings passed byref
    
param_convert(1)
    
    
// Arrays not yet initialized
    
if (!g_arrays_created) {
        
log_error(AMX_ERR_NATIVE"[%s] Can't register extra item yet (%s)"PLUGIN_NAMEname)
        return -
1
    
}
    
    
// Item name is empty
    
if (strlen(name) < 1) {
        
log_error(AMX_ERR_NATIVE"[%s] Can't register extra item with an empty name"PLUGIN_NAME)
        return -
1
    
}
    
    
// Already registered
    
new indexextraitem_name[32]
    for (
index 0index g_extraitems_countindex++)
    {
        
ArrayGetString(ItemNameindexextraitem_namecharsmax(extraitem_name))
        if (
equali(nameextraitem_name))
        {
            
log_error(AMX_ERR_NATIVE"[%s] Extra item already registered (%s)"PLUGIN_NAMEname)
            return -
1
        
}
    }
    
    
// Add the item
    
ArrayPushString(ItemNamename)
    
ArrayPushCell(ItemCostcost)
    
    
// Increase registered items counter
    
g_extraitems_count++
    
    
// Inform in server console that item is registered
    
log_amx("An item ^"%s^" successfully registered with ID: %i"nameg_extraitems_count-1)
    
    
// Return id under which we registered the item
    
return g_extraitems_count-1;
}

// Extra Items Menu
//show_menu_extras(id)
public menu_main(id) {
    if(!
is_user_connected(id))
        return 
PLUGIN_HANDLED
    
    
static menuszText[128], item_iditem_name[32], item_costszInfo[6]
    
    
// Title
    
formatex(szTextcharsmax(szText), "Server menu")
    
menu menu_create(szText"menu_handler")
    
    
// Item List
    
for(item_id 0item_id g_extraitems_countitem_id++)
    {
        
ArrayGetString(ItemNameitem_iditem_namecharsmax(item_name))
        
item_cost ArrayGetCell(ItemCostitem_id)
        
        
// Add Item Name and Cost
        
if(item_cost == -1) {
            
formatex(szTextcharsmax(szText), "%s"item_name)
        }
        else {
            
formatex(szTextcharsmax(szText), "%s \r[\y$\w%i\r]"item_nameitem_cost)
        }
        
        
num_to_str(item_idszInfocharsmax(szInfo))
        
menu_additem(menuszTextszInfo)
    }
    
    
// No items to display?
    
if (menu_items(menu) <= 0) {
        
client_print(idprint_chat"ERROR: There are no items in menu")
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED
    
}
    
    
menu_display(idmenu)
    return 
PLUGIN_HANDLED
}

// Extra Items Menu
public menu_handler(idmenuitem) {
    if(
item == MENU_EXIT) {
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED
    
}
    
    
// Retrieve extra item id
    
static szData[6], dummyitem_id
    menu_item_getinfo
(menuitemdummyszDatacharsmax(szData), __dummy)
    
item_id str_to_num(szData)
    
    
// Attempt to choose the item
    
ExecuteForward(g_extraItemSelectedg_resultiditem_id)
    
menu_destroy(menu)
    return 
PLUGIN_HANDLED;



CrazY. 06-08-2021 14:04

Re: natives usage (not sure about title)
 
I've tested your code, it worked without the problem you described.

Code:


L 06/08/2021 - 14:55:42: [testplugin.amxx] An item "item 1" successfully registered with ID: 0
item cost -> -1
L 06/08/2021 - 14:55:42: [testplugin.amxx] An item "item 2" successfully registered with ID: 1
item cost -> -1
L 06/08/2021 - 14:55:42: [testplugin.amxx] An item "item 3" successfully registered with ID: 2
item cost -> 10

https://i.imgur.com/Tgjetjw.png

Code:
#include <amxmodx> native register_item(const item[], cost=-1) public plugin_init() {     register_plugin("Plugin", "Version", "Author")     register_item("item 1")     register_item("item 2", -1)     register_item("item 3", 10) }

Natsheh 06-08-2021 15:25

Re: natives usage (not sure about title)
 
cost default value should be 0 and not -1 putting it -1 doesn't make alot of sense since 0 is for free :3

kww 06-08-2021 15:41

Re: natives usage (not sure about title)
 
Quote:

Originally Posted by CrazY. (Post 2749269)
I've tested your code, it worked without the problem you described.

Oh.. I recompiled the code and it now works as should. Idk where the mistype was but earlier it shown me:
Code:

1. olala [$2345]
and "2345" wasn't a random number. It shown me every time I recompiled this code

Quote:

Originally Posted by Natsheh (Post 2749272)
cost default value should be 0 and not -1 putting it -1 doesn't make alot of sense since 0 is for free :3

i want to make it like:
if -1 then it is free as you wrote but cost will be hidden (e.g. item is added and can be used but is blocked from buying by cvar (maybe i'll add this, i'm not sure))
if 0 or more it will be shown as [$0] or more


All times are GMT -4. The time now is 02:33.

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