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

Sharing the array handle via natives


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 02-15-2014 , 18:31   Sharing the array handle via natives
Reply With Quote #1

Is there any way to simply share array handle through dynamic natives or do I have to copy the entire array? Something like this is possible with SQLx for example, where you can simply do

PHP Code:
public Handlenative_handleriPluginiParams )
{
    return 
g_iSqlHandle

However, if I do the same thing with a dynamic array, the calling plugin gets an invalid handle provided error.

PHP Code:
new Array: g_aArray

public plugin_init( )
{
   
g_aArray ArrayCreate( )
}

public Array: 
native_handleriPluginiParams )
{
   return 
g_aArray

__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 02-15-2014 , 19:11   Re: Sharing the array handle via natives
Reply With Quote #2

Code:
#include <amxmodx> #include <cellarray> new Array:g_hArray; public plugin_init() {     register_plugin("Test Plugin 1", "", "");         g_hArray = ArrayCreate(); } public plugin_natives() {     register_native("get_arrayhandle", "native_get_arrayhandle"); } public Array:native_get_arrayhandle() {     return g_hArray; }

Code:
#include <amxmodx> #include <cellarray> native Array:get_arrayhandle(); public plugin_init() {     register_plugin("Test Plugin 2", "", "");         set_task(1.0, "test"); } public test() {     new Array:hArray = get_arrayhandle();         ArrayPushCell(hArray, 666); }

Code:
#include <amxmodx> #include <cellarray> native Array:get_arrayhandle(); public plugin_init() {     register_plugin("Test Plugin 3", "", "");         set_task(2.0, "test"); } public test() {     new Array:hArray = get_arrayhandle();         server_print("Array test: %d", ArrayGetCell(hArray, 0)); }

Code:
Array test: 666
Problem?
__________________

Last edited by Black Rose; 02-15-2014 at 19:15.
Black Rose is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 02-15-2014 , 20:33   Re: Sharing the array handle via natives
Reply With Quote #3

Weird, I have it done exactly the same way and it just refuses to work. Might be something with the array not being initialized soon enough (it did say that the handle integer was 0, which I had found mysterious).

I'll look it over tomorrow again, thanks for the reply.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 02-15-2014 , 21:10   Re: Sharing the array handle via natives
Reply With Quote #4

Since hdls.exe is single threaded, if the plugins are in the correct order there should be no problem.
I tried again without set_task() delay and initializing the array on both plugin_natives and plugin_init. No problem.

Forwards are called in this order:
Code:
plugin_natives
plugin_precache
plugin_init
plugin_cfg
...
plugin_end
plugin_modules is deprecated and no longer called.

Since you can initialize the array on plugin_natives, just to be safe I suggest you do that.
That way, no matter the order of plugins.ini the array will always be initialized for any plugin using the native in plugin_init.
__________________

Last edited by Black Rose; 02-15-2014 at 21:16.
Black Rose is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-15-2014 , 22:16   Re: Sharing the array handle via natives
Reply With Quote #5

tl;dr

I think you should be able to use xvars would eliminate natives.
__________________
fysiks is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 02-16-2014 , 09:03   Re: Sharing the array handle via natives
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
tl;dr

I think you should be able to use xvars would eliminate natives.
Not really, the result would probably be exactly the same. You'd be better off reading the thread next time.


Edit: Now that I think of it, I'd be better off to copy the array anyway, since I don't want other plugins to push additional data into it.


PHP Code:
public Array: _ac_get_achievementsPluginParams )
{
    if( 
DEBUG DEBUG_NATIVE_CALLS )
    {
        
log_amx"DEBUG_NATIVE_CALLS (_ac_get_achievements(%d,%d)): NATIVE CALLED"PluginParams )
    }
    
    new Array: 
iArrayHandle ArrayCreateAchievementData )
    

    new 
aAchievementAchievementData ]
    
    for( new 
iArraySizeg_aAchievements ); ++ )
    {
        
ArrayGetArrayg_aAchievementsiaAchievement )
        
        
ArrayPushArrayiArrayHandleaAchievement )
    }
    return 
iArrayHandle

I don't like the solution, but it works fine.
__________________
Currently busy working on a very large scale anime database project.

Last edited by Backstabnoob; 02-16-2014 at 09:24.
Backstabnoob is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-16-2014 , 09:30   Re: Sharing the array handle via natives
Reply With Quote #7

To share an integer, xvar would be simple and appropriate to use.
__________________
Arkshine is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 02-16-2014 , 09:59   Re: Sharing the array handle via natives
Reply With Quote #8

I'd rather have everything in one place and more understandable than a couple percent increased performance in one call.

It's aimed at complete newbies too and additional steps, such as caching the XVar's ID might be confusing for the end user.


About simplicity,
PHP Code:
new Array: Achievements ac_get_achievements( ) 
seems simple enough to me.


My point about xvars in this thread was that fysiks suggested me to use them without actually reading the thread, since his response doesn't seem any relevant to this thread's purpose.
__________________
Currently busy working on a very large scale anime database project.

Last edited by Backstabnoob; 02-16-2014 at 10:01.
Backstabnoob is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-16-2014 , 14:39   Re: Sharing the array handle via natives
Reply With Quote #9

Quote:
Originally Posted by Backstabnoob View Post
Not really, the result would probably be exactly the same. You'd be better off reading the thread next time.
I read the original post just fine. I suggested a valid solution. I didn't say it would eliminate ALL natives but it would eliminate all natives created to solve this problem.

Quote:
Originally Posted by Backstabnoob View Post
It's aimed at complete newbies too and additional steps, such as caching the XVar's ID might be confusing for the end user.
The end user should not ever need to touch the code. Also, it's not a hard concept and "newbies" probably can't understand half of the other stuff going on. If they can, they can easily learn this.
__________________
fysiks is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 02-16-2014 , 16:25   Re: Sharing the array handle via natives
Reply With Quote #10

I just don't see how using an xvar would solve this, both the native and an xvar would return the same integer. The only difference is that xvar is a little bit more complex to use than standard natives, where you literally need one line to get exactly where you want to.

It is aimed for people who are willing to make their own achievements. The API, while being really large, is also extremely easy to use and that's what I want to keep. I don't really need 30 natives and one xvar, I'd rather have 31 natives.

I'm not saying xvars are more efficient or super hard to use compared to natives, but the majority of people don't even know such a thing exists and would rather use what they've been using for the last couple of years.

All in all, what I have works just fine and also protects the main array from being edited from within other sub plugins, which is what I was looking for. Unless there's anything I can do to improve the code I have right now - which is copying the entire array in order to prevent the sub-plugins from directly editing the main array, I think that further discussion is unnecessary.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob 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 06:27.


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