Raised This Month: $51 Target: $400
 12% 

Writing Include Files (.inc)


Post New Thread Reply   
 
Thread Tools Display Modes
psychonic

BAFFLED
Join Date: May 2008
Old 01-29-2015 , 07:49   Re: Writing Include Files (.inc)
Reply With Quote #11

Quote:
Originally Posted by Black-Rabbit View Post
You don't have a choice, you have to create native to share variables values, you can't share variables. Create get/set natives is pretty much the solution. Except if you do like events scripts and variables = console variables which I would not recommend.
That's partially true, but with SM 1.7, you could use some abuse some of the new syntactical sugar to make it look like sharing variables in the calling plugin. Something like:

Code:
// include

methodmap MyPluginIFace
{
    property SomeVariable {
        public native get();
        public native set(int value);
    }
}

native MyPluginIFace MyPlugin_GetIFace();

// MyPluginIFace iface = MyPlugin_GetIFace();
// iface.SomeVariable = 3;
// PrintToServer("Value is %d", iface.SomeVariable);


// plugin

CreateNative("MyPlugin_GetIFace", GetIFace);
CreateNative("MyPluginIFace.SomeVariable.get", GetSomeVariable);
CreateNative("MyPluginIFace.SomeVariable.set", SetSomeVariable);

int g_SomeVariable;

public int GetIFace(Handle plugin, int numParams)
{
    // dummy var
    return 1;
}

public int GetSomeVariable(Handle plugin, int numParams)
{
    return g_SomeVariable;
}

public int SetSomeVariable(Handle plugin, int numParams)
{
    // param one is the dummy |this| var
    g_SomeVariable = GetNativeCell(2);
    return 0;
}
psychonic is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-29-2015 , 17:11   Re: Writing Include Files (.inc)
Reply With Quote #12

Quote:
Originally Posted by Black-Rabbit View Post
You don't have a choice, you have to create native to share variables values, you can't share variables. Create get/set natives is pretty much the solution. Except if you do like events scripts and variables = console variables which I would not recommend.
GET/SET natives sounds good enough and simple enough.

Quote:
Originally Posted by ddhoward View Post
Be sure to post your work here if it's not a private plugin (or even just post the relevant part if it's private.) There are a few little easy mistakes that can cause major bugs that might not be noticed until AFTER plugin release.
My work so far.

The sp file itself is not finished because the wiki said to write the inc file first prior to the sp file. That being said, I hope that the .inc file is syntactically correct? (I know the sp file itself is wholly wrong... going to work on fixing that up right now).

This is a private plugin, so a majority of the code has been removed from the sp file. Still, I hopefully left enough of it in so that a clueless soul like myself who wants to write .inc files can follow along. I'll post a more-useful sp file later.

