godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

No assert ?

Open SeleDreams opened this issue 3 years ago • 7 comments

Hi, I'm sending this message because as far as I can see I find no way to actually do an assert using godot-cpp which is a problem as the standard c++ assert function only prints to stdout and godot (god knows why) doesn't use the standard stdout making assert useless is there anything like Godot::assert or something that serves the same purpose ? I expected to find a Godot::assert the same way as the Godot class has the printing methods but no...

SeleDreams avatar May 08 '21 07:05 SeleDreams

godot (god knows why) doesn't use the standard stdout making assert useless

Godot prints error and warning messages (printed using ERR_PRINT()) to the standard error stream, so that they can be filtered independently of lines printed by the project using print() (print_line() in C++).

Also, I don't see the relationship between an assert function and stdout/stderr. Are you referring to the Godot process' exit codes instead? Godot tries to be as well-behaved as possible in this regard, but it's not perfect. Feel free to open pull requests on the main Godot repository to improve this :slightly_smiling_face:

Calinou avatar May 08 '21 17:05 Calinou

I ended up finding the macro CRASH_COND that seems to have a similar behaviour to assert (except that it takes the crash condition rather than the assertion condition) this one outputs to the godot log properly rather than stdout

SeleDreams avatar May 09 '21 11:05 SeleDreams

godot (god knows why) doesn't use the standard stdout making assert useless

Godot prints error and warning messages (printed using ERR_PRINT()) to the standard error stream, so that they can be filtered independently of lines printed by the project using print() (print_line() in C++).

Also, I don't see the relationship between an assert function and stdout/stderr. Are you referring to the Godot process' exit codes instead? Godot tries to be as well-behaved as possible in this regard, but it's not perfect. Feel free to open pull requests on the main Godot repository to improve this slightly_smiling_face

what i meant was that the problem caused by asset is that as it outputs to stdout, when it does crash because of an assert it won't output the assert output to the godot log, now that i found CRASH_COND Now though it's fine

SeleDreams avatar May 09 '21 11:05 SeleDreams

in fact seems like CRASH_COND only stops the current function though it doesn't stop the execution of the rest of the program

SeleDreams avatar May 09 '21 12:05 SeleDreams

I was able to make an ASSERT macro that works the same as CRASH_COND but reverses the condition and calls abort rather than return

#ifndef ASSERT
#include <Defs.hpp>
#define ASSERT(cond)                              \
    do                                            \
    {                                             \
        bool condition = !(cond);                 \
        if (unlikely(condition))                  \
        {                                         \
            FATAL_PRINT(ERR_MSG_COND(condition)); \
            abort();                              \
        }                                         \
    } while (0)
#endif

it might be a good addition to godot-cpp

SeleDreams avatar May 09 '21 12:05 SeleDreams

cc @Zylann @vnen

Calinou avatar May 09 '21 18:05 Calinou

This is supposed to be covered by CRASH_COND. I opened an issue a while ago because it does not behave correctly at the moment. It barely does the same as ERR_FAIL_COND... https://github.com/godotengine/godot-cpp/issues/521 instead it should basically do the same as assert() with reverse condition: causing an actual crash with the error printed to stderr, which in debug mode makes the debugger stop on it as well.

Zylann avatar May 09 '21 18:05 Zylann