Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.19
more ▼

asked May 09 '18 at 01:56 PM in Blueprint Scripting

avatar image

3 1 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered May 09 '18 at 02:17 PM

avatar image

10.1k 33 9 23

avatar image Jempol_lele May 09 '18 at 02:49 PM

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?

avatar image Everynone May 09 '18 at 04:41 PM

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 ;)

avatar image Jempol_lele May 09 '18 at 09:49 PM

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.

avatar image ThompsonN13 May 09 '18 at 10:48 PM

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.

avatar image Jempol_lele May 10 '18 at 01:20 AM

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.

alt text

alt text

Then the gamemode will randomly pick one to spawn.

alt text

After spawn the game mode creating widget to display the name of the random shape chosen.

alt text

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?

cube.png (101.1 kB)
1.png (74.4 kB)
gamemode.png (106.7 kB)
avatar image ThompsonN13 May 10 '18 at 03:15 PM

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.

alt text

capture.png (305.3 kB)
avatar image Jempol_lele May 11 '18 at 11:58 AM

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.

alt text

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?

alt text

Thanks a bunch!

try.png (175.3 kB)
240549-capture.png (265.2 kB)
avatar image ThompsonN13 May 11 '18 at 03:12 PM

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.

avatar image Jempol_lele May 12 '18 at 07:47 AM

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.

avatar image ThompsonN13 May 12 '18 at 03:23 PM

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

avatar image Jempol_lele May 12 '18 at 04:44 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question