(Major thanks to FlaminSarge for this tf2attributes.inc and tf2attributes.sp since that's what I used as a reference to write the .inc file.)
Attached Files
File Type: inc private.inc (3.6 KB, 53 views)
File Type: sp Get Plugin or Get Source (private.sp - 125 views - 2.6 KB)
Potato Uno is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-30-2015 , 13:30   Re: Writing Include Files (.inc)
Reply With Quote #13

How do you write a native to return handles without causing leaks?

For example, here's a native:

PHP Code:
public Native_Func(Handle:pluginnumParams)
{
    new 
Handle:Result CreateArray(10);
    return 
Result

And in the plugin I call

PHP Code:
new Handle:Something Func(); 
Now I have 2 references to the adt_array handle? If I call CloseHandle(Something), that would only kill the new reference I made in the plugin scope. How can I properly close the handle?
Potato Uno is offline
psychonic

BAFFLED
Join Date: May 2008
Old 01-30-2015 , 13:35   Re: Writing Include Files (.inc)
Reply With Quote #14

Quote:
Originally Posted by Potato Uno View Post
How do you write a native to return handles without causing leaks?

For example, here's a native:

PHP Code:
public Native_Func(Handle:pluginnumParams)
{
    new 
Handle:Result CreateArray(10);
    return 
Result

And in the plugin I call

PHP Code:
new Handle:Something Func(); 
Now I have 2 references to the adt_array handle? If I call CloseHandle(Something), that would only kill the new reference I made in the plugin scope. How can I properly close the handle?
PHP Code:
public Native_Func(Handle:pluginnumParams)
{
    new 
Handle:mine CreateArray(10);
    new 
Handle:theirs CloneHandle(mineplugin);
    
CloseHandle(mine);
    return 
theirs;

And in your function, note that they will need to close the handle when finished with it, just as if they were to call CreateArray instead of Func.
psychonic is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-30-2015 , 13:44   Re: Writing Include Files (.inc)
Reply With Quote #15

Quote:
Originally Posted by Potato Uno View Post
How do you write a native to return handles without causing leaks?

For example, here's a native:

PHP Code:
public Native_Func(Handle:pluginnumParams)
{
    new 
Handle:Result CreateArray(10);
    return 
Result

And in the plugin I call

PHP Code:
new Handle:Something Func(); 
Now I have 2 references to the adt_array handle? If I call CloseHandle(Something), that would only kill the new reference I made in the plugin scope. How can I properly close the handle?
The Handle system was really written to be able to pass things back from an extension to the plugin that asked for it to be created in the first place. There are other consequences for this as well...

Handles have an associated HandleSecurity struct on the C++ side. By default, a Handle security for HandleAccess_Delete is "HANDLE_RESTRICT_OWNER". That is, the plugin that asked for the Handle. HandleAccess_Delete controls what plugins can call CloseHandle on a Handle.

I got around this in one of my plugins (NativeVotes) by having a native that would close the Handle and also deal with any embedded Handles it had (i.e. you have other Handles stored in an adt_array or KeyValues).
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 01-30-2015 at 13:45. Reason: struct not object
Powerlord is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-31-2015 , 14:52   Re: Writing Include Files (.inc)
Reply With Quote #16

So far the natives seem to be working fine... will ask any questions if any issues pop up. Thanks for all the help!

It was just annoying to write the natives as it seemed unnecessarily convoluted to grab the parameters, and when dealing with string buffers, trying to assign the newly made buffer back to the passed in buffer (SetNativeString). The wiki, however, listed enough information to get the job done.
Potato Uno is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 01-31-2015 , 17:37   Re: Writing Include Files (.inc)
Reply With Quote #17

https://wiki.alliedmods.net/Optional...Mod_Scripting)

If you want to make a plugin that uses those natives but can function without them, you should read this page. Otherwise the plugin that uses the .inc will NOT load unless private.smx is also loaded.
__________________

Last edited by ddhoward; 01-31-2015 at 17:38.
ddhoward is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 02-01-2015 , 20:59   Re: Writing Include Files (.inc)
Reply With Quote #18

Quote:
Originally Posted by ddhoward View Post
https://wiki.alliedmods.net/Optional...Mod_Scripting)

If you want to make a plugin that uses those natives but can function without them, you should read this page. Otherwise the plugin that uses the .inc will NOT load unless private.smx is also loaded.
Is it possible to force sourcemod to load the main plugin last?

That is, I want it to always load the dependency plugin first, and then the main plugin after that. Or is this already the default behavior?

(The natives are mandatory.)
Potato Uno is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-01-2015 , 21:55   Re: Writing Include Files (.inc)
Reply With Quote #19

Quote:
Originally Posted by Potato Uno View Post
Is it possible to force sourcemod to load the main plugin last?

That is, I want it to always load the dependency plugin first, and then the main plugin after that. Or is this already the default behavior?

(The natives are mandatory.)
To my knowledge, a plugin requirement will make the required plugin load first. Which obviously isn't what you wanted.

However, OnAllPluginsLoaded fires after all plugins in the plugins directory are loaded.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 02-01-2015 , 22:47   Re: Writing Include Files (.inc)
Reply With Quote #20

Quote:
Originally Posted by Powerlord View Post
To my knowledge, a plugin requirement will make the required plugin load first. Which obviously isn't what you wanted.

However, OnAllPluginsLoaded fires after all plugins in the plugins directory are loaded.
Yes that forward seems to be what I'm looking for. Thank you Ross!
Potato Uno 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 15:24.


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