AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Array VS Multiple Array (https://forums.alliedmods.net/showthread.php?t=333065)

ConorCC 06-17-2021 15:22

Array VS Multiple Array
 
Hello there!

I was thinking about why ZP50's dev team using multiple arrays to store zombie class data (name, desc, HP etc.) Wouldn't be a better to store them only in one with nested arrays? I think they did this for reason but I don't know.

If I am right and I use the array in array method which one is better to retreive data with netives?
1. Using multiple natives for retreive name and desc. OR
2. Using only one native that retreive the complett object. For example a zombie class's name and desc.

Ofc I don't want to modify their code. I only want to implement mine as bast I can.
Thanks for your advices.

Celena Luna 06-17-2021 22:03

Re: Array VS Multiple Array
 
I think there is ArrayPushArray and ArrayGetArray act like nested arrays for AMXX. (I have never messing around with it though)
Something like this

PHP Code:

enum _:wpn_data
{
    
W_Name[50],
    
W_Type,
    
W_Cost
}

new Array:
g_Weapon
new g_WeaponCount

public plugin_precache()
{
    
g_Weapon ArrayCreate(wpn_data)
}

public 
RegisterWeapon(const Name[], typecost)
{
    new 
g_Data[wpn_data]

    
copy(g_Data[W_Name], charsmax(g_Data), Name)
    
g_Data[W_Type] = type;
    
g_Data[W_Cost] = cost

    ArrayPushArray
(g_Weapong_Data//Push it all into g_Weapon (so it is only 1 Array)

    
g_WeaponCount++
    return 
g_WeaponCont //Give out WeaponID
}

public 
Native_Get_Weapon_Info(weaponid, const info[])
{
    new 
g_Data[wpn_data]

    
ArrayGetArray(g_Weaponweaponidg_Data)
    
    
//Now we got all data we need in g_Data
    //I forget how to push it to Info parameter to retrieve it other plugins :P


But usually, I would prefer only getting what I need (for example: classname) via 1 Array since I am not sure how costly it is if getting a lot of it when getting all of class data at once and then never use the remaining data.

But hey, I haven't use it before so I might being bs here

ConorCC 06-18-2021 03:26

Re: Array VS Multiple Array
 
When I said nested arrays I meant exactly your example. Sorry for misunderstanding. Anyways this is totaly fine. Currently I'm using this with the 2nd point. So every time when I need the name as well as the description I have to call natives twice like:

static name[32];
get_my_item_name(item_id, name, charsmax(name));

static desc[32];
get_my_item_desc(item_id, desc, charsmax(desc));

BUT this one seems mutch better.

static name[32], desc[32];
get_my_item(item_id, name, charsmax(name), desc, charsmax(desc));

Even if I don't need them bot I can use something like this. Right?

static desc[32];
get_my_item(item_id, _, _, desc, charsmax(desc));

In this case what do you think? Is it better than their implementation?

Natsheh 06-18-2021 08:15

Re: Array VS Multiple Array
 
The second one
Since its 1 native call.

ConorCC 06-18-2021 08:57

Re: Array VS Multiple Array
 
All right. Thanks! And what do you think? Why did they use the first one?

CrazY. 06-18-2021 09:07

Re: Array VS Multiple Array
 
You could also do something similar to engine entity_get_* and fakemeta pev, it does not really matter, choose what you are comfortable with.

Code:
enum {     zombie_health,     zombie_name,     zombie_desc,     ... } native get_zombie_class_int(index, var/key/value) native Float:get_zombie_class_float(index, var/key/value) native get_zombie_class_string(index, var/key/value, out[], len) or just native get_zombie_class_opt/prop/var/key/value(index, opt/prop/var/key/value, any:...) whatever you prefer

As for the first question, I do also use the same method as celena. Honestly it does not matter either.

AnimalMonster 06-19-2021 07:20

Re: Array VS Multiple Array
 
I think that they think it would be easier than creating array in array + wouldn't it be the same thing even if you creat them manually or add them to a existent array?

HamletEagle 06-19-2021 14:28

Re: Array VS Multiple Array
 
I agree that it doesn't matter. However, I want to point out a situation in which it having the data together vs splitting the data would matter.

If dynamic arrays were implemented under the hood using a structure like linked lists then it would make a difference. Accessing an index into an array, like array[i] is always an O(1) operation, meaning it doesn't matter if you access the first or the last index, it takes the same number of steps(or if it helps you better visualize this, "the same time", even if not technically correct).
In a linked list, to access the i-th index you have to loop thru all the other i-1 elements to get to the i-th element. This means that accessing the first element is only one step and accessing the last element takes a number of steps equal to the length of the list.

Let's say we split the data in 3 parts:

With arrays, we would have something like:
Code:

array1[i] <- one step
array2[i] <- one step
array3[i] <- one step
no matter which "i" we access, even if i = 1000000 we still perform one step

In total we are doing 3 steps, which isn't very different than doing one step.

With a list, assuming we have a long list and assuming we want to access the element with the index = 1 million

Code:

list1[1000000] <- 1000000 steps
list2[1000000] <- 1000000 steps
list3[1000000] <- 1000000 steps

In total, we get 3 million steps. On the other hand, if you had only one list you'd do only 1 million steps. 3 million steps vs 1 million steps is a pretty big difference.

Some of you may be quick to point out that asymptotically O(3*n) = O(n) and you'd be right, but for a large "n" the constant 3 actually matters and can cause a very noticeable slowdown. For small "n" it wouldn't make a difference.

You can code just fine without understanding the above explanation, especially because you don't work with huge amounts of data in plugins and we don't have list like structures, but I figured someone could find this useful.

ConorCC 06-21-2021 06:34

Re: Array VS Multiple Array
 
Posts by both of you are very interesting. Always good to learn new things. :3 Thank you!


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

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