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!

There is a reference node from “Spawn actor from class”, you could use that as reference and add it to an array and get that index and store it as a variable for later reference in a foreachloop check in that actor class.

Ok sorry for my English, I think I have to rephrase my question. I’ll make this more simple.

  1. I wanted to make a simple dictionary game where upon screen tap/click the game will randomly spawn a model on the level and display it’s name in UMG.

  2. For example from blank screen when u tap on it, apple will appear and “Apple” text displayed on top of it. When we tap the screen again maybe this time orange picture appear and “Orange” text printed on it.

  3. My progress so far is done creating the models into actor class and give them text variable called “Name” and I set their default value with their name. For apple model I put Apple, for orange model I put Orange.

  4. The reason I created variable instead of draw it together with the static mesh is that I wanted to be able to both get and set their value as part of the “dictionary” aspect of the game. I might need to change it into other language or display other text along with it.

  5. Now I have no problem to choose randomly and send the chosen model to viewport.

  6. However I couldn’t find a way to retrieve the value of the “Name” variable to be sent to UMG. I believe this is due to the engine can’t identify from which actor it should get the variable from before it is spawned.

  7. Even when I try to retrieve the reference from the spawn return node it simply ask gave me all possible “Name” variable I have to chose from all actors.

  8. I also don’t want to make super long list of actors and cycle thru it manually as it won’t be efficient especially when I plan to update or expand the list. If possible I wanted it to be handled by 1 gamemode.

I hope this time it’s clear.

Sorry, i am pretty new to unreal engine but i got a similar project where i need to spawn actors and get/set information.
Are they in the same actorclass?
If they are and you do a “get all actors from class” and from the “get” you could set a random integer in range. And the range max would be the length of the actor array. And when the actor spawns you need to have information already in the actor so you can get variables from it , for example the name and mesh and do stuff with it such as setting the variable as text for your umg.

Then next time you click you could hide the actor or destroy it and spawn the next one and do a index check so it doesn’t spawn the same actor in that clasd.

Maybe this is a faulty method but that’s how i’ve done.

Hi thanks Muntrax. Appreciate your help. I do able to spawn the actor randomly. However the difficulty is to get the variable from it.
What do you mean by this “And when the actor spawns you need to have information already in the actor so you can get variables from it”?
The variable was stored on actor class BP. Is that not supposed to be done?
Thanks again.

You can search for that specific variable in the blueprint even tho it is not in the same actor blueprint.
And if it is in another blueprint then you need an actor reference for that variable so that the blueprint can communicate.
and the actor reference is the actor you just spawned.
so if you have your SpawnactorBP you search for the variable-name from the actorClassBP.
Are you sure you have created the variable? It should show up without context sensitive.

From the spawn actor node where you add unique. Make it a variable first and name it something like “spawned actor”.
Then in the foreachloop you can do a ==equal to the array element. And make a branch and if it is valid you break the loop and set the index to a variable.
Now you now the actor index in the actor class.

From the return value of the spawn actor, it is called “promote to variable” and it will set you up

Hi Muntrax, thanks again!
It does works.
Ok I’m sorry but I have next stupid questions. It means if I have i.e 50 possible actors I have to make 50 “equal” checking? Also how this will work if I wanted to spawn 2 or more random actor at the same time? For quiz mode where player needs to tap on the shapes with correct name.
Well thanks anyway. If my next questions is too troublesome just ignore it I think I can figure it out myself from there.

You should not have to do that… uhm. Maybe add the used actors to an array and do a index loopcheck when you spawn new actors. And if it is false then spawn the new actor and add it your usedActorArray.

I would do 2 spawn actor functions but with the same mechanism to make sure wich has already spawned.