Return arrays in ArrayLists by reference
Is it possible to retrieve an array pushed into an ArrayList by reference?
As hinted at in the documentation, ArrayList.GetArray() seems to make a copy of the array. Please excuse me if this is a stupid question or if I missed something. Thank you. |
Re: Return arrays in ArrayLists by reference
Use ArrayList.Get to retrieve arraylists from an arraylist. ArrayLists are handles, not arrays.
|
Re: Return arrays in ArrayLists by reference
You can't - the contents of an ArrayList are outside of a plugin's memory space; the only way a plugin is allowed to access it is through the provided natives, and none of them give you that kind of memory access.
ArrayList.Set lets you update specific entries in an array stored in an ArrayList; otherwise you will need to copy data to / from your plugin (as you've already discovered). What's your particular use case? Without further context this sounds like a preemptive attempt at optimization. |
Re: Return arrays in ArrayLists by reference
Thanks a lot to you two, that makes total sense.
I'm currently discovering the new enum structs (which is why I'm pushing regular arrays in the ArrayList instead of another ArrayList) and am trying to see what I can get away with by nesting them like a crazy person. Thus for example I have an ArrayList of Players (I realize that isn't as performant as storing a regular old array of these) which looks like this in it's most basic form: PHP Code:
|
Re: Return arrays in ArrayLists by reference
You can specify which index inside the arraylist entry you get or set using the ArrayList.Get or ArrayList.Set natives ' "block" parameter.
When creating the ArrayList, you set the blocksize to be the number of cells of your enum struct. PHP Code:
PHP Code:
But if we really want to use ArrayLists, you can use them like above. Of course you have to get the correct index into the ArrayList which belongs to the entry for your client first, but luckly ArrayList.FindValue has a block parameter too, so you can look for entries which have the client index at the e.g. 2nd block. This allows you to access enum struct members without grabbing the whole array - BUT the performance gain you're hoping for is gone once you need to get/set two members at once. Additionally you have to keep a local copy of the enum struct updated yourself. PHP Code:
|
Re: Return arrays in ArrayLists by reference
@Peace-Maker, blyatiful. Thanks for coding example
|
Re: Return arrays in ArrayLists by reference
First of, thanks a lot for taking time out of your day to explain this in detail @Peace-Maker. Awesome write-up and great example!
Quote:
One more follow-up question about my example... I'm writing up a basic minigame that (hypothetically) may have multiple instances running simultaneously with different clients on a server belonging to different games. Now I could still use a static Player players[MAXCLIENTS + 1] array per game, however as it is (understandably) not allowed to set multidimensional fields on an enum struct, I could not add the players as a field for e. g. a Game struct. Is there a workaround that doesn't defeat the purpose of the enum struct syntax, achieving something like this? PHP Code:
|
Re: Return arrays in ArrayLists by reference
Another thing... Is it possible to retrieve a method from an enum struct which is stored in an ArrayList and then execute it, without getting a full copy of the enum struct? Similar to how Peace-Maker explained retrieving a field on an enum struct stored inside of an ArrayList.
Somewhat pseudo-code: PHP Code:
PHP Code:
EDIT: I just realized that not making a copy of the enum struct before executing it's method would also allow the method to change the enum struct's fields directly. At least that is what I'm hoping for. Updated the examples to reflect what I'm trying to achieve. |
Re: Return arrays in ArrayLists by reference
Quote:
PHP Code:
Quote:
|
Re: Return arrays in ArrayLists by reference
Alright, thank you again for the elaborate answer. That about covers all my questions towards the enum struct syntax. It's pretty helpful but doesn't result in great code if overused, as it's just not all that flexible.
Also thinking about it, it is obvious that you can't access the functions via :: as they are not saved in that array. Would be nice to be able to call them from within the plugin, when they are in an ArrayList though. |
All times are GMT -4. The time now is 12:35. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.