AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED] Saving upgrades with nVault (https://forums.alliedmods.net/showthread.php?t=273728)

OciXCrom 10-25-2015 11:00

[SOLVED] Saving upgrades with nVault
 
I'm trying to save the player's upgrades with nVault.

PHP Code:

new g_iUpgrades[33][33

PHP Code:

new const g_szUpgrades[][] = {
    
"Increase Alien Speed""2130""Your ^3Alien running speed ^1is now ^4increased",
    
"Additional Alien Health""2650""You will now get more ^3health points ^1as an ^4Alien",
    
"Additional predator Health""3200""You will now get more ^3health points ^1as a ^4Predator",
    
"Decrease Scream Cooldown""750""You can now ^3scream ^1more often",
    
"Unlock Ultra Classes""5000""You now have access to the ^3Ultra Classes^1"


PHP Code:

public SaveData(idszAuthId[35])
{
    new 
szVaultKey[64], szVaultData[256]
    
format(szVaultKeycharsmax(szVaultKey), "%s"szAuthId)
    
    for(new 
0sizeof(g_szUpgrades) - 2+= 3)
        
add(szVaultDatacharsmax(szVaultData), "%i#"g_iUpgrades[id][i])
    
    
nvault_set(g_VaultszVaultKeyszVaultData)
    return 
PLUGIN_CONTINUE
}

LoadData(id)
{
    new 
szAuthId[35], szVaultKey[64], szVaultData[256]
    
get_user_authid(idszAuthIdcharsmax(szAuthId))
    
format(szVaultKeycharsmax(szVaultKey), "%s"szAuthId)
    
    for(new 
0sizeof(g_szUpgrades) - 2+= 3)
        
add(szVaultDatacharsmax(szVaultData), "%i#"g_iUpgrades[id][i])
    
    
nvault_get(g_VaultszVaultKeyszVaultDatacharsmax(szVaultData))
    
replace_all(szVaultDatacharsmax(szVaultData), "#"" ")
    
    
/* The "fun" part starts here */
    
new szUpgrades[32]
    
    for(new 
0sizeof(g_szUpgrades) - 2+= 3)
    {
        
parse(szVaultDataszUpgrades1)
        
g_iUpgrades[id][i] = str_to_num(szUpgrades[i])
    }
    
/* This code doesn't work */
    
    
return PLUGIN_CONTINUE


PHP Code:

stock set_upgrade(idiUpgrade)
    
g_iUpgrades[id][iUpgrade] = 1

stock is_upgraded
(idiUpgrade)
    return 
g_iUpgrades[id][iUpgrade

I'm used to saving things with only one number, but I have no idea how to save a value that has two arrays - g_iUpgrades[id][iUpgrade]. The "iUpgrade" is the upgrade's ID, according to "g_szUpgrades", so it can be 0, 3, 6, 9, 12... Is this even a good way of saving the upgrades?

Bugsy 10-25-2015 11:13

Re: Saving upgrades with nVault
 
Could use a bitsum to record each players upgrade status and then store the resulting numerical value to nvault.

So you would have 5 upgrades as you do in your list. If the player has upgrades 1 and 3, their bitsum would look like 00000101 [1<<0 OR 1<<2] which is equal to "5". I can write this for you later.

OciXCrom 10-25-2015 11:25

Re: Saving upgrades with nVault
 
Thank you, I would appreciate it. I'm not familiar with bitsums yet. Also, would adding an upgrade in the future affect the saved data?

Bugsy 10-25-2015 12:40

Re: Saving upgrades with nVault
 
It would have no impact. This method allows up to 32 updates. If you really needed more it could be accomplished

OciXCrom 10-25-2015 13:22

Re: Saving upgrades with nVault
 
Good, I won't need that many.

Depresie 10-25-2015 13:34

Re: Saving upgrades with nVault
 
im wondering, if you are trying to create abilities that can be upgraded multiple times, for example upgrade health to lvl 2, lvl 3 etc how should the array be written?

OciXCrom 10-25-2015 13:37

Re: Saving upgrades with nVault
 
That probably won't change much, because now if you have an upgrade, the array is g_iUpgrade[id][iUpgrade] = 1, so for the next level it would just be equal to 2.

Bugsy 10-25-2015 15:35

Re: Saving upgrades with nVault
 
So does each of your 5 upgrades in your original post have multiple levels, or is it just a yes/no for each?

OciXCrom 10-25-2015 15:57

Re: Saving upgrades with nVault
 
It's only yes/no.

Bugsy 10-25-2015 17:04

Re: Saving upgrades with nVault
 
Untested

Edit: Added load\save data on authorized\disconnect.
PHP Code:


#include <amxmodx>
#include <nvault>

new const szVersion[] = "0.1";

const 
MAXPLAYERS 32;

enum UpgradeTypes
{
    
utIncAlienSpeed,
    
utAddAlienHealth,
    
utAddPredHealth,
    
utDecrScreamCooldown,
    
utUnlockUltraClasses
}

enum UpgradeInfo
{
    
szDescription64 ],
    
iValue,
    
szMessage64 ]
}

new const 
g_UpgradesUpgradeTypes ][ UpgradeInfo ] = 
{
    { 
"Increase Alien Speed" 2130 "Your ^3Alien running speed ^1is now ^4increased" } ,
    { 
"Additional Alien Health" 2650 "You will now get more ^3health points ^1as an ^4Alien" } ,
    { 
"Additional predator Health" 3200 "You will now get more ^3health points ^1as a ^4Predator" } ,
    { 
"Decrease Scream Cooldown" 750 "You can now ^3scream ^1more often" } ,
    { 
"Unlock Ultra Classes" 5000 "You now have access to the ^3Ultra Classes^1" }
};

new 
g_PlayerUpgradesMAXPLAYERS ];
new 
g_szAuthIDMAXPLAYERS ][ 34 ];
new 
g_Vault;

