Runtime generation of actors

I’ve started again and added the code into my gamemode.cpp instead. No separate cube class. The header file is fine and the constructor seems fine, however I’m getting the errors you see below. That AStaticMeshActor doesn’t exist (and obviously NewCube doesn’t but that’s because of the previous error). Any suggestions or anything to say on where to go from here?

AStaticMeshActor* NewCube = GetWorld()->SpawnActor( FVector::ZeroVector, FRotator::ZeroRotator );

I just did this. It compiled file. Did you include your project header file in the cpp?

Edit: Also, use the output window, not error window, when looking for errors.

Yeah my project header file is included. i’ll show you.

75276-unreal+header+files.png

these are my errors in unreal after trying to compile it:

No idea tbh. :frowning:

haha welcome to my world. Errors occur where there are no errors for everyone else. should probably quite coding haha

shall i try just starting a new project?

You could give it a go.

quick question, which version of the engine are you using?

Tried it in 4,8 and 4.10.

cool, did you use a function you created or BeginPlay() to have the spawning functions and params?

I added to a random function in an actor.

How did you do that? I can’t seem to access any any of the static mesh actors in my editor

when you say you added a random function in an actor did you create an actor yourself or did you access the code of a static mesh actor because if it’s the latter I can’t seem to do that?

I added it to a random function in a random class. It wasn’t even an actor, game instance, I think.

Have you included the StaticMeshActor class?

#include “Engine/StaticMeshActor.h”

Put that at the top of the header or source file referencing AStaticMeshActor

I tried adding this line to my game mode header file and it “kills” the entire thing. By this I mean it starts leaving red lines under all the lines of code that came with the cpp and header files as standard. Yet if I include it in the cpp file it accepts it

Ok so after adding the include you suggest nedwardsnae and the code you suggested TTaM (massive thanks to the both of you :slight_smile: ) the project now crashes every time I try to run it. I could try showing you the code if you’d like and see if you get the same thing or see if you can spot where it might crash?

Ok guys good progress. I’ve altered the code to perform a check to see if the world is there or not and run the generate cube code. everything compiles and in the world outliner the cube appears, however in the game itself there is nothing in the location i specified. any ideas?

found the problem but still need a solution to it. when i click on the static mesh in the world outliner there’s not static mesh assigned to it even though i do that in the code

Have you tried placing a breakpoint in your code at the point where you assign the static mesh to see if it actually does it? Might be helpful if you reposted your code again so we can get a better sense of where you’re at too.