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

Writing Include Files (.inc)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-28-2015 , 17:00   Writing Include Files (.inc)
Reply With Quote #1

So I searched the alliedmodders wiki on sourcemod code writing and haven't found much about this subject. Even the search bar is returning a lot of false positives here. Basically I have a plugin I wrote that is huge, and I originally intended to merge it with another plugin. However, since the source code is massive, I'd rather write an .inc file and use #include <filename> in the other plugin for the sake of cleanliness and maintenance.

I've seen some discussion of include files in the wiki, but it's mostly geared to how to read them, not how to write them.

Suppose I have the following as a plugin (this is a purposely contrived example)

PHP Code:
new Handle:MyArray INVALID_HANDLE;
new 
String:Derp[100];
new 
counter;

public 
OnPluginStart()
{
    
MyArray CreateArray();
    
Derp "Hello";
    
counter ++;
}

public 
FetchArray(index)
{
    
counter ++;
    return 
GetArrayCell(MyArrayindex);

How can I make the FetchArray() function, the MyArray handle, the counter variable, and the Derp string accessible to the other plugins?

In other words, how do I write the inc file such that I can make all those variables/functions accessible to other plugins?

If there's any article or some tutorial on this that I managed to not find, I'd gladly read that (and ask any further clarifying questions here).

Thank you!
Potato Uno is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 01-28-2015 , 17:03   Re: Writing Include Files (.inc)
Reply With Quote #2

What you looking for is creating native functions https://wiki.alliedmods.net/Creating...Mod_Scripting)

Last edited by Mathias.; 01-28-2015 at 17:05.
Mathias. is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 01-28-2015 , 17:03   Re: Writing Include Files (.inc)
Reply With Quote #3

The only way to do what you're looking for would be to use natives or forwards (depending on the case) to pass the information along through the separate plugins.

https://wiki.alliedmods.net/Creating...Mod_Scripting)
https://wiki.alliedmods.net/Function...ting)#Forwards

Last edited by bl4nk; 01-28-2015 at 17:04. Reason: slight correction
bl4nk is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-28-2015 , 17:37   Re: Writing Include Files (.inc)
Reply With Quote #4

Beautiful, I will read over those wiki pages. Thank you both!
Potato Uno is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-28-2015 , 21:14   Re: Writing Include Files (.inc)
Reply With Quote #5

So from reading those articles, functions can be made into natives, but there's no way of making variables visible in other plugins?

In other words, is there not a way to import handles, strings, and numeric variables from one plugin to another? Do I need to write additional natives that would, say, insert or retrieve data from the MyArray handle in the example above?

(Which doesn't sound right, since if you run #include <tf2> you get the TFClass_Scout, TFClass_Soldier, etc variables included within the namespace of your file.)
Potato Uno is offline
The1Speck
SourceMod Donor
Join Date: Oct 2014
Location: USA
Old 01-28-2015 , 21:22   Re: Writing Include Files (.inc)
Reply With Quote #6

Quote:
Originally Posted by Potato Uno View Post
So from reading those articles, functions can be made into natives, but there's no way of making variables visible in other plugins?

In other words, is there not a way to import handles, strings, and numeric variables from one plugin to another? Do I need to write additional natives that would, say, insert or retrieve data from the MyArray handle in the example above?

(Which doesn't sound right, since if you run #include <tf2> you get the TFClass_Scout, TFClass_Soldier, etc variables included within the namespace of your file.)
So like GET and SET functions? Only way to accomplish what you're asking if I'm understanding correctly.


PHP Code:
public GetHandle()
{
    return 
MyArray;
}

public 
SetCounter(value)
{
    
counter value;

__________________

Add me on Steam for personal inquiries or direct assistance.

Last edited by The1Speck; 01-28-2015 at 21:22.
The1Speck is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-28-2015 , 21:31   Re: Writing Include Files (.inc)
Reply With Quote #7

Quote:
Originally Posted by Potato Uno View Post
So from reading those articles, functions can be made into natives, but there's no way of making variables visible in other plugins?

In other words, is there not a way to import handles, strings, and numeric variables from one plugin to another? Do I need to write additional natives that would, say, insert or retrieve data from the MyArray handle in the example above?

(Which doesn't sound right, since if you run #include <tf2> you get the TFClass_Scout, TFClass_Soldier, etc variables included within the namespace of your file.)
When you have an #include line, it is replaced with the contents of the file during one of the preprocessor passes.

So... its OK to have an enum or constant in an include file as their values never change. On the flip side, putting a variable there ends up with each plugin having their own independent copy of the variable.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-28-2015 , 23:56   Re: Writing Include Files (.inc)
Reply With Quote #8

Quote:
Originally Posted by The1Speck View Post
So like GET and SET functions? Only way to accomplish what you're asking if I'm understanding correctly.
Yes that's what I was talking about. I see I'll have to write a few more natives to do exactly that.

Quote:
Originally Posted by Powerlord View Post
When you have an #include line, it is replaced with the contents of the file during one of the preprocessor passes.

So... its OK to have an enum or constant in an include file as their values never change. On the flip side, putting a variable there ends up with each plugin having their own independent copy of the variable.
Own independent copy of the variable... do not want.

I suppose I'll just write a bunch of additional natives that get/set values on variables in the other plugin (like TheSpeck was mentioning). Keeping multiple copies sounds like a lot of unwanted bookkeeping work (especially if handles are involved, which it is in my case).

Thanks for the responses! I'll give it an attempt tonight/tomorrow and see if it works out well.
Potato Uno is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 01-29-2015 , 00:49   Re: Writing Include Files (.inc)
Reply With Quote #9

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.
__________________

Last edited by ddhoward; 01-29-2015 at 00:49.
ddhoward is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 01-29-2015 , 03:37   Re: Writing Include Files (.inc)
Reply With Quote #10

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.

Last edited by Mathias.; 01-29-2015 at 03:38.
Mathias. 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 05:54.


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