Iterate and Retrieve Variables from Characters

Hello friends,

I’m trying to simply set variables from multiple spawned characters, but mixed results with Get All Actors of Class. Problem is it sets the same value to all instanced characters, instead of individually. Also, outputs multiple times. Perhaps I’m doing something wrong. Thanks for the help! :slight_smile:

We might need to see more of your Blueprint to get a better idea of what you’re doing, but one thing I can say is that you should probably use a Foreach loop after getting all the characters you want.

Thanks for the reply. Without getting overly complicated, i’ll just show the troubled part in AI Controller blueprint.

The end just simply prints out the result of Get All Actors of Class. In my level blueprint I spawned 10 AI actors, and when I use the GetAllActors function it also outputs a list of 10.

Makes sense for getting ALL the actors. However, even using the get command with the index at 0 STILL outputs all (10) actors, hmm. Perhaps someone can show their way of iterating and assigning values to actors? Hope it was clear enough, thanks :slight_smile:

Since this is called inside the AI Controller blueprint, this would explain the issue: each character is controlled by an AIController, which is why you get 10 outputs. Every controller gets the list of all characters, and prints the name of the first one.

You could either get only the character owned by the controller, or you could get the list of characters outside the controller blueprint, and print everything inside a Foreach loop.
As your goal is to assign values to each character, I would personally recommend the second way, as you simply won’t be able to do that inside the controller blueprint (you only want to give each controller relevant data, and not data about every character).

I didn’t consider that, thanks! However, I should’ve been more clear. I am trying to assign a value to a Blackboard key - the reason I did this in the Controller blueprint. Apparently you can’t assign values within the Character blueprint, because I get the error, “Error Blueprint Runtime Error: Accessed None trying to read property Blackboard from function: ‘ExecuteUbergraph_AICharacter’ from node: Set Value as Int in graph: EventGraph in object: AICharacter”. It turns up null otherwise.

As you mentioned, when I assign a value to the Blackboard ALL the controllers do this execution and thus always share the same value. So I am having trouble assigning unique blackboard key values. I guess the new question would be how to iterate and assign unique blackboard key values?:smiley:

You will need a function that takes the parameter you want to assign to the Blackboard as input, so that you can call it from outside the controller blueprint, and pass different values for each controller.
This should allow you to easily assign different values for each controller.

What people are trying to say:
There are 10 instances (copies) of your AI controller, because there are 10 characters being AI-controlled.
EACH ONE OF THEM will run a copy of the same code you make.
So, instead of getting display name of item 0 in an array, you can use Get Controlled Character to get the character that the particular AI controller instance is controlling, and then print the display name of that.
That will let you vary what’s being printed based on which specific character is being controlled.

Now, you can provide different property values for your controlled character instances when you spawn them, and/or in the level if you pre-place them.
Define properties in the character blueprint as “editable” and “expose on Spawn” to do this.
You can then cast the Get Controlled Character return value to YourCharacterClass, after which you can read the specific instance variable value (say, each character could have a different Strength attribute, or whatever.)

Thanks soooo much guys! Got it :smiley:
Finally I can make the Blackboard more dynamic.