1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

ModAPI : Chat message event ?

Discussion in 'Modding API' started by Albino, Sep 25, 2014.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Albino

    Albino Trainee Engineer

    Messages:
    22
    I cant figure out how to create an evetn with the MyAPIGateway.Utilities.MessageEntered so that when a player types something, I can call a method ... probably because I dont know c#. Can someone show me a little exemple script for that ?
     
  2. thepenguinmaster

    thepenguinmaster Trainee Engineer

    Messages:
    25
    [SIZE= 10pt]Here is some code to get you started. It is a good template for new files. I pulled it from one of my other projects and cleaned out the code specific to that mod. [/SIZE][SIZE= 10pt]

    You will want to lean c# for sure, if not only for Space Engineers, but also for a good life skill if you see yourself using the computer a lot in the future. [/SIZE]

    The following code shows two useful things, logging, and responding to the message events. It may be confusing at first, but once you start to work with it, it will start to make better sense.

    Here is ‘basically' what is going on:
    Space engineers will take care of calling UpdateBeforeSimulation for us. This is because we have the attribute (enclosed in the [ and the ] at the top of the file) that tells space engineers when to call our code.

    The UpdateBeforeSimulation will call every frame, and it will check if the script has initalized yet, using the _IsLoaded boolean flag (true or false). If it is the first frame, then we will add an event handler to MessageEntered which already exists in space engineers.

    Now whenever someone enters a message, it will do a very basic split and switch to determine what to do next. This is the part you will replace and do what you want. There are much better ways to go from a command to actual actions, but this is just a very very basic way so that you will be able to get going. There is nothing wrong with doing it this way, but if you get into doing a lot of commands then it will become a bit messy. Just give this method a try. You will want to put it in the correct mods folder which you can find documented elsewhere.
    Code:
    using Sandbox.ModAPI;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.Game;
    using Sandbox.Common;
    using Sandbox.Common.ObjectBuilders;
    using Sandbox.Common.Components;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    namespace thepenguinmaster.scripts
    {
        [Sandbox.Common.MySessionComponentDescriptor(Sandbox.Common.MyUpdateOrder.BeforeSimulation)]
        class commandScript : Sandbox.Common.MySessionComponentBase
        {
            private bool _IsLoaded = false;
            public override void UpdateBeforeSimulation()
            {
                if (!_IsLoaded)
                {
                    init();
                }
            }
            public void init()
            {
                _IsLoaded = true;
                MyAPIGateway.Utilities.MessageEntered += Utilities_MessageEntered;
            }
            private void Utilities_MessageEntered(string messageText, ref bool sendToOthers)
            {
                log("test");
                if (!messageText.StartsWith("/")) { return; }
                List<string> words = new List<string>(messageText.Trim().ToLower().Replace("/", "").Split(' '));
                sendToOthers = false;
                switch (words[0])
                {
                    case "Hello":
                        MyAPIGateway.Utilities.ShowMessage("HAL", "Hello World");
                        break;
                }
            }
            void ProcessCommand(List<string> words)
            {
                switch (words[1])
                {
                    case "cortana":
            MyAPIGateway.Utilities.ShowMessage("Cortana", "Hello World");
                        break;
                    case "hal":
                       MyAPIGateway.Utilities.ShowMessage("HAL", "Hello World");
                        break;
                    case "scotty":
                       MyAPIGateway.Utilities.ShowMessage("Scotty", "Hello Catptain");
                        break;
                }
            }
     
            protected override void UnloadData()
            {
                MyAPIGateway.Utilities.MessageEntered -= Utilities_MessageEntered;
                base.UnloadData();
            }
    
            private void log(string text)
            {
                using (TextWriter writer = MyAPIGateway.Utilities.WriteFileInGlobalStorage("Log.txt"))
                {
                    writer.WriteLine(text);
                }
            }
        }
    }
    
    
     
  3. soat7ch

    soat7ch Junior Engineer

    Messages:
    605
    If i wasn't busy with so much engineering classes right now i'd really love to learn C#. do you think the coming in-game programming will have a simpler surface (any kind of visualization)?
    When looking a the code it all seems to stand in a absolutely logic order, kinda like an electric circuit, but i have no clue which of those words in the text do what.
     
Thread Status:
This last post in this thread was made more than 31 days old.