AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   MarkNativeAsOptional - inconvenient moment. (https://forums.alliedmods.net/showthread.php?t=334702)

client21 10-14-2021 11:48

MarkNativeAsOptional - inconvenient moment.
 
z.inc

PHP Code:

#if defined _z_included
#endinput
#endif
#define _z_included

native int Test_z();

public 
SharedPlugin __pl_z 
{
    
name "z",
    
file "z.smx",
    
required 0
};

public 
void __pl_z_SetNTVOptional()
{
    
MarkNativeAsOptional("Test_z");


z.sp

PHP Code:

#include <z>

public void OnClientPostAdminCheck(int client)
{
    
bool b false;
    if (
b) {
        
Test_z();
    }


sm plugins load z.smx
[SM] Plugin z.smx failed to load: Native "Test_z" was not found.

If I rename z.smx to zz.smx (or other) then it works.

if I do:

PHP Code:

public APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
MarkNativeAsOptional("Test_z");
    return 
APLRes_Success;


in z.smx then it works.

Why public void __pl_z_SetNTVOptional() doesn't work for z.smx :cry:
Why am I forced to do AskPluginLoad2->MarkNativeAsOptional in z.smx :cry:

-----------------
It will work if in z.smx I do:

PHP Code:

public APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
CreateNative("Test_z"Test_z_);
    return 
APLRes_Success;
}

public 
int Test_z_(Handle pluginint args) {
    return 
0;


But my goal is to create native "Test_z" with a delay.
Apparently __pl_z_SetNTVOptional doesn't work because of the delay and I have to do MarkNativeAsOptional.
Anyway, I expected more from __pl_z_SetNTVOptional.

Fortis 10-14-2021 22:38

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by client21 (Post 2760582)
But my goal is to create native "Test_z" with a delay.

I'm confused by what you mean here, you create natives in AskPluginLoad2 to ensure all other plugins on your server can use the native as long as they have the include file. You can mark natives as optional in the include file so if you unload the plugin you created the native in, the other plugins using it can still run and won't fail to load.

Example:

z.inc
PHP Code:

#if defined _z_included
#endinput
#endif
#define _z_included

native int Test_Z();

public 
SharedPlugin __pl_z 
{
    
name "z",
    
file "z.smx",
#if defined REQUIRE_PLUGIN
    
required 1,
#else
    
required 0,
#endif
};

#if !defined REQUIRE_PLUGIN
public void __pl_z_SetNTVOptional()
{
    
MarkNativeAsOptional("Test_Z");
}
#endif 

z.sp
PHP Code:

#include <z>

public APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
CreateNative("Test_Z"Native_Z);
    return 
APLRes_Success;
}

public 
int Native_Z(Handle pluginint numParams)
{
    return 
0;
}

public 
void OnClientPostAdminCheck(int client)
{
    
int z Test_Z();


Any other plugin now can use the Test_Z native as long as its included

zz.sp
PHP Code:

#include <z>

public void OnClientPutInServer(int client)
{
    
int z Test_Z();



client21 10-15-2021 13:24

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by Fortis (Post 2760620)
Example:

Thanks, but I know that. This way is not suitable for me because:

Quote:

Originally Posted by client21 (Post 2760582)
But my goal is to create native "Test_z" with a delay.

Don't ask me why I need it, just accept the fact that I need it :)

In any case, I will find the optimal solution, but I thought that __pl_z_SetNTVOptional was not working correctly and created this thread. Perhaps I should have written about this in section "Issues" (github).

headline 10-15-2021 19:49

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by client21 (Post 2760703)
Thanks, but I know that. This way is not suitable for me because:
Don't ask me why I need it, just accept the fact that I need it :)

AskPluginLoad2 is the earliest you may create a it such that all other plugins are able to use the native.

Fortis was kind enough to show you the right way to register a native (if you're still unsure review the wiki).

Quote:

Originally Posted by client21 (Post 2760703)
Perhaps I should have written about this in section "Issues" (github).

The first question we'd ask you in the issue is "why".

client21 10-15-2021 20:33

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by headline (Post 2760721)
Fortis was kind enough

Yes, you are all very kind, but for some reason you do not answer my main question. I've provided a specific example code and I'm wondering why __pl_z_SetNTVOptional->MarkNativeAsOptional doesn't work for z.smx. Instead of answering this, you teach me to create natives, although I can do it.

asherkin 10-16-2021 10:34

Re: MarkNativeAsOptional - inconvenient moment.
 
A plugin doesn't run its own SetNTVOptional implementation, it doesn't make sense to - natives should always exist for the lifetime of a plugin.

client21 10-16-2021 11:57

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by asherkin (Post 2760774)
A plugin doesn't run its own SetNTVOptional implementation, it doesn't make sense to - natives should always exist for the lifetime of a plugin.

This is done on purpose. Understood, thanks :3

Fortis 10-16-2021 12:48

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by client21 (Post 2760703)
Don't ask me why I need it, just accept the fact that I need it :)

We're asking why because if you don't create the native in AskPluginLoad2 then to my knowledge no other plugin can use it besides the one it was created in. (Read the Wiki Under Registering Natives) At that point why create a Native if only one plugin is gonna use it? If you're ultimately set on creating a native with a delay, just use a timer, just note only this plugin can use it.

PHP Code:

#include <z>

public void OnPluginStart()
{
    
CreateTimer(2.0Timer_Natives);
}

public 
Action Timer_Natives(Handle timer)
{
    
CreateNative("Test_Z"Native_Z);
    return 
Plugin_Handled;



client21 10-16-2021 16:10

Re: MarkNativeAsOptional - inconvenient moment.
 
Quote:

Originally Posted by Fortis (Post 2760792)
just note only this plugin can use it.

PHP Code:

#include <z>

public void OnPluginStart()
{
    
CreateTimer(2.0Timer_Natives);
}

public 
Action Timer_Natives(Handle timer)
{
    
CreateNative("Test_Z"Native_Z);
    return 
Plugin_Handled;



Why are you lying?) If another plugin is loaded later or there is the necessary communication between them (for example, using events, or GetFeatureStatus->FeatureType_Native), then this will work. The topic does not need to be continued, the answer was received. It's okay that you couldn't help me :)

----------
I think I understand what you mean.
If the native is created using a timer (even 0.1), then other plugins get an error: "[SM] Exception reported: Native is not bound".
But still works in "OnPluginStart" or if the plugin was loaded after the native-creator plugin.
Thx.


All times are GMT -4. The time now is 10:58.

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