Results 1 to 3 of 3

Thread: How to access code from various submissions from each other, using a dll

  1. #1
    Ace
    Join Date
    May 2015
    Location
    Kansas City, Missouri area
    Posts
    515
    Post Thanks / Like
    Total Downloaded
    130.02 MB

    How to access code from various submissions from each other, using a dll

    Many of us want to modularize our mission code used in CloD. One way to do that is to have various submissions that include code to handle various tasks. For example, we have submissions to handle stats, aircraft supply, various radar functions, handing AI aircraft movement, etc.

    The problem is, the code from each of those submissions is a silo--you can't access any variables, methods, data, etc from submission #1 if you are in submission #2 code.

    So, there is a pretty simple way to solve this using a compiled .dll that you put into parts/core.

    Here is a thread on 1cpublishing that explains how, using a singleton class to exchange certain variables:

    http://forum.1cpublishing.eu/showthread.php?t=32329

    I took it a step further and had various submissions exchange their Mission class instance in the hopes I would be able to just access (for example) StatsMission.MyMethod() from MainMission without further ado.

    It's not quite that easy as you have to define an interface for each Mission class in the .dll, then include that .dll as a //$reference in each submission.cs file, then implement that interface in your various submission Mission classes. Then you can access any methods, variables, etc defined in the interface in any of the submission files. (The submission .cs files are compiled into separate dlls, so without the interfaces defined in the dll--which is then loaded by each submission.cs--they have no idea about each other's publicly available methods or variables.)

    So it's a bit complex to set up but pretty slick in the end.

    The dll code we're currently using is here:

    https://github.com/bhugh/TWC_Mission...ommunicator.cs

    Using that code, here is how (for example) the Main and Stats submissions can communicate:

    In the -main.cs initializer method:

    Code:
    ...
    //$reference parts/core/CloDMissionCommunicator.dll
    ...
    public class Mission : AMission, IMainMission //Mission implements the interface defined in the dll.
    { ...
    
    Public Mission() {
    ...
    TWCComms.Communicator.Instance.Main = (IMainMission)this; //allows -stats.cs and other submissions to access this instance of Mission
    ...


    public override void OnMissionLoaded(int missionNumber)
    {
    ....
    TWCStatsMission = TWCComms.Communicator.Instance.Stats;

    ...
    }

    }
    Then in the -stats.cs

    Code:
    ...
    //$reference parts/core/CloDMissionCommunicator.dll
    ...
    public class Mission : AMission, IMainMission //Mission implements the interface defined in the dll.
    { ...
    
    Public Mission() {
    ...
    TWCComms.Communicator.Instance.Stats = (IStatsMission)this;  //Gives this instance to the communicator so -main.cs and other submissions can access it
    TWCMainMission = TWCComms.Communicator.Instance.Main; //gets the instance of -main.cs Mission class so that this class can use it
    ...
    
    
    public override void OnMissionLoaded(int missionNumber)
        {
    ....
    
    TWCSupplyMission = TWCComms.Communicator.Instance.Supply; //get the instance of another submission .cs Mission class that we will need to access elsewhere
    //You can access -main.cs immediately in the class initializer but other submissions might load before or after this one, so you need to load their instances OnMissionLoaded to be sure you get them, if they load later than this submission.  
    ...
    }
    Then I can access methods & variables from, for example, -stats.cs in -main.cs like this:

    Code:
    if (TWCStatsMission != null)  TWCStatsMission.Display_AircraftAvailable_ByName(player, nextAC: false, display: true, html: false);  //need to check TWCStatsMission != null because the -stats.cs loads somewhat late and their could be times very early in initialization where the variable will still be null
    And I can access something from -main.cs in -stats.cs like this:

    Code:
    mission_id = TWCMainMission.MISSION_ID; //don't need to check TWCMainMission != null here because main mission always loads before the subsmission, so if TWCMainMission isn't available something is very seriously wrong here
    So, that is a lot of code to do something pretty simple, but it does work.
    Last edited by TWC_Flug; Oct-25-2018 at 11:18.

  2. #2
    ATAG Member ATAG_Torian's Avatar
    Join Date
    Sep 2011
    Location
    Newcastle, Australia
    Posts
    1,592
    Post Thanks / Like
    Total Downloaded
    108.53 MB

    Re: How to access code from various submissions from each other, using a dll

    I admit that all of the above might as well be in Mandarin for me but I am soooo glad there are folk like you who put in all the effort to make these things happen.
    System specs:
    Gaming laptop: Metabox P750ZM, CPU Intel i7-4790K @ 4GHz, GTX 980m with 8Gb DDR5 VRAM, 16Gb Dual channel DDR3 RAM, 2 x Samsung EVO 840 500Gb SSDs, Win10 Home 64bit.

    Gaming PC: MSI PRO Z690 WIFI DDR5 mobo, Intel i5 12600KF cpu, 32Gb Corsair DDR5 5200Mhz RAM, Samsung 980 Pro 1 TB M2 SSD, Corsair 850w psu, MSI RTX 3070 8GB vram vid card, Windows 10 Home 64bit.

  3. Likes ATAG_Snapper, SIA_Sp00k, MezzA liked this post
  4. #3
    Novice Pilot
    Join Date
    Jul 2013
    Posts
    50
    Post Thanks / Like
    Blog Entries
    2
    Total Downloaded
    78.01 MB

    Re: How to access code from various submissions from each other, using a dll

    Back in the days when I was more active solution for those submission problem was recompiled Strategy.dll, of course with a bit of help of MySql but it saved a lot of code.. and time.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •