How to differentiate between blueprint class object instances?

So I have a Squad of characters which has a separate ‘Squad’ blueprint which is supposed to control the characters assigned to it all. I have built functionality for that but problem happens when there are multiple squads and I can’t figure out a way to assign the Squad blueprint to the characters as their leader as there are multiple instances of the same Squad and thus the need to differentiate between instances.

Without knowing much about how the rest of your project is setup, I would one of the following approaches:

  1. Pass in the Squad to the character that is being created as it’s being created
  2. Assign the Squad to the character through a higher level process
  3. Tweak your architecture such that the character does not requires knowing the Squad it is a part of

If there’s going to be a dependency between those two types, it’s better to make that explicit upfront. It also likely that there’s no reason to try and discover the information when the code spawning the character actor probably already knows the Squad they should be added to (since it’s probably the Squad spawning them in the first place?).

1 Like

Thank you for such a detailed reply!! really appreciate it.

For 1. So there seems to be a problem trying to assign the Squad to the Soldier at Spawn as the pin only takes in the parent class of the soldiers and I would need to build an kinda completely new functionality for that so not doing that now.
For 2. The band aid fix is to duplicate the entire Squad folder and naming it separately to get another Squad in game. That way everything should work as expected. I think this is what you meant by a higher level process?

For 1. you should be able to pass in a Squad instance, if there is a Squad property on the soldier blueprint, and this property has the “Expose to Spawn” checkbox checked in property settings.

For 2. duplicating the blueprint/folder has nothing to do with a higher level process. Currently the challenge for you seems to be that you’re not clearly separating “class” from “instance.” A blueprint defines a “class” but once you instantiate it, you get an “instance.” You want something like 6 soldier instances, and 2 squad instances, where 3 of the soldiers reference squad A (and that squad references those soldiers,) and the other 3 reference squad B (and that squad references those soldiers.)

The easiest way to do this, is for the Squad blueprint instance to simply spawn 3 soldiers, and setting the Squad property on each of those soldiers to itself. (And add the soldiers to its own array of soldiers controlled.)
Then place two separate instances of Squad in the world, where you want each clump of 3 soldiers to spawn.
You could even make this “squad spawner” contain an array of markers where the soldiers will be spawned for each.

Another option is to place the soldiers in the world, then place two squad instances in the world, and use an actor eyedropper to mark which soldier goes with which squad (or vice versa.) Have the Squad (or soldier) wire itself up in On Begin Play based on the configured target actor.

Here’s an example of that method:

1 Like

So I was using the Array method but now somehow I just can’t figure out how to communicate with the soldiers that are getting spawned and access their reference variable to set it :frowning:
Its should be really simple but I just can’t figure it out, the object pin does not let me access the variable, and casting is not really possible with an array.
First pic shows where I am trying to access the variable from
Second pic shows the variable im trying to access to set a reference

You probably don’t have that variable flagged as instance editable, but you don’t have the properties shown so I can’t be sure.

1 Like

Set the “squad” variable on the “soldier” instances?
You spawned the soldier. You have an object of the right type. You should be able to get it, unless the variable in question is marked Private or not Instance Editable. I’m assuming you’re talking about the “Squad” back reference variable on the Soldier here.
But maybe you’re talking about the “array of soldiers” variable on the Squad? Still, same thing with access controls.

However, you’re also talking about “casting” – what is it you want to cast? Why is the object not already of a suitable class?
If you want to do something to each object in an array, you have to iterate over the array, and do it to each element. If the element is not of the right subclass, you have to cast the element, not the array.

And, finally, in the first of your two screen shots, you show an array named “spawned soldiers,” but there is no variable named “Spawned soldiers” so I assume this means it’s just a return variable from the function, not a member of the blueprint. If that’s the variable you want to access, that’s not going to work – it needs to be a real variable on the thing you’re accessing.

1 Like