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

C# constructors appear to be called twice #40970

Open
nathanjwtx opened this issue Aug 2, 2020 · 7 comments
Open

C# constructors appear to be called twice #40970

nathanjwtx opened this issue Aug 2, 2020 · 7 comments

Comments

@nathanjwtx
Copy link

Godot version:
v3.2.2 stable mono

OS/device including version:
Windows 10
GLES3
Nvidia GTX1050

Issue description:
Not really sure if this is a bug as my game seems to run just fine but seemed like odd behaviour.
I was doing some testing to check on order of loading/creation by adding some log statements to constructors and _Ready. The out appears to suggest that constructors are called twice.

Steps to reproduce:
Create a Mono/C# project.
Create some classes and add console statements to the constructor and _Ready method.

image

@Calinou
Copy link
Member

Calinou commented Aug 2, 2020

@nathanjwtx
Copy link
Author

@Calinou thank you for the link. from reading that i'm not sure if that means I shouldn't use a C# constructor or not. I'm assuming the additional call to the Singleton will just be ignored/return the existing instance but what about the other classes...

@nathanjwtx
Copy link
Author

If you want to close this issue and carry on with the linked issue that's fine with me 👍

@Calinou
Copy link
Member

Calinou commented Aug 2, 2020

I think we can leave this issue open. I'm not sure if this is a bug or intended behavior that should be documented.

@okla
Copy link

okla commented May 12, 2022

I think it is a bug, because it may lead to the undesired results and there is no way to workaround as far as I can see.

For example, I create a C# class through AutoLoad. In its constructor I make OS callbacks (create shared memory and semaphores), and since it's called twice all the setup made twice too and from OS side it looks like there are two launched processes, which is unwanted.

And there is no way to workaround this, because even static variables are reset, so I can't say if current constructor call is first or not.

@CavemanIke
Copy link

I was getting some weird problems with my project because of this issue.

For testing, all I did was create a new project, added a Node to the scene, then added a new .cs file to that node. In the .cs file I created a constructor as one might do in C#. The following output occurs, showing that the constructor is called twice.

image

I tried this with 3 different new projects and got the same result each time. I would also like to note that this behavior does NOT occur if the project is run more than once without modification. If any of the source files attached to any nodes in the scene are modified and the project re-ran the constructor will be called twice again. This will also occur on all nodes in the scene with constructors.

@MBoqui
Copy link

MBoqui commented May 31, 2023

First of all, this is my first comment, if this is the wrong place for it, please let me know how to improve and where to put it.

I believe I had this same issue with GDExtension (Godot 4.0.2-stable).

I had this in the constructor of a class extended from node:

BehaviorTreeAgent::BehaviorTreeAgent()
{
...
    BehaviorTreeManager *behavior_tree_manager = BehaviorTreeManager::get_singleton();
    _tree_root = behavior_tree_manager->get_target_tree(_target_tree);
...
}

And it was crashing my project on opening. Both opening the project or running the main scene directly caused this crash. It was a null reference on _target_tree, which was supposed to be set in the inspector.

The problem is: even just opening the project was calling the constructor with a null reference and crashing.

I added a if (_target_tree == nullptr) {return;} before the above code and it solved my problem. And just like @ByteSizeOtter mentioned in the previous comment, once it runs this constructor once at project opening, it is not called again, unless I change stuff.

This behavior seems very odd to me. The constructor being called by itself without any instance on the scene or even the game running, just opening the project. It means we have to handle this "initialization" of our class in the constructor together with our normal constructor stuff. To me, this is breaks single responsibility of the constructor and isn't very usual, which made it take me a long time to figure out what the problem was, until I found this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants