Raised This Month: $32 Target: $400
 8% 

Solved natives usage (not sure about title)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 06-07-2021 , 14:53   natives usage (not sure about title)
Reply With Quote #1

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)
}

Last edited by kww; 06-08-2021 at 15:41.
kww is offline
YankoNL
Junior Member
Join Date: Dec 2018
Old 06-07-2021 , 15:05   Re: natives usage (not sure about title)
Reply With Quote #2

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.
YankoNL is offline
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 06-07-2021 , 15:14   Re: natives usage (not sure about title)
Reply With Quote #3

Quote:
Originally Posted by YankoNL View Post
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?

Last edited by kww; 06-07-2021 at 15:16.
kww is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-07-2021 , 19:08   Re: natives usage (not sure about title)
Reply With Quote #4

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.
__________________









Last edited by CrazY.; 06-07-2021 at 19:09.
CrazY. is offline
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 06-07-2021 , 23:57   Re: natives usage (not sure about title)
Reply With Quote #5

Quote:
Originally Posted by CrazY. View Post
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?

Last edited by kww; 06-08-2021 at 00:29.
kww is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 06-08-2021 , 03:20   Re: natives usage (not sure about title)
Reply With Quote #6

Quote:
Originally Posted by kww View Post
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
__________________
My plugin:

Last edited by Celena Luna; 06-08-2021 at 03:20.
Celena Luna is offline
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 06-08-2021 , 04:21   Re: natives usage (not sure about title)
Reply With Quote #7

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;


Last edited by kww; 06-08-2021 at 04:23.
kww is offline
Old 06-08-2021, 05:23
Celena Luna
This message has been deleted by Celena Luna. Reason: no wait, it is nonsense...
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-08-2021 , 14:04   Re: natives usage (not sure about title)
Reply With Quote #8

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


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) }
__________________









Last edited by CrazY.; 06-08-2021 at 14:05.
CrazY. is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-08-2021 , 15:25   Re: natives usage (not sure about title)
Reply With Quote #9

cost default value should be 0 and not -1 putting it -1 doesn't make alot of sense since 0 is for free
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 06-08-2021 , 15:41   Re: natives usage (not sure about title)
Reply With Quote #10

Quote:
Originally Posted by CrazY. View Post
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 View Post
cost default value should be 0 and not -1 putting it -1 doesn't make alot of sense since 0 is for free
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

Last edited by kww; 06-08-2021 at 15:47.
kww 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 14:12.


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