This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

My purpose is to print something only in debug version but not in release version.

As I known, one way is

if OS.is_debug_build():
   print(XXX)

I try to use such a function:

func debug_print(x):
  if Os.is_debug_build():
     print(x)


debug_print(x) (work)
debug_print(a,b) (failded)
debug_print(a,b,"c") (failded)

However, the function can only handle one argument. How to handle more argmuments like print function?

Godot version 4.0 beta1
in Engine by (24 points)

1 Answer

+1 vote
Best answer

However, the function can only handle one argument. How to handle more argmuments like print function?

GDScript doesn't not support variadic function arguments, so you have to use the following approach instead:

func debug_print(arg1 = "", arg2 = "", arg3 = "", arg4 = "", arg5 = "", arg6 = "", arg7 = "", arg8 = ""):
    if OS.is_debug_build():
        print(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)

It's not very elegant, but it works :)

The above example supports up to 8 arguments, which is enough in nearly all sitautions you'd use a debug print for.

by (12,908 points)
selected by

As you say, it works!
Thanks very much!

According to the documentation, the code inside assert is only run in debug mode.

assert(not print(a, b, c, d, e, f))

However, given that print does not return any value, I don't know if such code will be safe. Apparently, in Godot 3.x, calling assert with "not void" does not generate an error.

Another solution is to pass an array to your function.

func debug_print(x):
  if Os.is_debug_build():
     print(x)
debug_print(x) (work)
debug_print( [ a,b ] ) (work)
debug_print( [ a, b, "c" ] ) (also work)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.