Raised This Month: $32 Target: $400
 8% 

MethodMaps


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
masamesa
Junior Member
Join Date: Nov 2020
Old 11-27-2020 , 13:05   MethodMaps
Reply With Quote #1

Hi,

I'm a little confused on methodmaps in the sense of trying to use arrays in them.

For example, I have a methodmap named PlayerInfo, In this player info I'd like it to contain a string array and an int array. However, I'm finding zero results for how to do something like this.

I was told previously to use an enum struct when I was looking for something similar to a tuple. If it's possible to use one inside of a methodmap, I'd greatly appreciate it. https://forums.alliedmods.net/showpo...27&postcount=2

To clarify in object terms I'm looking for something like either one of these:

example 1:

Code:
{
    stringArray: {
                  'test1',
                  'test2
                 },
   intArray: {
               1,
               2
             }
}
or

example 2:

Code:
{
   enumStruct:{
                 {'test1', 1},
                 {'test2', 2}
              }
}
Thanks for any help!

Last edited by masamesa; 11-27-2020 at 13:07.
masamesa is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 11-27-2020 , 15:10   Re: MethodMaps
Reply With Quote #2

You can't have storage with methodmaps but you can use enum structs.

Example 1:
Code:
enum struct PlayerInfo {
    char stringArray[5][256]; // 5 - number of strings (array size), 256 - max length of each string
    int intArray[5]; // 5 - array size
}
or you can use dynamic arrays like StringMap or ArrayList.


Example 2:
Code:
enum struct PlayerInfo {
    StringMap stringArray; // contains key:value pairs
}
If you want to store only integers, StringMap has two methods: GetValue and SetValue
Code:
StringMap hMap = new StringMap();
hMap.SetValue( "test1", 5 ); // store number 5 for key "test1"

int iValue;
hMap.GetValue( "test1", iValue ); // get value from key "test1" and store it into iValue
hMap.Close(); // you should close/free these (and most of Handle-based types) after you've done with them
__________________

Last edited by MAGNAT2645; 11-27-2020 at 15:13.
MAGNAT2645 is offline
masamesa
Junior Member
Join Date: Nov 2020
Old 11-27-2020 , 21:35   Re: MethodMaps
Reply With Quote #3

Quote:
Originally Posted by MAGNAT2645 View Post
You can't have storage with methodmaps but you can use enum structs.

Example 1:
Code:
enum struct PlayerInfo {
    char stringArray[5][256]; // 5 - number of strings (array size), 256 - max length of each string
    int intArray[5]; // 5 - array size
}
or you can use dynamic arrays like StringMap or ArrayList.


Example 2:
Code:
enum struct PlayerInfo {
    StringMap stringArray; // contains key:value pairs
}
If you want to store only integers, StringMap has two methods: GetValue and SetValue
Code:
StringMap hMap = new StringMap();
hMap.SetValue( "test1", 5 ); // store number 5 for key "test1"

int iValue;
hMap.GetValue( "test1", iValue ); // get value from key "test1" and store it into iValue
hMap.Close(); // you should close/free these (and most of Handle-based types) after you've done with them
Thank you! I'm pretty certain that answered my question! Much appreciated

Last edited by masamesa; 11-27-2020 at 21:35.
masamesa is offline
masamesa
Junior Member
Join Date: Nov 2020
Old 12-07-2020 , 23:52   Re: MethodMaps
Reply With Quote #4

Hi, I'm at a loss again; the array lists aren't saving the data. I can only initialize them outside of the enum struct so they're never going to save the data in memory once pushed to an array list is what I'm finding out.

In essence I'm grabbing something from the database and pushing it into an array list in which gets pushed into an array list

My models are like this:

Code:
enum struct PlayerInfo {
       ArrayList items;
}

enum struct Items{
      char itemName[255];
      int itemId;
}

ArrayList list;
list = new ArrayList(1, 0);

PlayerInfo player;
Items items;

items.itemName = "test";
items.itemId = 1;


player.items.PushArray(items, sizeof(items))
Of course this isnt't 1:1 code, but it should help you understand. btw this is in sourcepawn; this is the only forum I can find that does a language close to it.

Last edited by masamesa; 12-07-2020 at 23:56. Reason: i hit space and it submitted early.
masamesa is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 12-08-2020 , 03:52   Re: MethodMaps
Reply With Quote #5

Quote:
Originally Posted by masamesa View Post
Hi, I'm at a loss again; the array lists aren't saving the data. I can only initialize them outside of the enum struct so they're never going to save the data in memory once pushed to an array list is what I'm finding out.

In essence I'm grabbing something from the database and pushing it into an array list in which gets pushed into an array list

My models are like this:

Code:
enum struct PlayerInfo {
       ArrayList items;
}

enum struct Items{
      char itemName[255];
      int itemId;
}

ArrayList list;
list = new ArrayList(1, 0);

PlayerInfo player;
Items items;

items.itemName = "test";
items.itemId = 1;


player.items.PushArray(items, sizeof(items))
Of course this isnt't 1:1 code, but it should help you understand. btw this is in sourcepawn; this is the only forum I can find that does a language close to it.
First of all, you need to create ArrayList with blocksize equal to enum struct size.
Code:
PlayerInfo player;
player.items = new ArrayList( sizeof Items );

Items item;
item.itemName = "test 1";
item.itemId = 1;

player.items.PushArray( item ); // it is not necessary to specify array size because we did it when created our ArrayList

// also you can get/set specific value in array using offsets/blocks
player.items.Set( 0, 5, Items::itemId, false ); // change itemId from 1 to 5

// and we can get these specific values without reading whole array
int Id = player.items.Get( 0, Items::itemId, false ); // read itemId from first (zero index) array
__________________

Last edited by MAGNAT2645; 12-08-2020 at 06:59.
MAGNAT2645 is offline
masamesa
Junior Member
Join Date: Nov 2020
Old 12-08-2020 , 21:15   Re: MethodMaps
Reply With Quote #6

Quote:
Originally Posted by MAGNAT2645 View Post
First of all, you need to create ArrayList with blocksize equal to enum struct size.
Code:
PlayerInfo player;
player.items = new ArrayList( sizeof Items );

Items item;
item.itemName = "test 1";
item.itemId = 1;

player.items.PushArray( item ); // it is not necessary to specify array size because we did it when created our ArrayList

// also you can get/set specific value in array using offsets/blocks
player.items.Set( 0, 5, Items::itemId, false ); // change itemId from 1 to 5

// and we can get these specific values without reading whole array
int Id = player.items.Get( 0, Items::itemId, false ); // read itemId from first (zero index) array
Yeah, I did that last night but when I pushed the playerinfo to an arraylist, I can't fetch data from the arraylist Items inside of that playerinfo stored in the arraylist. this was the stack trace I got

Code:
L 12/07/2020 - 18:01:43: [SM] Exception reported: Invalid Handle 41565445 (error: 4)
L 12/07/2020 - 18:01:43: [SM] Blaming: test-plugin.smx
L 12/07/2020 - 18:01:43: [SM] Call stack trace:
L 12/07/2020 - 18:01:43: [SM]   [0] GetArrayCell
L 12/07/2020 - 18:01:43: [SM]   [1] Line 203
masamesa is offline
Reply



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 19:30.


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