Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the 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: 1884
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 34

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