AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Writing Include Files (.inc) (https://forums.alliedmods.net/showthread.php?t=256950)

Potato Uno 01-28-2015 17:00

Writing Include Files (.inc)
 
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!

Mathias. 01-28-2015 17:03

Re: Writing Include Files (.inc)
 
What you looking for is creating native functions https://wiki.alliedmods.net/Creating...Mod_Scripting)

bl4nk 01-28-2015 17:03

Re: Writing Include Files (.inc)
 
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

Potato Uno 01-28-2015 17:37

Re: Writing Include Files (.inc)
 
Beautiful, I will read over those wiki pages. Thank you both!

Potato Uno 01-28-2015 21:14

Re: Writing Include Files (.inc)
 
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.)

The1Speck 01-28-2015 21:22

Re: Writing Include Files (.inc)
 
Quote:

Originally Posted by Potato Uno (Post 2255653)
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;



Powerlord 01-28-2015 21:31

Re: Writing Include Files (.inc)
 
Quote:

Originally Posted by Potato Uno (Post 2255653)
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.

Potato Uno 01-28-2015 23:56

Re: Writing Include Files (.inc)
 
Quote:

Originally Posted by The1Speck (Post 2255656)
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 (Post 2255658)
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.

ddhoward 01-29-2015 00:49

Re: Writing Include Files (.inc)
 
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.

Mathias. 01-29-2015 03:37

Re: Writing Include Files (.inc)
 
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.


All times are GMT -4. The time now is 17:31.

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