AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Saving all of this in nVault? (https://forums.alliedmods.net/showthread.php?t=308190)

redivcram 06-09-2018 18:41

Saving all of this in nVault?
 
PHP Code:

#include <amxmodx>
#include <nvault>

// The Data
enum {coinskeyschipsrocksgemsgoldsuccessinpawn};

// 1st Dimension: Player's ID
// 2nd Dimension: Max Data
new g_iDataHolder[33][8];

new 
g_iVault;

public 
plugin_init()
{
    
register_plugin("qwe""rty""uiop");

    
register_clcmd("change_data""CmdYourOwnData");
    
register_clcmd("save_data""SaveVault");

    
g_iVault nvault_open("hgrp");
}

public 
client_putinserver(id)
{
    new 
i;

    
// First set every data to 0
    
for(08i++)
        
g_iDataHolder[id][i] = 0

    
// successinpawn aka g_iDataHolder[id][7] should be set to 100 by default (For newcomers aka people who didn't save anything in vault)
    
g_iDataHolder[id][successinpawn] = 100;

    
LoadVault();
}

public 
ChangeYourOwnData(id)
{
    new 
szArgv_Select[2], szArgv_Set[4];

    
read_argv(1szArgv_Selectcharsmax(szArgv_Select));
    
read_argv(2szArgv_Setcharsmax(szArgv_Set));

    
// No checks for empty strings/boundaries necessary since its known what to type in this small temporary example

    
g_iDataHolder[id][str_to_num(szArgv_Select)] = str_to_num(szArgv_Set);

    return 
PLUGIN_CONTINUE;
}

public 
SaveVault() {return PLUGIN_CONTINUE;}
public 
LoadVault() {return PLUGIN_CONTINUE;} 

I know how to use nVault for storing simple data. This right here is not simple at all. I tried a couple of dumb ways, but none seem to do the thing. I can see the data in the vault file, but it never loads. How should it be done properly?

Bugsy 06-09-2018 19:09

Re: Saving all of this in nVault?
 
Look at nVault array. You can save arrays, including arrays sized with an enumerator.

redivcram 06-10-2018 08:52

Re: Saving all of this in nVault?
 
Thanks,

Here's what I've done.

PHP Code:

#include <amxmodx>
#include <nvault>
#include <nvault_array>

// The Data
enum _:SomeValuableData{coinskeyschipsrocksgemsgoldsuccessinpawn};

// 1st Dimension: Player's ID
// 2nd Dimension: Max Data
new g_iDataHolder[33][SomeValuableData];

new 
g_iVault;

public 
plugin_init()
{
    
register_plugin("qwe""rty""uiop");

    
register_clcmd("change_data""ChangeYourOwnData");
    
register_clcmd("save_data""SaveVault");

    
g_iVault nvault_open("hgrp");
}

public 
plugin_end()
    
nvault_close(g_iVault);

public 
client_putinserver(id)
{
    new 
i;

    
// First set every data to 0
    
for(08i++)
        
g_iDataHolder[id][i] = 0

    
// successinpawn aka g_iDataHolder[id][7] should be set to 100 by default (For newcomers aka people who didn't save anything in vault)
    
g_iDataHolder[id][successinpawn] = 100;

    
LoadVault(id);
}

public 
ChangeYourOwnData(id)
{
    new 
szArgv_Select[2], szArgv_Set[4];

    
read_argv(1szArgv_Selectcharsmax(szArgv_Select));
    
read_argv(2szArgv_Setcharsmax(szArgv_Set));

    
// No checks for empty strings/boundaries necessary since its known what to type in this small temporary example

    
g_iDataHolder[id][str_to_num(szArgv_Select)] = str_to_num(szArgv_Set);

    
SaveVault(id);

    return 
PLUGIN_CONTINUE;
}

public 
SaveVault(id
{
    new 
szAuthID[32], szKey[32];

    
get_user_authid(idszAuthIDcharsmax(szAuthID));

    
format(szKeycharsmax(szKey), "%s-heyo"szAuthID);

    
nvault_set_array(g_iVaultszKeyg_iDataHolder[id], sizeof(g_iDataHolder[]));

    return 
PLUGIN_HANDLED;


public 
LoadVault(id

    new 
szAuthID[32], szKey[32];

    
get_user_authid(idszAuthIDcharsmax(szAuthID));

    
format(szKeycharsmax(szKey), "%s-heyo"szAuthID);
     
    
nvault_get_array(g_iVaultszKeyg_iDataHolder[id], sizeof(g_iDataHolder[]));

    return 
PLUGIN_HANDLED;


But, here's an error

Code:

L 06/10/2018 - 14:37:11: [AMXX] Plugin ("nvault_problem.amxx") is setting itself as failed.
L 06/10/2018 - 14:37:12: [AMXX] Plugin says: [nVault Array] Can only use nvault_get_array() on data that was saved using nvault_set_array().
L 06/10/2018 - 14:37:12: [AMXX] Displaying debug trace (plugin "nvault_problem.amxx")
L 06/10/2018 - 14:37:12: [AMXX] Run time error 1: forced exit
L 06/10/2018 - 14:37:12: [AMXX]    [0] nvault_array.inc::nvault_get_array (line 107)
L 06/10/2018 - 14:37:12: [AMXX]    [1] nvault_problem.sma::LoadVault (line 78)
L 06/10/2018 - 14:37:12: [AMXX]    [2] nvault_problem.sma::client_putinserver (line 38)

It is supposed to load the client's data when they connect. I performed a check before getting your modified nvault and the new nvault_array lib using nvault_lookup, but it doesn't work properly with arrays, I guess, I always seemed to get false when checking whether data existed with its suitable key.

Bugsy 06-10-2018 10:09

Re: Saving all of this in nVault?
 
Code:

[nVault Array] Can only use nvault_get_array() on data that was saved using nvault_set_array().
Based on the error it looks like you may have already stored data in string format under the same vault file and key for the player. When nvault_array attempts to load the data, it finds a string instead of the nvault_array-formatted data so it throws the error. If using nvault_array on a vault file that was previously used with nvault_set(), you must prune everything or use a new vault file.

redivcram 06-10-2018 10:33

Re: Saving all of this in nVault?
 
Thanks! I forgot to delete the vault file before using your codes. Everything works fine, for now.

redivcram 06-10-2018 14:30

Re: Saving all of this in nVault?
 
Can I save a 3dimensional array the same way or is there another way or no way at all?

maqi 06-10-2018 14:32

Re: Saving all of this in nVault?
 
Quote:

Multidimensional arrays are not directly supported, but if you use an enum to size your array, it can be accomplished.


All times are GMT -4. The time now is 04:42.

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