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

Need a good array for this...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bally
Senior Member
Join Date: Aug 2015
Old 12-20-2015 , 19:41   Need a good array for this...
Reply With Quote #1

Code:
"Locations"
{
	"Arena"
  	{
       		"ammo" 		"1"
       		"ctlocx" 	"535"
        	"ctlocy"	 "5290"
        	"ctlocz" 	"1924"
        	"tlocx" 	"4467"
        	"tlocy" 	"45361"
        	"tlocz" 	"375"
    	}
	"TradeRooms"
  	{
       		"ammo" 		"0"
       		"ctlocx" 	"535"
        	"ctlocy"	 "5290"
        	"ctlocz" 	"1924"
        	"tlocx" 	"4467"
        	"tlocy" 	"45361"
        	"tlocz" 	"375"
    	}
	"Surf"
  	{
       		"ammo" 		"0"
       		"ctlocx" 	"535"
        	"ctlocy"	 "5290"
        	"ctlocz" 	"1924"
        	"tlocx" 	"4467"
        	"tlocy" 	"45361"
        	"tlocz" 	"375"
    	}
	"Bhop"
  	{
       		"ammo" 		"0"
       		"ctlocx" 	"535"
        	"ctlocy"	 "5290"
        	"ctlocz" 	"1924"
        	"tlocx" 	"4467"
        	"tlocy" 	"45361"
        	"tlocz" 	"375"
    	}
}
Q.1 - I have this KeyValue Config which I'll need to use it later in my plugin. I need to store every section data in an adt_array in such form as it can be used later given the section name... How can I do it? I was thinking something like:
PHP Code:
tpos_adtArray CreateArray(32)(3)(1); 
However, I'm not sure.

(32) = section Name
(3) = pos
(1) = 1/0

Q.2 - After creating the multi-dimensional array what would be the best form to either add/use data from it?

Q.3 - Should I keep the keyvalues Handle opened, so I don't need to over-stress server?

Thank you very much for reading my questions. Have a great time.
__________________
Perhaps my lack of faith was my undoing,
bally is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 12-20-2015 , 20:33   Re: Need a good array for this...
Reply With Quote #2

