Why is "Cast To MaterialstanceDynamic" not working in some cases in the ConstructionScript?
So I've made a simple function (in a BlueprintFunctionLibrary) to change the color of an object based on its team. (The "Colors" array is hard-coded local variable in the function)
I use it in 2 of my classes' ConstructionScripsts: a Character and an Actor
Now the end result looks the same in both cases - the mesh does change its color.
However, while the Character is going through "CREATE material" once and then only changes it, the Actor is constantly creating new materials (cast fails all the time)!
The thing that puzzles me the most is that when I put the function in the TickEvent() graph of the Actor it starts working as intended (1 create and many changes).
Can someone explain this behavior?
asked Jul 18 '18 at 07:16 PM in Blueprint Scripting
If your Actor holds the variables referencing the MDI, then there is nothing there until the Constructor is finished. It just doesnt exist yet.
So Beginplay is probably a better node for using the MDIs and setting their params, but i think it works okay to create the MDIs and set the variables that reference them.
answered Jul 19 '18 at 07:46 PM
I could be wrong but as far as I have observed in my own development efforts, the actor is NOT placed in the level until AFTER the construct function is done, and the BeginPlay function fires. The Actor might exist somewhere in memory, but I think it is not actually placed in the level until then. But maybe the components are initialized enough to make an MDI off of them before then. I'm not sure. Your problem could be something else but this seemed like a good place to start looking.
Why not try moving it to BeginPlay and see if it works (even if it gets the team color wrong?)
answered Jul 25 '18 at 04:49 PM
That's pretty weird. I don't know why that's happening and wish I could help better. I had an equally weird idea though: What if you created the MDI outside of the class that uses it, like some kind of singleton actor or the GameInstance? Not the best practice probably but maybe it would stop it from being so loopy.
answered Aug 21 '18 at 06:32 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here