[Discussion] [Changing properties on run-time, dynamically added ActorComponents]

Good morning everyone.

I’ve spent the past couple of weeks diving into UE, wanting to explore and learn more about it.
As a learning challenge, I’ve taken one of my favorite games (RTS Warcraft 3) and I’m recreating from scratch different aspects of it.

I’ve been making great progress so far and I have encountered an interesting dilemma on how to best handle dynamically added ActorComponents to my units at run-time and wanting to change properties before I actually run the game.
e.g - I add a Movement component that allows the unit to move, but it has a property called Speed.

Note: Anyone familiar with RTS games will know of the CommandCard (The area that holds your buttons that you issue commands to units with. e.g - Move, Stop, Hold Position, Attack, Patrol, etc…) I will be focusing on this area.

Let me step back a bit and explain what I’ve done up to this point.

  • I have a parent class called BP_TestUnit that has an ActorComponent called CommandCard, that itself has an array property called CommandCardItems that can be filled with ActorComponents. (The array holds the components that will appear in your CommandCard. I will be focusing on a specific ActorComponent called Movement)
  • A child BP of BP_TestUnit called Worker will inherit the CommandCard actor component and fill its array with whatever it wants.
  • In the example of Worker, he will have a Movement component in this array.
  • When I run the game, I iterate through through the Array above and dynamically add (in this example for the Worker) the Movement component to it. (Note: The Worker unit only has the CommandCard actor component attached to it)

Question: My Movement actor component has properties (e.g - Speed) that I can’t edit in the editor because I dynamically add the Component in runtime. If I wanted to keep my system the way it is, is their a way to allow me to change the property on the Movement component if it isn’t on the Worker unit?

Note: I’m not blocked as their are multiple ways I can get around this. I see this side-project purely as a learning exercise and I’m exploring to what is in the realm of possible.

Video of the same things I have described above, just explained with visual examples.
Youtube video explaining everything visually

1 Like

Would this work? Or this is more complex and your have inheriting actor components?


Alternatively, spawn with defaults and call a custom event / function in the AC after adding it, piping the data in.

Thank you Everynone for your suggestions!

I will take a look at making the variables Instance Editable & Expose on Spawn and see if it works for me.
Question: What is that Add New Blueprint? I can’t seem to find it anywhere. :face_with_raised_eyebrow:


My second plan is nearly identical to yours, I would have a huge list of variables exposed to the User (Like the Warcraft3 object editor), call a function in the AC and pipe the data in.

It’s Add Component by Class:

It’s a dynamic node, so if you DO choose a class from the drop down, the node and its name will change. The top 2 items are the same node.

The one at the bottom is the one you were probably using to add a component. It will also work once you expose the variables:

image

The thing is that you may want to add a lot of components, each from a different class:

Adding 5 would by fine in any way but adding 150 would be tiresome, so Add Component by Class would make more sense. But then you cannot expose since each class has different data pins. But then there is inheritance and we actually can:


I would have a huge list of variables exposed to the User (Like the Warcraft3 object editor), call a function in the AC and pipe the data in.

I would then look into how structs work if you haven’t done so already:

Essentially you pack a lot of data types into a wrapper, so all this:

image

Can become a single variable, which you can then have nested arrays of. So options for storing millions of variables open up.

a huge list of variables

Data Tables also work with structs; and a Data Table can pull data from a spreadsheet / json. It’s easier to operate on 1 million lines in Excel than it is in Unreal’s data table.

Thank you!

I see the Add Component by Class and if I plug in my Movement AC, I do see the exposed attributes!

I will have to look into how to create a new function that inherits from Add Component by Class, like you did with Add ARComponent.
I don’t believe I have tried that before.


I am aware of Structs and you are right, I should be using that instead of a massive list of variables!

1 Like

Hey Everynone!

After thinking about it for awhile, I’ve used half of what you suggested and half my own way, to get it to work!

I wasn’t able to figure out how to get the Inheritance working, that you mentioned with “Add ARComponent” and exposing the variables.

So instead…
In both my Actor Component & Actor base classes. (I will refer to them as BaseActorComponent & BP_TestUnit)

  • In my BP_TestUnit, I added a custom struct for my Movement Actor component, I set this up across 4 units (Warrior, Ranger, Worker & Hero)
    image

  • Then I loop through my CommandCardItems array, add the Actor Component and call its Initialize Stats interface call.

  • The Initialize Stats function call is implemented for each custom Actor Component that grabs the relevant structure from the actor.

  • I can see that each of my units have their own Movement speed set! (Granted its not really doing anything atm).

It’s probably not the best system as I foresee some potential issues when this system scales up, but this is a great starting place and I’m very happy with it for now!!

1 Like