When creating a ::godot::Node we should use memnew instead of new. I have made a subclass of Node3D that has two constructor parameters. I tried to create an instance with

memnew (MySubclassOfNode3D {x, y})

This fails, because memnew is a macro, and to the C++ preprocessor this is a macro call with two arguments, rather than one.

How do I create an instance of my class? Thanks!

  • xyz replied to this.

    ariess What happens if you do memnew(MySubclassOfNode3D(x, y)) ?

      xyz
      Congratulations, then it works!

      But I thought that the curly braces were now the preferred syntax for initialisation, in general in C++?

      • xyz replied to this.

        ariess There's no preferred syntax. You can use any syntax supported by the standard. Of course, it's best to keep it consistent within a project. So you should probably follow the style that Godot's source uses.

        Looks like the macro doesn't like braces for some reason. This issue should perhaps be reported. What errors do you get from the compiler?

          xyz
          The compiler says

          error: macro "memnew" passed 2 arguments, but takes just 1

          The error is a generic error related to macros, it's the way the preprocessor parser arguments to macros.

          xyz

          So you should probably follow the style that Godot's source uses.

          This is not always feasible. I might be working on a project that has it's own style guide. Or working with other libraries that each have their own style guide.

          xyz
          Btw, if I redefine the memnew macro as

          #define memnew(...) ::godot::_post_initialize(new ("") __VA_ARGS__)

          then it works. But I do not know enough about macros to be able to say whether this may cause other issues.

          5 days later