AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [SOLVED] Array Question (https://forums.alliedmods.net/showthread.php?t=184755)

Razmo51 05-08-2012 15:54

[SOLVED] Array Question
 
Hello

Today, I'm looking to create an array with 'default value' (don't know how to say that in English ><)

I saw this post: http://forums.alliedmods.net/showthread.php?t=182348&

but I don't understand I tested this:

PHP Code:

enum AnEnum

    
AnEnum_FirstValue 5000,
    
AnEnum_SecondValue 6000,
    
AnEnum_ThirdValue 8000,
};

new 
g_var[AnEnum];

// ...


// in a test command:     
PrintToChatAll("test 2 first:%i second:%i third:%i",g_var[AnEnum_FirstValue],g_var[AnEnum_SecondValue],g_var[AnEnum_ThirdValue]); 

return:
test 2 first:0 second:0 third:0


how can can I return the value in the enum ? (5000,6000,8000) ?

thx

Powerlord 05-08-2012 16:36

Re: Array Question
 
As asherkin pointed out in the other thread, enums don't work that way.

If you want to create an array with default values, you can do this:

PHP Code:

new g_var[] = { 500060008000 }; 

and use it like this:

PHP Code:

PrintToChatAll("test 2 first:%i second:%i third:%i",g_var[0],g_var[1],g_var[2]); 

Note: I didn't actually test this, but it does compile.

Razmo51 05-08-2012 17:26

Re: Array Question
 
and is it possible to associate a String to the value

Eg:

"Smoke" -> 5000
"Flash" -> 6000
"Godmode" -> 8000

what should I do to perform this ?

berni 05-08-2012 18:35

Re: Array Question
 
Either create a string array and a cell array or use tries

Razmo51 05-09-2012 08:41

Re: Array Question
 
Quote:

Originally Posted by berni (Post 1705144)
Either create a string array and a cell array

and how to do that ?

sorry, but I'm a noob in array

Dr!fter 05-09-2012 09:56

Re: Array Question
 
Quote:

Originally Posted by Razmo51 (Post 1705480)
and how to do that ?

sorry, but I'm a noob in array

Use a trie. Its faster and easy to use.

Code:
new Handle:g_trie;//global trie public OnPluginStart() {     g_trie = CreateTrie();//Create         SetTrieValue(g_trie, "Smoke", 5000); //Store the values     SetTrieValue(g_trie, "Flash", 6000);     SetTrieValue(g_trie, "Godmode", 8000); } public OnPluginEnd() {     CloseHandle(g_trie);//Make sure to close the trie. } MyRandomFunction() {     //Grab the Smoke value.     new value;     if(GetTrieValue(g_trie, "Smoke", value))     {         PrintToServer("The value for smoke is %i", value);     }     else     {         PrintToServer("Failed to get the value for smoke from the trie");     } }

Razmo51 05-09-2012 10:36

Re: Array Question
 
Quote:

Originally Posted by Dr!fter (Post 1705565)
Use a trie. Its faster and easy to use.

Code:
new Handle:g_trie;//global trie public OnPluginStart() { &nbsp;&nbsp;&nbsp;&nbsp;g_trie = CreateTrie();//Create &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;SetTrieValue(g_trie, "Smoke", 5000); //Store the values &nbsp;&nbsp;&nbsp;&nbsp;SetTrieValue(g_trie, "Flash", 6000); &nbsp;&nbsp;&nbsp;&nbsp;SetTrieValue(g_trie, "Godmode", 8000); } public OnPluginEnd() { &nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(g_trie);//Make sure to close the trie. } MyRandomFunction() { //Grab the Smoke value. &nbsp;&nbsp;&nbsp;&nbsp;new value; &nbsp;&nbsp;&nbsp;&nbsp;if(GetTrieValue(g_trie, "Smoke", value)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;PrintToServer("The value for smoke is %i", value); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;PrintToServer("Failed to get the value for smoke from the trie"); &nbsp;&nbsp;&nbsp;&nbsp;} }

Thanks you for your exponation

:bacon!:

Bacardi 05-09-2012 12:47

Re: Array Question
 
Quote:

Originally Posted by berni (Post 1705144)
Either create a string array and a cell array...

example, if I understand what berni means cell array :3
PHP Code:

// String array
new String:g_key[][] =
{
    
"Smoke",
    
"Flash",
    
"Godmode"
}

// Cell array
new g_value[] =
{
    
5000,
    
6000,
    
8000
}

public 
OnPluginStart()
{
    
RegConsoleCmd("sm_test"cmd_callback"Test array by give number between 0 - 2");
}

public 
Action:cmd_callback(clientargs)
{
    if(
args 1// Typed only cmd
    
{
        
ReplyToCommand(client"Array g_key size %i,  g_value size %i"sizeof(g_key), sizeof(g_value));
        return 
Plugin_Handled;
    }

    new 
number;
    new 
String:arg[3];
    
GetCmdArg(1argsizeof(arg));
    
number StringToInt(arg);

    if(
number > -&& number sizeof(g_key) && number sizeof(g_value)) // Make sure not get error - [SM] Plugin encountered error 15: Array index is out of bounds
    
{
        
ReplyToCommand(client"key %s value %i"g_key[number], g_value[number]);
    }
    else
    {
        
ReplyToCommand(client"Invalid number. Array index is out of bounds");
    }
    return 
Plugin_Handled;



berni 05-09-2012 15:13

Re: Array Question
 
Quote:

Originally Posted by Bacardi (Post 1705674)
example, if I understand what berni means cell array :3
PHP Code:

// String array
new String:g_key[][] =
{
    
"Smoke",
    
"Flash",
    
"Godmode"
}

// Cell array
new g_value[] =
{
    
5000,
    
6000,
    
8000



yep, that's what I meant.


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

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