Raised This Month: $12 Target: $400
 3% 

[SOLVED] Array Question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Razmo51
Senior Member
Join Date: Aug 2011
Location: Event "player_death
Old 05-08-2012 , 15:54   [SOLVED] Array Question
Reply With Quote #1

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
__________________
Sorry for my bad English, I'm Belgian

Last edited by Razmo51; 05-09-2012 at 10:37.
Razmo51 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-08-2012 , 16:36   Re: Array Question
Reply With Quote #2

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.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 05-08-2012 at 16:37.
Powerlord is offline
Razmo51
Senior Member
Join Date: Aug 2011
Location: Event "player_death
Old 05-08-2012 , 17:26   Re: Array Question
Reply With Quote #3

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 ?
__________________
Sorry for my bad English, I'm Belgian
Razmo51 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 05-08-2012 , 18:35   Re: Array Question
Reply With Quote #4

Either create a string array and a cell array or use tries
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Razmo51
Senior Member
Join Date: Aug 2011
Location: Event "player_death
Old 05-09-2012 , 08:41   Re: Array Question
Reply With Quote #5

Quote:
Originally Posted by berni View Post
Either create a string array and a cell array
and how to do that ?

sorry, but I'm a noob in array
__________________
Sorry for my bad English, I'm Belgian
Razmo51 is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 05-09-2012 , 09:56   Re: Array Question
Reply With Quote #6

Quote:
Originally Posted by Razmo51 View Post
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");     } }
Dr!fter is offline
Razmo51
Senior Member
Join Date: Aug 2011
Location: Event "player_death
Old 05-09-2012 , 10:36   Re: Array Question
Reply With Quote #7

Quote:
Originally Posted by Dr!fter View Post
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

__________________
Sorry for my bad English, I'm Belgian
Razmo51 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 05-09-2012 , 12:47   Re: Array Question
Reply With Quote #8

Quote:
Originally Posted by berni View Post
Either create a string array and a cell array...
example, if I understand what berni means cell array
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;

Bacardi is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 05-09-2012 , 15:13   Re: Array Question
Reply With Quote #9

Quote:
Originally Posted by Bacardi View Post
example, if I understand what berni means cell array
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.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Reply


Thread Tools
Display Modes

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 15:11.


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