How to use the class GlobalStructureList?

Discussion in 'Empyrion API' started by Hackasaurus Rex, Aug 10, 2017.

  1. Hackasaurus Rex

    Hackasaurus Rex Ensign

    Joined:
    Jul 15, 2017
    Messages:
    7
    Likes Received:
    0
    The class, GlobalStructureList is returned from, "Event_GlobalStructure_List" from the request, "Request_GlobalStructure_Update." I'm using the _update variant to get playfield-specific structures. I am looking specifically for what faction a specific structure belongs to, but I would like to know the method of retrieving that information from GlobalStructureList. It looks like GlobalStructureList.globalStructures is a dictionary, but to my knowledge I need the key to get the value, but I can't seem to find a method to retrieve what keys are available in there.

    A little template in case anyone needs to go into VS and fiddle around with it.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Eleon.Modding;
    namespace StructureThingy
    {
        public class StructureThingy : ModInterface
        {
            ModGameAPI gameAPI;
    
            public void Game_Start(ModGameAPI dediAPI)
            {
                gameAPI = dediAPI;
                gameAPI.Game_Request(CmdId.Request_GlobalStructure_Update, (ushort)CmdId.Request_GlobalStructure_Update, new PString("Akua"));
            }
    
            public void Game_Event(CmdId cmdId, ushort seqNr, object data)
            {
                if (cmdId == CmdId.Event_GlobalStructure_List)
                {
                    GlobalStructureList GSL = (GlobalStructureList)data;
    
                    //No idea what to do with this now.
                }
            }
    
            public void Game_Update()
            {
    
            }
    
            public void Game_Exit()
            {
    
            }
        }
    }
    Any help is appreciated :)

    P.S. If you do a game request with say, a ushort of 55, will the corresponding event be returned with a ushort of 55?
     
    #1
  2. Xango2000

    Xango2000 Captain

    Joined:
    Jun 15, 2016
    Messages:
    385
    Likes Received:
    202
    I'm guessing the Key is the name of the playfield

    so...
    Code:
                            GlobalStructureList structureList = (GlobalStructureList)data;
                            for (int i = 0; i < structureList.globalStructures.Count(); i++ )
                            {
                                if (structureList.globalStructures["playfield"] == "factionId")
                                {
                                    "do this stuff";
                                }
                            }
                            break;
    
    keep in mind, I have never played around with this event and I'm VERY new to programming.
     
    #2
    jmcburn likes this.
  3. jmcburn

    jmcburn Rear Admiral

    Joined:
    Jan 15, 2017
    Messages:
    1,113
    Likes Received:
    1,759
    That should do the trick:

    Code:
    
    public void Game_Event(CmdId cmdId, ushort seqNr, object data)
          {
               if (cmdId == CmdId.Event_GlobalStructure_List)
               {
                   GlobalStructureList GSL = (GlobalStructureList)data;
    
                   foreach(var structure in GSL.globalStructures)
                   {                  
                       if (((Eleon.Modding.GlobalStructureInfo)structure).factionId==3)
                       {
                                //Do what you wish
                        }
                   }
    
               }
           }
    
    
    if you cast 'structure' to Eleon.Modding.GlobalStructureInfo, you get the available fields/properties like 'factionId'.

    Yes, exactly, if you send a request with a specific ushort seqNr, the corresponding event will have the same
    seqNr when it returns.

    Hope this helps.

    /jmc
     
    #3
  4. Hackasaurus Rex

    Hackasaurus Rex Ensign

    Joined:
    Jul 15, 2017
    Messages:
    7
    Likes Received:
    0
    I'll check these out when I can. Thanks.
    foreach(var structure in GSL.globalStructures)
    I don't quite understand this. I know what foreach does, but how does the "var structure in GSL.globalStructures" work?
     
    #4
  5. jmcburn

    jmcburn Rear Admiral

    Joined:
    Jan 15, 2017
    Messages:
    1,113
    Likes Received:
    1,759
    It means you define a new variable named 'structure' (or any other name) of variable type (with 'var' as type c# decides the correct type) pointing to the current element in the array 'globalstructures'.

    Now by using the variable 'structure' inside the loop, you are accessing the current element of globalstructures in the foreach loop.

    Feel free to ask, if you have any follow up questions.

    /jmc
     
    #5
  6. jmcburn

    jmcburn Rear Admiral

    Joined:
    Jan 15, 2017
    Messages:
    1,113
    Likes Received:
    1,759
    You could also write:

    Code:
    
    foreach(KeyValuePair<string, List<Eleon.Modding.GlobalStructureInfo>> structure in GSL.globalStructures)
    {
           if (((Eleon.Modding.GlobalStructureInfo)structure).factionId==3)
           {
                    //Do what you wish
           }
    }
    
    
    btw: globalStructures is the Dictionary that holds all the relevant information for each structure.

    if you cast structure like above to:

    ((Eleon.Modding.GlobalStructureInfo)structure).

    right after you enter the dot, visual studio will suggest all available properties and fields of the structure.

    upload_2017-8-13_11-55-54.png

    /jmc
     
    #6
    Last edited: Aug 13, 2017

Share This Page