This forum is locked and will eventually go offline. If you have feedback to share you can find us in our Discord channel "MythoLogic Interactive" https://discord.gg/nECKnbT7gk

Forum rules

Add overloadings of IGame.WriteToConsole to quickly log messages

Here you can find ScriptAPI suggestions implemented in the game.
Forum rules
By using the forum you agree to the following rules.
Locked
NearHuscarl
Superfighter
Superfighter
Posts: 97
Joined: Thu Feb 07, 2019 4:36 am

Add overloadings of IGame.WriteToConsole to quickly log messages

Post by NearHuscarl » Sat Mar 21, 2020 5:57 pm

In Javascript there is a `console.log()` method that helps you quickly log messages. I like that a lot and write a similar one in my script and in a lot of cases for me it removes the need to open Visual Studio and attach the debugger to SFD process which is quite troublesome. Here is the implementation

Code: Select all

        private static string GetDefaultPlaceholder(object[] messages)
        {
            var placeholder = "";
            var count = messages.Length;
            for (var i = 0; i < count; i++)
            {
                var isFloatOrDouble = messages[i] is float || messages[i] is double;
                placeholder += "{" + i + (isFloatOrDouble ? ":0.00" : "") + "}";
                if (i != count - 1) placeholder += " ";
            }
            return placeholder;
        }

        public static void WriteToConsole(params object[] values)
        {
            if (IsEditorTest)
            {
                WriteToConsole(string.Format(GetDefaultPlaceholder((values), values));
            }
        }
        public static void WriteToConsoleF(string placeholder, params object[] values)
        {
            if (IsEditorTest)
            {
                if (string.IsNullOrEmpty(placeholder))
                {
                    placeholder = GetDefaultPlaceholder(values);
                }
                WriteToConsole(string.Format(placeholder, values));
            }
        }
Usage

Code: Select all

Game.WriteToConsole("one", "two", "two".Length, 4.00000001); // prints "one two 3 4.00"
Game.WriteToConsoleF("{0}_{1}_{2}_{3}", "one", "two", "two".Length, 4.00000001); // prints "one_two_3_4.00000001"
0 x
Image

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

Post by Gurt » Sat Mar 21, 2020 6:32 pm

Neat suggestions. Adding it after v.1.3.4.
Game.WriteToConsole(params object[] values)
Game.WriteToConsoleF(string placeholder, params object[] values)
0 x
Gurt

Locked