Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert() macro #2309

Merged
merged 3 commits into from Sep 1, 2021
Merged

Add assert() macro #2309

merged 3 commits into from Sep 1, 2021

Conversation

MoritzBrueckner
Copy link
Collaborator

@MoritzBrueckner MoritzBrueckner commented Aug 26, 2021

This PR adds an assert() macro to Armory in order to make the handling of some error cases easier and optional for better performance. This is especially helpful for checking correct user input in logic nodes for example, but this PR doesn't introduce any code yet that actually uses the macro. Assertions are completely turned off and omitted from the code when publishing a build.

Please note that assertions don't replace checks that are required during runtime! For example they should not replace important safety checks in some nodes where the input is allowed to be invalid during runtime (e.g. unavoidable null values).

For debugging, there are two different assert levels available: Warning (only shows a message but doesn't throw an exception) and Error (throws an exception). Error assertions are also active when the warning level is set by the user. The user can also decide whether to ignore assertions completely even in debug builds and whether the game should quit if an exception happens (helpful when in fullscreen for example). I'm not sure if there is a need for more levels, but they can be easily added later.

Usage

assert() is imported automatically in the armory directory but has to be imported for user scripts.

// If the second "condition" parameter evaluates to false, the assertion fails
// The last message parameter is optional
assert(Warning, 1 < 0, "Test assertion");

// Leads to

// Sources/armory/logicnode/PrintNode.hx:12: Failed assertion:
//        Message: Test assertion
//        Expression: (1 < 0)
assert(Error, 1 < 0, "Test assertion");

// Leads to

// Trace: Error:
// Failed assertion:
//         Message: Test assertion
//         Expression: (1 < 0)
//     at Function.throwAssertionError (<anonymous>:24213:9)
//     at armory_logicnode_PrintNode.run (<anonymous>:16643:24)
//     at armory_logicnode_MergedKeyboardNode.runOutput (<anonymous>:886:19)
//     at armory_logicnode_MergedKeyboardNode.update (<anonymous>:14860:9)
//     at update (<anonymous>:24822:28)
//     at kha_TimeTask.task (<anonymous>:51806:4)
//     at Function.executeTimeTasks (<anonymous>:51689:76)
//     at Function.executeFrame (<anonymous>:51639:18)
//     at renderCallback (<anonymous>:52540:17)

Unfortunately, the Blender UI is kind of ugly, but I didn't find a way to move the dropdown label above the dropdown while keeping it aligned with the column layout:

Screenshot of the new assertion settings in the Blender UI

@luboslenco luboslenco merged commit 052c7b0 into armory3d:master Sep 1, 2021
@luboslenco
Copy link
Member

Thanks!

@MoritzBrueckner MoritzBrueckner deleted the assert branch September 1, 2021 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants