Swapping from Unity to Unreal 4 – pt1
I’m just going to throw down some questions, solutions and other junk here as I go.
I’ve found that Unreal 4 has a far higher barrier of entry than Unity, due to how new it is as a platform it just doesn’t have the wealth of information about it that Unity does. I’ve found many posts from people asking questions met with inconclusive answers or wrong answers or in the sadder cases no answers at all. I never found this when searching for Unity things, there were always dozens of questions asking exactly what I was, each with fantastic quality answers to everything.
So to help with this I’m going to try and document some of my struggles in the interest of clarifying things for myself and hopefully being corrected if I’ve assumed something wrong.
A lot of my initial concerns came from how objects in UE4 compare to objects in Unity.
I think Actors in UE4 is roughly analagous to the GameObjects from Unity, because everything inherits from classes like AActor, there’s nothing in a blank script like OnStart() or Update() like Unity has, you have to go look up what’s available. And credit to Epic, they have all that documentation, but there are so many functions it’s just a little overwhelming.
I spent quite a while finding the analogue to OnStart()
AActor derived classes can override BeginPlay(), BeginPlay() is called for any AActor derived class as soon as it spawns. But there’s another cool thing that you can do with the overridden functions…
The AGameMode class is used for every level, and is used to define general things about that level or type of level, but it also has a function StartPlay() which is what does the very first call to BeginPlay() for all the actors currently in the level. This is super useful in combination with
Super::StartPlay();
since you can put code before or after this to get it to run before the game starts or after all other actors start…acting. Putting this at the end means everything in your gamemode’s StartPlay() will run before any other actors start acting.
Next was how to spawn something…anything…
I already knew everything needs to be derived from AActor (APawn is another common one and derives from AActor but is used for things that can be controlled by human or AI players, ‘possessed’ by them in the lingo of UE4).
In Unity, you can instantiate an object with
GameObject wall; // usually set by editor serialisation
GameObject newwall = (GameObject)Instantiate( wall );
and, Unreal uses a UWorld class which seems to act as a hub for general things and manages the actors in the level
FActorSpawnParameters SpawnParams;
AWalker* const FirstWalker = (AWalker*)GetWorld()->SpawnActor(
AWalker::StaticClass(), SpawnParams );
Weirdly enough AActor doesn’t have a transform. So to get your spawned object to have a position or rotation you need to give it a USceneComponent. Every actor needs(should have?) a root component, since the components an actor has are essentially the heirachy of things an actor has. This is where I’m getting to the edge of my knowledge, so I’ve made the SceneComponent the root component of my walker (so far that’s all it’s got, but that may change, who knows!?!)