AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [?]saving a value on user id (https://forums.alliedmods.net/showthread.php?t=270829)

sentoki 08-30-2015 10:36

[?]saving a value on user id
 
Hello guys, I recently got into scripting and trying to learn while making an example plugin for myself.
I try to add many things to the vip plugin to get to know as much as I can.
I thought about ability for a player to open a vipmenu at any time ,doesn't matter he's dead or alive(and user can open menu and change his choice multiple times), then save his choice and give him selected item at the beginning of the next round.
For example player opens the menu, chooses let's say free armor, his choice is saved, when the next round starts he's given free armor and this happens at every round until he chooses something else.
My idea was to let the player open a menu at any time he wants, give every item a case number, then when he chooses, save that number as a variable value, and at the beginning of every round, check the value of a given variable and give items according to that value.

My question is, how do I save the value of that variable depending on a player? I mean, I think I somehow need to assign the value to the id's of players and how do I do that? or is it simpler than that?

The part pattern of the code would look something like this:
PHP Code:

<...>
public 
ShowVipMenu(idlvlcid){
    
    if(!
cmd_access(idlvlcid0))
        return 
PLUGIN_HANDLED

    
new menu menu_create("vipmenu""uservipmenu")

    
menu_additem(menu"option 1"""0// case 0
    
menu_additem(menu"option 2"""0// case 1
    
menu_additem(menu"option 3"""0// case 2
    
menu_additem(menu"option 4"""0// case 3
    
menu_additem(menu"option 5"""0// case 4
    
menu_additem(menu"option 6"""0)// case 5

    
menu_setprop(menuMPROP_EXITMEXIT_ALL)
    
menu_setprop(menuMPROP_PERPAGE6)
    
menu_setprop(menuMPROP_EXITNAME"exit")

    
menu_display(idmenu0)

    return 
PLUGIN_HANDLED
}

public 
savingChoice(idmenuitem){
    
    if(
item == MENU_EXIT){
        
menu_cancel(id)
        return 
PLUGIN_HANDLED
    
}

    new 
command[6], name[64], accesscallback

    menu_item_getinfo
(menuitemaccesscommandsizeof command 1namesizeof name 1callback)

    switch(
item){
        
        case 
0:{
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
userMenuChoice 1
        
}
        case 
1: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
userMenuChoice 2
        
}
        case 
2: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
userMenuChoice 3
        
}
        case 
3: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
userMenuChoice 4
        
}
        case 
4: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
userMenuChoice 5
        
}
        case 
5:{
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
userMenuChoice 6
        
}
    }

    
menu_destroy(menu)

    return 
PLUGIN_HANDLED
}

public 
LogEvent_RoundStart(iduserMenuChoice){
    
    if(
userMenuChoice == 1){
        
give_item(id"item to give")
    }
    
    else if(
userMenuChoice == 2){
        
give_item(id"item to give")
    }
    
    if(
userMenuChoice == 3){
        
give_item(id"item to give")
    }
    
    if(
userMenuChoice == 4){
        
give_item(id"item to give")
    }
    
    if(
userMenuChoice == 5){
        
give_item(id"item to give")
    }
    
    if(
userMenuChoice == 6){
        
give_item(id"item to give")
    }

    return 
PLUGIN_HANDLED
}

<...> 

I searched in forums but could find similar question answered, sorry if it exists and I missed it, link to that question would be appreciated.
any help is appreciated. Thank you

fysiks 08-30-2015 11:57

Re: [?]saving a value on user id
 
You simply create a global array with a size of 33. Then you index that array using "id" and assign the value you want to store to that cell of the array.

PHP Code:

new PlayerData[33]


...

function(
id)
{
    
PlayerData[id] = // Save the data for the player
}

other_function(id)
{
    new 
MyVariable PlayerData[id// Get saved data for player



Phant 08-30-2015 12:19

Re: [?]saving a value on user id
 
I think better to use MAX_PLAYERS than 33:
PHP Code:

new g_PlayersItemsIDs[MAX_PLAYERS

Also:
PHP Code:

new MyVariable PlayerData[id

In some cases not necessary at all. You can work with PlayerData[id] directly.

fysiks 08-30-2015 14:06

Re: [?]saving a value on user id
 
Quote:

Originally Posted by Phant (Post 2338328)
I think better to use MAX_PLAYERS than 33:
PHP Code:

new g_PlayersItemsIDs[MAX_PLAYERS


No, that is wrong for two reasons. MAX_PLAYERS is not a feature of the latest stable version of AMX Mod X (1.8.2), it is only available in the beta releases. Also, MAX_PLAYERS value is 32. You have to size the array with 33 for it to work on all servers.


Quote:

Originally Posted by Phant (Post 2338328)
Also:
PHP Code:

new MyVariable PlayerData[id

In some cases not necessary at all. You can work with PlayerData[id] directly.

I only created a variable for illustrative purposes (i.e. to show the usage of getting array data).

sentoki 09-01-2015 14:43

Re: [?]saving a value on user id
 
Thank you, this helped, but I bumped into another thing related to this array.
i print values to check whether they were assigned correctly, and on the part "mainvipmenu" the value remains what it was assigned, however i tried printing out the value of vipPlayerValues on the round start and it always gives me a 0, and so if it's a zero it is neither of the cases.
i commented on the code what I tried, but still same result:?
might you spot why this is happening? I would appreciate it.

PHP Code:


#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>


#define PLUGIN "learn vip plugin"
#define VERSION "1.0"
#define AUTHOR "me"

#define VIP_FLAG ADMIN_RESERVATION

new vipPlayerValues[33]


new const 
showvipmenu[]={
    
"say vipmenu""say_team vipmenu","say /vipmenu""say_team vipmenu""say menu""say_team menu""say /menu""say_team /menu"
}

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)

   
    for(new 
0sizeof showvipmenui++)
        
register_clcmd(showvipmenu[i], "mainVipmenu"ADMIN_ALL"brings up vipmenu")
        
     
register_logevent("roundstart"2"0=World triggered""1=Round_Start")

    
    
}


public 
mainVipmenu(idlvlcid){

    if(
get_user_flags(id) & VIP_FLAG){ //Checks if player is VIP
        
new menu menu_create("vipmenu""vipmenuChoices")
    
        
menu_additem(menu"option 1"""0// case 0
        
menu_additem(menu"option 2"""0// case 1
        
menu_additem(menu"option 3"""0// case 2
        
menu_additem(menu"option 4"""0// case 3
        
menu_additem(menu"option 5"""0// case 4
        
menu_additem(menu"option 6"""0);// case 5
    
        
menu_setprop(menuMPROP_EXITMEXIT_ALL)
        
menu_setprop(menuMPROP_PERPAGE6)
        
menu_setprop(menuMPROP_EXITNAME"exit")
    
        
menu_display(idmenu0)
    }
    
    else{
        
client_print(idprint_chat"[VIP] you are not a VIP user!")
    }
    return 
PLUGIN_HANDLED                                                                                                                                   
}

public 
vipmenuChoices(idmenuitem){
    
    if(
item == MENU_EXIT){
        
        
menu_cancel(id)
    }
    
    new 
command[6], name[64], accesscallback
    
    menu_item_getinfo
(menuitemaccesscommandsizeof command 1namesizeof name 1callback)
    
    switch(
item){
        
        case 
0:{
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
vipPlayerValues[id] = 1
        
}
        case 
1: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
vipPlayerValues[id] = 2
        
}
        case 
2: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
vipPlayerValues[id] = 3
        
}
        case 
3: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
vipPlayerValues[id] = 4
        
}
        case 
4: {
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
vipPlayerValues[id] = 5
        
}
        case 
5:{
            
            
client_print(idprint_chat"The chosen item will be activated next round")
            
vipPlayerValues[id] = 6
        
}
    }
    
client_print(idprint_chat,"vipplayervalues is %d"vipPlayerValues[id]) //here value is printed as it should be, according to the number chosen
    
menu_destroy(menu)
    return 
PLUGIN_HANDLED // i tried PLUGIN_CONTINUE instead of PLUGIN_HANDLED same thing..
    //also tried return vipPlayerValues[id]
}


public 
roundstart(idvipPlayerValues[]){  //here tried public roundstart(id) instead

    
client_print(idprint_chat,"vipplayervalues is %d"vipPlayerValues[id]) //here always gives 0, and no further action

    
if(vipPlayerValues[id] == 1){
        
give_item(id"waepon_m4a1")
        
client_print(idprint_chat"[VIP] item given")
    }
    
    else if(
vipPlayerValues[id] == 2){
        
give_item(id"waepon_m4a1")
        
client_print(idprint_chat"[VIP] item given")
    }
    
    else if(
vipPlayerValues[id] == 3){
        
give_item(id"waepon_m4a1")
        
client_print(idprint_chat"[VIP] item given")
    
    }
    else if(
vipPlayerValues[id] == 4){
        
give_item(id"waepon_m4a1")
        
client_print(idprint_chat"[VIP] item given")
    }
    
    else if(
vipPlayerValues[id] == 5){
        
give_item(id"waepon_m4a1")
        
client_print(idprint_chat"[VIP] item given")
    }
    
    else if(
vipPlayerValues[id] == 6){
        
give_item(id"waepon_m4a1")
        
client_print(idprint_chat"[VIP] item given")
    
    }
    
    return 
PLUGIN_HANDLED



wickedd 09-01-2015 14:59

Re: [?]saving a value on user id
 
Round start doesn't passes the player index, use get_players and loop through them.

sentoki 09-02-2015 14:33

Re: [?]saving a value on user id
 
All worked well ^^. thank you all for the help


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

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