Why can't I spawn actors from within construction scripts?

The spawn actor from class node is not available in a construction script. Is there a way of doing something similar?

you don’t want to spawn actors inside a constructor because it’s called in the editor when you drop it as well as in the game. If you want to create actors based on a constructor try adding a child instead with AddChildActorComponent. That will create a new Actor that is a part of the current blueprint and thus attached.

If you want it to spawn something different like a bot that is independent of the blueprint use the event begin play node in the event graph.

You might have to uncheck Context Sensitive.

I don’t understand the reason “because it’s called in the editor when you drop it as well as in the game”.

Construction scripts are run within the editor, even when the game is not running (they are also run when the Blueprint is loaded at runtime/in-game). Every time you compile a blueprint or move a blueprint in your level, its construction script is run. Bad things would happen if you could spawn actors that way.

Instead of putting it directly in the Construction Script, add it to a function and call the function in the construction script.

actually this is exactly what i am looking for i want to be able to run scripts when creating the level in the editor before running the game so these scripts would be only for before running the game or to be more clear before entering a level (for the instance of creating it randomly in the game like clicking generate level in the game it will generate it and then the player can enter it )

1- is there any way to achieve the above?

to be more clear i am creating a modular level building system i am currently as you have pointed out using add child actor and it does work to some extent but
is there any other way to run a script say spawning the next piece of level when we want say by making a bool set to true inside the view port for example i give it the spec i want and i make “create” (a bool exposed to the view port) set to true and so the script runs and makes what i wanted it to (off course i could easily achieve the run code with construction script using a branch but i wouldn’t be able to spawn any actor with it so it would be useless)

i should state that all the process i am trying to achieve is before run time when i am making the level after made the scripts will be deactivated and the level will be there as if i have placed everything manually in the places

2- is there a work around for what i want to achieve?

3- on the basis of continuing my current solution how much will adding child actor components each with there own customized setting effect performance in comparison to having each of those child actors separate actors in the game ?

I’m really sorry, but can you punctuate your comment? I’m dyslexic and my brain just cannot process it. Then I will answer! thanks :slight_smile:

Im doing it like this:

Is this what you are trying to achieve?

Result, from default value:

If one’s calling a function (which is calling Spawn Actor inside itself) from the Construction Script, then a warning message will be logged:
“LogSpawn: Warning: SpawnActor failed because we are running a ConstructionScript”
, and the Construction Script will fail to do what you want it to do.
So Spawn Actor and Construction Script cannot go together.