How to set a variable of an AActor before it is spawned by using Textbox input?

Hi all!

I have a small 3d game in Unreal Engine and in the default level, there is an actor called SpawnedVolume. Its purpose to spawn the enemies with the specified number and time delay. I would like one of the textboxes in the menu of the game to set the maximum number of enemies. This variable is in the SpawnVolume but the SpawnedVolume is only available after the level is opened. Secondly this class is a simple AActor, so it is not a ACharacter. In the case of an ACharachter, AFAIK I could use “Spawn AIFrom Class” node.
I was also thinking about if it is possible to reach the texbox content from the contruction script of the SpawnVolume class.

I am interested in C++ and blueprint solutions as well.

Thank you!

You could spawn the actor with desired parameters:


I hope it’ll be helpful for you.

My Products

1 Like

If I use the Spawn Actor Spawn Volume node, I have to give the location as well. But the location is specified by dragging and dropping the actor in the desired location in the editor. I don’t know if there is a better solution.

I tried to save the textbox input into a variable of the GameMode blueprint, however when I tried to get this variable in the construction script or event graph of the SpawnVolume blueprint, it always give me zero. Below you can see some screenshots about this situation. The first image shows, when I am saving the texbox input variable and the second, when I would like to use it to set the enemyToSpawn variable of the SpawnVolume Blueprint.

I’m not sure I entirely understand what you’re wanting to do, but I think what I’d do is have the text-box entry affect a value in my GameMode or GameInstance (depending on what lifetime this value needs to have) - whenever I change that value have it go to the Game, and then you grab it whenever you need it in the Spawner.

Doing almost anything in the “Constructor Script” part of a Blueprint is very, very rarely the correct answer, as the item doesn’t exist in the world when Constructor is called, so you don’t have access to anything, and a lot of things that you might think about doing there will just break things worse. BeginPlay is almost always the correct place to do startup-only logic.

1 Like

Yeah, this what I want to do at the moment. I save the textbox input into the variable of GameMode blueprint with a setter and get it in the event graph of the SpawnVolume and then set the maxEnemy variable.
When I save textbox variable and print out the variable it is ok. Hovewer, when I would like to retrieve the value from the GameMode blueprint it always give me zero.

Does the Gamemode blueprint “forget” the value of the variable between the two event graphs?

no, but it would help if we could see the code in question :slight_smile:

1 Like

I attached some screenshots. :slightly_smiling_face:
The first two is for the MainMenuWidget_BP, where the textboxes appear. The third one is the eventGraph of the SpawnVolume. The SpawnVolume_BP is inherited from a C++ class, just like the MainMenuWidget_BP.

I didn’t find the solution, but I realized to use the cast to Soldier3DTDGameMode_BP instead of cast to Soldier3DTDGameMode , because that is what I am using in the project as a default gamemode.

Somewhy it seems, that there are two different instances of gamemode BPs.