ok so basically i have made a c++ function to add a component either before play by dragging an actor into the editor or dynamically at runtime. This all works fine no crashes
I then tried to make a Blueprint node to emulate the c++ code.
When i use the created node on begin play everything compiles and works fine as you can see here.
i am requiring 3 components. 2* box collision and 1* audio component.
as you can see it has worked perfectly and it has added the 3 required components dynamically at runtime begin play.
When i try and use the node in construction script so that it will behave the same as the c++ version in that i can drag an actor into the editor and it will create the component and try to compile the blueprint i get an editor crash
with the following errors. “stack overflow exception thrown” and i tried to debug in visual studio as i am not getting any crash report from unreal itself.
The issue appears to be caused by an infinite loop due to the reference to “self”. What’s happening is when the construction script runs it adds the component to the actor, which updates the actor and causes the construction script to run again, which would add another component, updating the actor and triggering the construction script (and so on and so on). This works in BeginPlay because this function is only called once rather than the construction script which is called any time there is an update to the instance.
ah yeah that makes sense now. can you think of any workaround to the problem that would allow me to accomplish what i need using the blueprint node?
with the c++ version it is easy because i just override the “virtual void PostInitProperties()” function and that way it spawns the components when i drag the actor into the editor.
You may want to take a look at the code for the AActor::AddComponent() function in ActorConstruction.cpp. This is the code function called when any of the AddComponent nodes, such as Add Box Component, are used in the editor which can be called in a blueprint’s construction script with a reference to self.
Sorry still a bit confused . So you’re saying I can expose this function to blueprint call it in the construction scriot and then get it to create the required component ?? If so how do I tell it what class I need in the function it is using template name and other stuff I don’t quite understand sorry thanks
I’m not exactly sure which function you’re referring to when you say “this”. The AddComonent function is already marked as BlueprintCallable and usable in blueprints and their construction script. When you use the “Add Box Collision” or “Add Static Mesh Component” nodes, it is calling the AddComponent function with the type of component specified by the node. If using one of these nodes doesn’t work for your case, I would try reviewing the code for AddComponent() to see how it can be used in the Construction Script without issue.
i have been looking at the addComponentFunction but like i said i find it very hard to follow and understand because of all the stuff to do with templates and engine and blueprint helper functions, and i cant seem to pinpoint where it actually is that you tell it what component you wish to make and then it seems like you use a different method to actually make the component