Your declaration of CreateArray wouldn't work since the declaration's first argument determines the number of values it can hold in one array entry (and functions don't allow for arguments after the closing parenthesis). The size of the adt_array is dynamic and the initial size is the second argument. I wouldn't recommend using it for your particular use case.

You could do nested adt_trie so you could just read keys out, but you'd still have to deal with iterating through the KeyValues to put them into each trie.

Out of curiosity, why not just use the KeyValues directly? Since you know exactly what keys you're working with, you can use forward slashes to read a value from a subkey.

Assuming transitional syntax:
Code:
KeyValues kv = new KeyValues("Locations");
kv.ImportFromString(keyvalue_data_from_post);

// Read tlocx from Surf section
int tlocX = kv.GetNum("Surf/tlocx");
(Also, assuming the configuration file uses your own schema, you can also use kv.GetVector to read a float[3] instead of storing the individual (x, y, z) values.)
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 12-20-2015 at 20:47. Reason: updated reasoning for CreateArray
nosoop is offline
bally
Senior Member
Join Date: Aug 2015
Old 12-20-2015 , 21:31   Re: Need a good array for this...
Reply With Quote #3

Hi nosoop, thanks for replying me this fast, I did not understand what you meant with "adt_trie", would it be possible to add an example in? Also, I want to make this project extremely flexible and dynamic, therefore I can't just do what you told me.

The project objective is to build up a menu which it's contents will be the keyvalues section name... Done
Will retrieve data from the keyvalues and cache it
Will use this cached data when user inputs on the menu according to the specified option..

The dirty way would be accessing the keyvalues file everytime a user inputted data, however, I don't want it like that, so I need an array that could support all those 3 data ( name, ammo ? 1:0, pos[3] ) this way it wouldn't stress the server

Sorry for my bad english, looking forward for a reply.
Thanks.
__________________
Perhaps my lack of faith was my undoing,

Last edited by bally; 12-20-2015 at 21:32.
bally is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-20-2015 , 21:36   Re: Need a good array for this...
Reply With Quote #4

The KV file is already "cached" in memory, just don't continually close/re-open it.
__________________
asherkin is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 12-20-2015 , 21:57   Re: Need a good array for this...
Reply With Quote #5

Depending on what you're doing with the data, and how often, and how big the dataset is, you can put it into an adt-array or a trie.
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
bally
Senior Member
Join Date: Aug 2015
Old 12-20-2015 , 22:38   Re: Need a good array for this...
Reply With Quote #6

Dr greg, can you show me an example with the adt_array?
__________________
Perhaps my lack of faith was my undoing,
bally is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 12-20-2015 , 23:29   Re: Need a good array for this...
Reply With Quote #7

Are you sure you don't want to use the KeyValues? I'd say it's a good fit for what you're trying to do, provided it's accessible across your plugin (i.e., you read it once into a global handle and just call its getters / setters; if you want to write out user input, figure out what you need to change and write that specific stuff back out). As far as I can see, there's nothing dirty about accessing KeyValues that have already been read from a file (as long as you don't read from disk every time you just need to KvGetNum()).

Treat it as just another data structure like adt_* instead of just a (de)serialization format and you'll be fine.

But anyways, as requested, here's an example for nested adt_trie in 1.7:

Code:
StringMap config = CreateTrie();

// note:  I'm not 100% sure of how to even do iterative lookups, just basing this off of the wiki
if (!kv.GotoFirstSubKey()) {
    // your config is broked
}

do {
    char section[256];
    kv.GetSectionName(section, sizeof(section));
    
    StringMap subtrie = new StringMap();
    
    int tloc[3];
    tloc[0] = kv.GetNum("tlocx");
    /* repeat for all your position values */
    subtrie.SetArray("tloc", tloc, sizeof(tloc));

    /* same thing with the rest of your values */

    config.SetValue(section, subtrie);
} while (kv.GotoNextKey());

// Read TradeRooms / tloc
StringMap tradeRooms;
if (config.GetValue("TradeRooms", tradeRooms)) {
    int tloc[3];
    tradeRooms.GetArray("tloc", tloc, sizeof(tloc));
}

// I have no idea how to write this stuff back out to KV, but it will involve iterating over the keys again after creating a StringMapSnapshot / calling CreateTrieSnapshot
For adt_array, I'd expect that you just do CreateArray(7); and pack the seven values into one array and store the position of that array in a way that you can retrieve it (either using another adt_array with arrayList.FindString(section) or an adt_trie with trie.GetValue(section)).
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 12-21-2015 at 00:07.
nosoop is offline
bally
Senior Member
Join Date: Aug 2015
Old 12-21-2015 , 08:43   Re: Need a good array for this...
Reply With Quote #8

Quote:
Originally Posted by asherkin View Post
The KV file is already "cached" in memory, just don't continually close/re-open it.
So basically, what you're saying is, there's no need to store the values on a dynamic array, I can just access the keyvalues because they are already cached?
__________________
Perhaps my lack of faith was my undoing,
bally is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-21-2015 , 09:26   Re: Need a good array for this...
Reply With Quote #9

Quote:
Originally Posted by bally View Post
So basically, what you're saying is, there's no need to store the values on a dynamic array, I can just access the keyvalues because they are already cached?
Yes.
__________________
asherkin is offline
bally
Senior Member
Join Date: Aug 2015
Old 12-21-2015 , 09:36   Re: Need a good array for this...
Reply With Quote #10

Quote:
Originally Posted by asherkin View Post
Yes.
LOOL, you just saved me a bunch of time. cheers!
__________________
Perhaps my lack of faith was my undoing,
bally 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 17:08.


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