public 
plugin_init() 
{
    
register_plugin"Upgrade Example" szVersion "bugsy" );

    if ( ( 
g_Vault nvault_open"upgrades" ) ) == INVALID_HANDLE )
        
set_fail_state"Error opening nvault" );
}

public 
plugin_end()
{
    
nvault_closeg_Vault );
}

public 
client_authorizedid )
{
    
get_user_authidid g_szAuthIDid ] , charsmaxg_szAuthID[] ) );
    
LoadUpgradesid );
}

public 
client_disconnectid )
{
    
SaveUpgradesid );
}

public 
AddUpgradeiPlayer UpgradeTypes:utUpgrade )
{
    
g_PlayerUpgradesiPlayer ] |= ( << _:utUpgrade );
    
    
client_printiPlayer print_chat "%s upgrade added: %s" g_UpgradesutUpgrade ][ szDescription ] , g_UpgradesutUpgrade ][ szMessage ] );
}

public 
RemoveUpgradeiPlayer UpgradeTypes:utUpgrade )
{
    
g_PlayerUpgradesiPlayer ] &= ~( << _:utUpgrade );
    
    
client_printiPlayer print_chat "%s upgrade has been removed" g_UpgradesutUpgrade ][ szDescription ] );
}

public 
ClearUpgradesiPlayer UpgradeTypes:utUpgrade )
{
    
g_PlayerUpgradesiPlayer ] = _:0;
    
    
client_printiPlayer print_chat "All upgrades have been removed" );
}

public 
bool:PlayerHasUpgradeiPlayer UpgradeTypes:utUpgrade )
{
    return 
bool:!!( g_PlayerUpgradesiPlayer ] & ( << _:utUpgrade ) );
}

public 
SaveUpgradesid )
{
    new 
szVal12 ];
    
    
num_to_strg_PlayerUpgradesid ] , szVal charsmaxszVal ) );
    
nvault_setg_Vault g_szAuthIDid ] , szVal );
}

public 
LoadUpgradesid )
{
    
g_PlayerUpgradesid ] = nvault_getg_Vault g_szAuthIDid ] );




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

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