GameMaker: trace/log function

Debugging GameMaker games can be fun. Or not fun. Depends on what your defintion of "fun" is.
Either way, in GameMaker: Studio, a new debugger was added, which you can (and probably should) enable via File - Preferences - "Scripts and Code" - "Use the new Debugger".
That includes a profiler, a step-by-step debugger, and a bunch of other useful things.

But, alas, sometimes, there is no time to explain, no time to write the best code of century, and certainly no time to sit down and stare at variables in watch window. You got to make games fast. And debug fast.

GameMaker: Studio noticeably spots a "console" dock, which is used to display compilation and runtime information, errors, and bunch of other things. But the important part is, that you can also send your own messages into it via show_debug_message. That's nice, since throwing a look at dock is fast. But, you know, what's not fast? Using show_debug_message to do so.

So this post is basically about what I've came up with to improve use of the thing, both for Studio and not.

Now, to why the standard show_debug_message is not fast to use:
Consider the situation, where we want to log X and Y of player in format such as

780, 42

(if coordinates are 780 and 42 for X and Y accordingly)
That should literally take a second to type, so we'll just -

show_debug_message(string(x) + ", " + string(y))

... oh. Did it take 48 symbols to show two values? Well that's embarassing. A bit.
Of course, we can save some symbols and time on those spaces, or save some time with auto-completion, but thing remains - it's not that fast, and there's no time to explain, remember?
So what my smart planTM is, that we first get this little script going:

/// trace(...)
var r = string(argument[0]), i;
for (i = 1; i < argument_count; i++) {
    r += ", " + string(argument[i])

And then the use is as simple as:

trace(x, y)

11 symbols and same result! That's more like it. Could have named the script log for saving extra two symbols, but that'd clash with those math functions a bit, and I personally prefer trace more anyway.

What is going on, is that we simply add all arguments into a comma-separated value string, and then show that. Comfort at no cost.

But, not at all times the game may be ran from IDE, and not at all times you may be using GameMaker: Studio for development. Thus another output method is needed.
A thing that can be done quite easily and work at many places is logging of values into a file. For that one has to add a line of code to open file for writing at game start,

global.logfile = file_text_open_write(working_directory + "\temp1.txt")

And add another bit of code into the trace script to actually write text into it:

file_text_write_string(global.logfile, r)

And, perhaps, comment out the show_debug_message call, if you are using pre-Studio version of GameMaker, to avoid actual messages showing up (since that used to be the behaviour).

So, that's it! Hopefully, this was useful.

Related posts:

2 thoughts on “GameMaker: trace/log function

Leave a Reply

Your email address will not be published. Required fields are marked *