Download

How can I spawn an actor sphere without bluprints?

Hey guys, I have been looking for quite a while on how I can go about spawning an actor for unreal 4.8 and I cannot find anything. Can someone please explain how to do it? I don’t know the diffrence between a “CreateDefaultSubobject” and was trying that for a few hours.
Thanks,

Hey, to spawn an actor in C++ you can use the following code (change it for your needs).



FActorSpawnParameters SpawnParams;  // You can add SpawnParamater similar to the one i did below. There are a few more!

SpawnParams.bNoCollisionFail = true;

// You can either spawn them only with GWorld, or use a Pointer to directly save the reference to it. 
// Make sure the Class you spawn, the Template Parameter <..> and the Item Type do match

// So, "YourClass" is the Name of the Class i'm spawning here. You can change "FVector" and "FRotator" with the SpawnPoint and Rotation you want to use
YourClass* Spawned_Actor = GetWorld()->SpawnActor<YourClass>(YourClass, FVector, FRotator, SpawnParams);


But i have no idea how to spawn a Sphere with that. If you have the Sphere Class, you can just use it for the “YourClass”.
You may want to look up how to get the Sphere Class. :X

Question:


YourClass* Spawned_Actor = GWorld->SpawnActor<YourClass>('I don't know what goes here', FVector, FRotator, SpawnParams);

i don’t fully understand the hierarchic of unreal, are the classes instances of actors? In unity you make a “Prefab” and then you can add a class to it, but is it the other way around for unreal?
Thanks!

UE4 is totally diffeent from Unity in how they handle compoenents

In unity a prefab is a “GameObject” and we use


 Instantiate(Prefab,Location,Rotation) 

to spawn it

In UE4 an equivalent (Not Really) of GameObject is an “Actor”

Exemple you can create a new Class MyActor derived from AStaticMeshActor
AStaticMeshActor = AActor + UStaticMeshCompoent

FVector Location;
FRotator Rotation;


GetWorld()->SpawnActor<AMyActor >(AMyActor ::StaticClass(), Location, Rotation);

if you want a similar way to how you do in unity , you can fill “MyClass” in the editor with a Blueprint Actor for exemple


.h
UPROPERTY(EditAnywhere)
UClass* MyClass;


.cpp
GetWorld()->SpawnActor<AActor>(MyClass, Location, Rotation);

(PS: You may find some errors here , I’m using my phone)

No, other way round. A Class is the “template” to create lots of actors. For example you have a Class for your Character. It has all the Variable and Functions as well as Blueprint Components
that your PlayerCharacter should have. Then you can spawn multiple actors/instances of it.

In Unity, you do this differently and i highly recommend to stop thinking in Unitys kind of way. I also did this and this is just not how UE4 works.
You will not add classes or C# Scripts to a prefab but just put the whole logic into the C++ Class.

You may want to learn C++ a bit first before entering UE4. Then it makes much more sense. This is not a UE4 setup, this is basic C++ (:

EDIT: This will help you, although i would not say that Prefabs are Blueprints. Blueprints are just Visual Scripting ChildClasses of C++ Classes.
C++ ChildClass is no difference to a BP Child Class. Just that you are working a bit more limited and in a visual editor. So not really a prefab
thing like in Unity:

https://docs.unrealengine.com/latest/INT/GettingStarted/FromUnity/

Thatnks! So I understand that, but how does MyClass become an actor?
so basically what I am being told is I want to spawn a class that “Holds” an actor to it? Why don’t I just spawn an actor?

The Engine is spawning it at the given “FVector” position and with the given “FRotator” rotation and with the given “FActorSpawnParameters” like i showed you above.
The function to spawn it is part of the UWorld object. That’s why you call “GetWorld()” and from that pointer you call “GetWorld()->SpawnActor…” etc.
This function will return a pointer to the spawned actor that you can save to a variable to use it later on. Just like i posted it in my post above.

(Note: I accidentally wrote “GWorld->” instead of “GetWorld()->”. Sorry about that! Fixed it up there in my first post.)

Thanks so much and sorry if I’m ******* you off :stuck_out_tongue:

So this is what I have:



.cpp:
FActorSpawnParameters SpawnParams;  // You can add SpawnParamater similar to the one i did below. There are a few more!
SpawnParams.bNoCollisionFail = true;
FVector NewLocation =  FVector(0.f, 0.f, 300.f);
ASpawnFactory* Spawned_Actor = GetWorld()->SpawnActor<ASpawnFactory>(MyClass, NewLocation, FRotator::ZeroRotator, SpawnParams);

.h:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Pickup)
UClass* MyClass;


I am not familiar with how I can turn MyClass into a sphere, that was the question I was asking. Do I have to make a Class with an attached component in unreal editor? Do I have to make a Class and use a mesh component in the c++ attached to it? I am confused as to how I make “Meshes” in unreal. I am not confused about the logic.