No announcement yet.

Spawn random actor

  • Filter
  • Time
  • Show
Clear All
new posts

    Spawn random actor

    So basicly for my first ever project in Unreal Engine i followed 2D side scroller tutorial ( ) which was excelent kick off point for what i wanted to do.

    Setup is simple. Player is moving left and right inside fixed area catching "good things" and evading "bad things". For now i have main menu, first level, player, one good thing (apple) and one bad thing (rock). Apple adds to score, and rock removes life. When life hits zero, game pauses and menu opens where you can restart or quit.

    Apples and rocks are actors that randomly spawn inside spawner volume (position). Timing is random too. Apple has it's own loop with random pause and Rock has it's own loop with its own random pause.

    Idea is to add few more good and bad items. By default i could copy and paste current loops for apples / rocks and just add new items. But that would mean that apple and icecream could spawn at same / similar time, which is exactly what i do not want. What i want is that good things spawn in one loop and bad things in another. When loop gets to point where it needs to load actor, id like to somehow pick one of actors and load it.

    What i don't know is how to make a list of "good" actors and use that list to load em randomly.

    I tried variable that holds "actors" in array, but i can not pick any actors as they are not in scene (they are loaded by spawner), i tried normal "text" array with name of actor classes, but i dont know how to load actor only by class name etc.

    Basicly i need advice how to deal with it?
    Last edited by stjepan21; 07-27-2017, 02:52 AM.

    Instead of making the spawning logic self contained within each of your spawning actors, you should have a main actor spawning class that spawns everything. Inside that class, make an array that contains everything you want to spawn and pick a random actor from the array to spawn.

    If you need me to elaborate more on this, let me know.


      I did get desired effect without arrays, simple by adding into both spawning loops something like this : custom event->random int->switch by int->create random pause->load actor->call event again
      It looks like this:

      Click image for larger version

Name:	Screenshot_25.png
Views:	1
Size:	77.7 KB
ID:	1131500

      It's good enough for my little learning project, but i realize that it would be extremely bad for any kind of project that would have more than few of each "enemies" and "rewards", so yes i would appreciate if you could explain whole array thing to me.

      I know how to create variable inside blueprint. I know how to make that variable an array. I know how to save simple numbers and or text inside that variable or change type of data that variable can hold (int, string, text, etc.). I know how to access data from that variable.

      What i dont know is what kind of variable type does it has to be for me to be able to create list of all blueprints of actors (me inserting names of those blueprints manualy or some kind of automation is all the same to me right now) so i can call them later on. My first instinct was "Paper Character" type. After compiling blueprint in which variable is created i tried adding default data to it, but drop down list was empty, as none of actors were in scene (they are loaded by spawner and yes, i created that variable inside spawner blueprint).

      So if you could tell me what kind of variable type should it be so i can save names of actor blueprints and be able to call em later on i would appreciate it as it would help me in my future testing and works in engine as i really really like what i did by now and i would like to continue learning and eventually start serious work.
      I did try with "text" and similar types, but i dont know how to call blueprint by name or any other similar way.
      Last edited by stjepan21; 07-25-2017, 05:19 AM.


        What I would do is make a "Spawnable" Parent Class from which all of your enemy and reward actors inherit. Then you create an array with the Spawnable Class as the type (the purple one). For example:

        BP_Weap_Base Class is the variable type. When I add a new element to the array, it only lets me select the base class or classes that inherit from it.


          How exactly did you create BP_Weap_Base class so it ends up being purple? Every one i create ends up being light blue once i pick it as a variable type. Can it even be done trough blueprints or i need some c++ magic?


            Ok i found it by randomly looking at ways to create array of classes.
            This image gave me a hint:

            Click image for larger version

Name:	130692-minerals.png
Views:	1
Size:	58.9 KB
ID:	1131569

            I had no idea i can go to that "list" on the side of the Actor and pick.

            Thanks for all the help. With this under my belt i think i have it now.