Get variable from randomly spawned actor?

Hi all,

  1. I created several shape actors (for example cube, sphere, cylinder, cone, etc.).
  2. Then upon button press I will spawn them RANDOMLY in my level.
  3. When any of them spawn I will need to print their name on my widget (CUBE, SPHERE, etc.).
  4. What is the most optimised way for this?
  5. It would be best to have their name as variable on each actors and retrieve it from my gamemode BP when they spawn.
  6. However the difficulty is because they spawn randomly I couldn’t tell the engine to retrieve the name variable from certain actor before they spawn in game.
  7. As I plan to update the available shape actors as the game progresses, I want to avoid usage of any predetermined name list.
  8. Help? Thanks in advance!

Hey thanks for replying!
Actually depends on the level I might need to spawn several shapes at one button press.
So here’s how I set it up.

  1. I created 10 array of actor class.
  2. Each array consist of several number of actor class.
  3. Then I cast the 10 arrays to my gamemode bp.
  4. On my gamemode bp, I select 1 random array out of this 10.
  5. Then the selected random arrays feed into for each loop to retrieve their actor.
  6. Which is then spawned into the level using spawn actor from class.
  7. I believe there is no return node if I’m using this method?

Which is then spawned into the level
using spawn actor from class.

There is, on this very node. This is your object reference. Don’t lose it :wink:

Hi again,

  1. Yes there is return pin, however when I try to get actor name variable, it gave me selection of all possible variables from all possible random actors.

  2. I will then need to select to display which variable out of all possible variable from all possible actor but of course I do not know which actor will be spawned randomly.

  3. I tried to use same variable name for all actors and then atempted to get this variable from return pin.

  4. But of course it doesn’t work. The return pin still ask me to select from all possible random spawn actors.

im thinking this may be a communication and parenting issue. i dont think i was reading what you were trying to do correctly before. so your taking a array of actors and randomly selecting a actor from the array and spawning it right? then you want to add the name/ type of that actor to a widget which is presumably a list? (not sure on that last part as you havent described the widget).

are the actors your spawning blueprints? if they are then i think the issue may be in that they dont inherit from a common parent which would be the only way to get a variable that is common to all. try making a picture of what your looking to accomplish and show us what code your working with. also explain the widget a bit more.

im still very confused on what your looking to accomplish.

Hi!
This community is great. Please see my blueprint below,

I created object for example cube and sphere and I give them each variable “Name” and the value is “Cube” and “Sphere” then I cast them to gamemode. Like these two.

240509-1.png

Then the gamemode will randomly pick one to spawn.

After spawn the game mode creating widget to display the name of the random shape chosen.
240532-
How do I accomplish this is the number of objects is a lot or dynamic and I want to spawn more than one object and display more their respective name?

a few things, first it appears that your sphere and cube do not inherit from the same class so your two name variables are actually completely different and will cause problems for you when you try to get them after spawning. next why are you creating a reference to the game mode in the cube then calling it the cube reference?

below is the code i think youve been trying to make. at beginplay the game mode does a loop which spawns X number of items items and adds their name to a list that can be used to add their names to the widgets. it chooses random items from a predefined list of shapes (sphere, cube, cone, etc), spawns them, then adds the reference to a new list. once the loop completes a for each loop begins and prints the names to the screen. this last part is dependent on the widget you will be using as you never described it, is it one widget with all the names on it or does each item have its own widget.

this whole system relies on that all your shapes (cube sphere etc) derive from the same parent class. in my example i made a actor class of shapes and would create children of this class for cube and sphere, this way the children would inherit the properties of the parent such as the name variable.

also note that in my example the shapes list1 is a class reference array of the type shapes and the list spawned shapes is a object reference array of the type shapes.

Hi thanks for helping, really appreciate that.
However after I tried it seems it doesn’t work.

I follow your BP and everything seems fine. However when I arrived to the point where I need to retrieve Name variable, it gave me list of Name variable from all possible actors instead of generic ones.

Even this is actually achieved by un-ticking the context sensitive box which of course it’s also not compatible pin each other anyway.

How you get this node?

Thanks a bunch!

that node is just the node that gets the variable. did you setup your actors so that they inherit from a common parent? in the example i made all my shapes inherit from from an actor names “Shapes”. within the parent shapes bp i have the name variable, so i set the spawned shapes variable to the type shapes, this way all the array elements will have access to the name variable and not have to be casted.

Hi ThompsonN13,
It worked! Thank you.
One last question before I marked this as answered,
How you get the name variable without casting again?
I can do this but I still need to cast to parents shapes BP.

I just set the variable to the type shapes before plugging it up to the loop then you drag off the element pin on the loop and search for get name. You may need to disconnect every pin from the loop then reattach them for the change to take effect

Hi finally it worked!!!
Thank you for helping!

its really situation dependent and how your setting things up. if your looking to just get the variable when the actor is spawned then add them to an array or something then the easiest way would be to drag off the return pin when spawning as thats a reference to the actor thats spawned. this will get you a reference and therefore access to all variables therein.