Page 1 of 1

Add overloadings of IGame.WriteToConsole to quickly log messages

Posted: Sat Mar 21, 2020 5:57 pm
by NearHuscarl
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"

Re: Add overloadings of IGame.WriteToConsole to quickly log messages

Posted: Sat Mar 21, 2020 6:32 pm
by Gurt
Neat suggestions. Adding it after v.1.3.4.
Game.WriteToConsole(params object[] values)
Game.WriteToConsoleF(string placeholder, params object[] values)