Jump to content
Liven_28

Good modding practice : add a #define to the scripts to help other modders to fix conflicts.

Recommended Posts

What does a #define :

It allows to know if a mod is loaded or not.

 

How to add a #define :

Create a file like this /YourMod/Scripts/Common/define.c

and add this inside : #define THE_NAME_OF_YOUR_MOD

(you can set the name you want but it have to be different from the other mods)

And that’s all ! You see, very easy, very quick and trust me very useful !

 

How to use #define :

If you’re a modder and have a conflict with another mod, sometime you can add code to your mod to solve this conflict.

But doing this, it can add bugs for those using your mod but not the mod that conflict (when the fix need to refer to variables or classes included in the mod that conflict).

 

To solve this situation you can use this type of code :

#ifdef THE_DEFINE_OF_THE_MOD_THAT_CONFLICT

The code of your fix…

#endif

or

#ifndef THE_DEFINE_OF_THE_MOD_THAT_CONFLICT

The code of your fix…

#endif

 

Use #ifdef or #ifndef depending if you need the mod that conflict be loaded or not be loaded to execute your fix.

 

The last thing to do is to allow your mod to find the file where the #define is.

For that you have to modify your root config.cpp file.

For exemple if your fix is in /scripts/5_mission/YourScript.c you need to add in your « missionScriptModule » files path list :

"TheModThatConflictName/Scripts/Common",

place it before your own mission path and don’t forget the « " » and the « , » at the end of the path.


I recommend to unpack the mod that conflict to verify in its root config.cpp the path it use because some of them use a « prefix » before the mod name (example : PrefixName/ModName/Scripts/...). If a prefix is used it have to be include in the path of the define folder.
 

Additional Tips :

You don’t need to create a #define if your mod only contain config.cpp files because this feature only concerns scripts in .c file.

 

You can add your own internal #define, for example #define MY_DEBUG_MOD_ACTIVE, then you only have to comment/uncomment this define to active/deactive the code in all correspondant #ifdef at once, wherever it is in your scripts.

(Just don’t forget to add the path the your root config.cpp)

 

You can use the vanilla #ifdef SERVER to execute code only server side.

And to believe the official documentation it is little bit more optimised than « if (IsDedicatedServer()) » or « if( GetGame().IsServer() && GetGame().IsMultiplayer() ) »

 

Note that the code not supposed to be executed because of #ifdef / #ifndef is not compiled at all when the server/game is launch (that’s why #ifdef SERVER is less ressource consuming)

 

Conclusion :
I really think that if most of modders do that, it will make things lot easier to solve conflicts, sadly today it seems that only experienced modders know and use this feature whereas it is very easy to do.

The ideal solution would be that the vanilla game automatically detect the mods loaded or not, but as far as I know it is not the case 😞

 

If you know modders, please share the information,  I think #define can help to solve some situations.

I don't know where else to post to be visible, as platforms like discord or reddit… don’t keep posts visible a long time.

Maybe it would be a good idea to set page on the BI wiki but I can’t connect to it and don’t know if I could be allowed to post on it.

 

PS1 : in case a BI dev read this, it would be nice if an automatic build-in solution could be done (based on the class name of the mod set in the root cpp for example, all mods have this file).

Edited by Liven_28
  • Like 1
  • Thanks 2
  • Beans 1

Share this post


Link to post
Share on other sites

Hi, thanks for the article, I translated it into Russian and distributed it to the Russian Community.

Share this post


Link to post
Share on other sites
10 hours ago, eas1Iy said:

Hi, thanks for the article, I translated it into Russian and distributed it to the Russian Community.

Было бы что переводить, если не дружишь со скриптингом - это не лечится.

Не я не к аскоблениям, я просто констатирую положение дел 🙂

----- English version of russian answer:

It would be something to translate, if you (anyone) are not comfortable with scripting, it cannot be cured.

No I'm not about to offend or something, I'm just defined the state (of the problem) 🙂

  • Like 1

Share this post


Link to post
Share on other sites
On 2/11/2022 at 4:22 PM, Liven_28 said:

What does a #define :

It allows to know if a mod is loaded or not.

 

How to add a #define :

Create a file like this /YourMod/Scripts/Common/define.c

and add this inside : #define THE_NAME_OF_YOUR_MOD

(you can set the name you want but it have to be different from the other mods)

And that’s all ! You see, very easy, very quick and trust me very useful !

 

How to use #define :

If you’re a modder and have a conflict with another mod, sometime you can add code to your mod to solve this conflict.

But doing this, it can add bugs for those using your mod but not the mod that conflict (when the fix need to refer to variables or classes included in the mod that conflict).

 

To solve this situation you can use this type of code :

#ifdef THE_DEFINE_OF_THE_MOD_THAT_CONFLICT

The code of your fix…

#endif

or

#ifndef THE_DEFINE_OF_THE_MOD_THAT_CONFLICT

The code of your fix…

#endif

 

Use #ifdef or #ifndef depending if you need the mod that conflict be loaded or not be loaded to execute your fix.

 

The last thing to do is to allow your mod to find the file where the #define is.

For that you have to modify your root config.cpp file.

For exemple if your fix is in /scripts/5_mission/YourScript.c you need to add in your « missionScriptModule » files path list :

"TheModThatConflictName/Scripts/Common",

place it before your own mission path and don’t forget the « " » and the « , » at the end of the path.


I recommend to unpack the mod that conflict to verify in its root config.cpp the path it use because some of them use a « prefix » before the mod name (example : PrefixName/ModName/Scripts/...). If a prefix is used it have to be include in the path of the define folder.
 

Additional Tips :

You don’t need to create a #define if your mod only contain config.cpp files because this feature only concerns scripts in .c file.

 

You can add your own internal #define, for example #define MY_DEBUG_MOD_ACTIVE, then you only have to comment/uncomment this define to active/deactive the code in all correspondant #ifdef at once, wherever it is in your scripts.

(Just don’t forget to add the path the your root config.cpp)

 

You can use the vanilla #ifdef SERVER to execute code only server side.

And to believe the official documentation it is little bit more optimised than « if (IsDedicatedServer()) » or « if( GetGame().IsServer() && GetGame().IsMultiplayer() ) »

 

Note that the code not supposed to be executed because of #ifdef / #ifndef is not compiled at all when the server/game is launch (that’s why #ifdef SERVER is less ressource consuming)

 

Conclusion :
I really think that if most of modders do that, it will make things lot easier to solve conflicts, sadly today it seems that only experienced modders know and use this feature whereas it is very easy to do.

The ideal solution would be that the vanilla game automatically detect the mods loaded or not, but as far as I know it is not the case 😞

 

If you know modders, please share the information,  I think #define can help to solve some situations.

I don't know where else to post to be visible, as platforms like discord or reddit… don’t keep posts visible a long time.

Maybe it would be a good idea to set page on the BI wiki but I can’t connect to it and don’t know if I could be allowed to post on it.

 

PS1 : in case a BI dev read this, it would be nice if an automatic build-in solution could be done (based on the class name of the mod set in the root cpp for example, all mods have this file).

Mate honestly right idea for creating separe file for defines, but be honest #define can be declared in any part of the code except only inside the function body. 🙂

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×