So I have a random name generator that fires every time I spawn in a new enemy AI so it can assign them a random name.
The function exists in the construction graph of the enemy AI and it functions properly as I am able to print the results to the screen.
I create an AI health bar and text (for name) via UMG. I am casting in the results of the bar and it functions well. But I am binding the textfield so I can grab the randomly generated name. I am casting in the BP that holds the name and the binding function is throwing no errors. But at runtime it fails every time.
Man this is a tough one because I have been searching the webs all day and the only thing I think I am missing is what object is not linked up in the cast…
So I got this to work…but I am not sure it is the best practice… So let me share.
So in the bind function event I created a function that runs the name generation script and holds all the data for the names. Then in the bind I fire that event and get the variable holding the name.
I feel like this is not a clean way to do this and I would rather there be a central location for this data so the function can be used in other ways and I kind of like the data structs to be exposed the in the enemy AI class but it does function. So I would like to change my original question to - IS THIS a good way of doing this?
The Object you are casting in “screen shot 2017-01-02 at 11.20.23 am.png” is called healthBar. So I assume it is a REF to a UMG? If so this is your error. It needs to be a REF to your enemyshipActor instead.
Although your method may work, it isn’t very efficient.
When you spawn your enemy ship, the name generator should be activated by the Event Begin Play event in your enemy ship blueprint as this is triggered when the ship comes into existence in the world; activating it via the construction script is not very efficient. (If you need the name and health to update in editor, not game, then you will need to do it in the construction script)
The pirate ranks and pirate names arrays should probably be stored on either your GameMode or Game State, to make them easier to access. (This is what i’ve done in my example)
You should then store this name in a variable on your ship blueprint, either in a struct variable or just in a text variable.
See below set up in enemy ship blueprint:
Setting the variable in the widget:
Add an enemy ship variable to your widget, of type ‘your enemy ship blueprint’. Then in the ship when you open the widget, pass the reference to the enemy ship through to the widget and store that in a variable.
Where ever you open the UMG, set the variable in the following way:
You can then bind the property for the text directly instead of going through binding functions.
This way is more efficient and allows you to directly bind ANY property on your ship to the widget without any further coding.
See below how to bind directly through variable stored on widget: