Tumgik
#TSubClassOf
ue4journey · 2 years
Text
Blueprints to C++ : Day 37 of 42
Class References and TSubclassOf<>
https://www.youtube.com/watch?v=TVb9yTRpB-o
Create widget and add to viewport
https://www.youtube.com/watch?v=lXgoEcm9Qvk
UMG Native Construct and Button Function Binding
https://www.youtube.com/watch?v=Y1QzMsCT68M
18 notes · View notes
askagamedev · 4 years
Text
FANTa Update #1: Getting Unreal
I’ve started building FANTa on Unreal, doing some basic setup. We’ll be building on the foundation we lay here in the coming weeks and months. I’m still looking for a repository big enough to handle the FANTa file sizes. I’ll think of something. I’m also trying to strike a careful balance between doing specific things and being more general for speed and brevity’s sake. If you want to follow along in the meantime, here are the steps to get to where I currently am.
EDIT: Forgot to mention - you’ll need a C++ editor and compiler for this. I suggest Microsoft Visual Studio 2019 Community Edition. [Get it from Microsoft for free]. If you really don’t like Visual Studio, there’s some additional information on [setting up a different IDE here]. Remember, you can always compile your C++ code by hitting the Compile button in the editor.
We’ll start by creating a new Unreal 4.24.2 project. Under Games, choose the Third Person template, then select C++ instead of Blueprint. We’ll still be doing most of the work in blueprints, but we need C++ in order to work in the Gameplay Ability System that we’ll be using for the traversal mechanics and such, like so:
Tumblr media
(I’ve already created a FANTa project, which is why it says it already exists)
Tumblr media
Optionally, you can then go to the Unreal Marketplace and add a Paragon character to your FANTa project. The AAGD Patreon Dev Team voted in Terra for our player character model, so that’s who I am using.
After you’ve gotten the project created, you need to set up the basic Gameplay Ability System in C++. [Click here for the instructions to do so]. Make sure you have the Gameplay Abilities Plugin enabled, and make the core changes to FANTaCharacter.cpp and .h. You really only need to make the changes up until the Binding to Character Input section.  Find the ThirdPersonCharacter Blueprint and you should see the AbilitySystem inherited after making and compiling the C++ changes. Like this:
Tumblr media
We’ll rename ThirdPersonCharacter to PlayerCharacter, since that’s probably a more accurate name for it.
For fun, we can also change the blueprint’s Skeletal Mesh to Terra (or whoever you decided to choose) and the Animation Blueprint to Terra_AnimBlueprint (use the dropdown menu to choose) and it should swap the mannequin for our big beefy axe lady. Compile and save everything.
Now we need a Gameplay Ability to give to the player character. Let’s start by creating a Blueprint which inherits from Gameplay Ability like so:
Tumblr media
Name the GameplayAbility blueprint something like DebugTestAbility and open it up. We just want something for us to make sure it is being activated properly. Event ActivateAbility should just call PrintString with some message to indicate that the ability has been activated, like this:
Tumblr media
Next, we need to add a little bit more code. We want to add a way to grant abilities to a FANTa Character and we want it callable via Blueprints, so I wrote this bit in FANTaCharacter.h under public:
UFUNCTION(BlueprintCallable, Category = "Abilities") void GiveAbility(TSubclassOf<class UGameplayAbility> AbilityToGive);
This declares a new class function called GiveAbility that is callable by blueprint that takes a Gameplay Ability subclass as a parameter. Then, in FANTaCharacter.cpp, we define the function as so:
void AFANTaCharacter::GiveAbility(TSubclassOf<class UGameplayAbility> AbilityToGive) {    if (AbilityToGive)    {    AbilitySystem->GiveAbility(FGameplayAbilitySpec(AbilityToGive.GetDefaultObject(), 1, 0));   } }
This means that we can call GiveAbility from any blueprint that inherits from FANTaCharacter with a class definition for an ability and it will give that ability to that FANTaCharacter. Build it and it should be good.
Now, we need some way to make sure that this ability has a tag so we can activate it by that tag. In the Details of DebugTestAbility, edit the Ability Tag and add a new tag Debug.Test.DebugTestAbility to identify this ability in specific.
Tumblr media
This should be enough to set up our debug test ability.
Now we have to give this ability to the player character. Go back to the PlayerCharacter blueprint and create a new variable called StartingAbility. The variable type will be Gameplay Ability Class (not object). Then, in Event BeginPlay on PlayerCharacter, give that ability to the player character like so:
Tumblr media
You also need to set a class default for this ability - use the dropdown menu and choose DebugTestAbility for StartingAbility. This should be enough to give the debug test ability to the player character.
Finally, we need to put in a way to actually activate the ability. Open up your Project Settings and go to Input. Add a new action mapping and call it UseAbility1. Assign it to something. In this case, I’ve I’ve assigned it to a gamepad button.
Tumblr media
Now... in the PlayerCharacter blueprint, we’ll put in a quick test function for this. Add a new Event InputAction UseAbility1, and for “Pressed” you can put a printstring (to make sure the input is being registered), followed by TryActivateAbilitiesByTag. The Target should be the inherited Ability System Component. Then you just need an array of gameplay tag containers. We do that by doing MakeLocalGameplayTag (choosing the tag we added above - Debug.Test.DebugTestAbility), MakeArray on it, then MakeGameplayTagContainerFromArray, and feeding that into the GameplayTagContainer input field on TryActivateAbilitiesByTag.
Tumblr media
Compile your blueprints and run. You should be able to move around and pressing the UseAbility1 button should print a string to the screen that matches what we set in DebugTestAbility.
Congratulations everybody, FANTa has begun (again). We’ve set up our character and our gameplay ability system with one working ability! Future posts will be a little less in the nitty gritty as we become more familiar with the UE interface and I’ll focus more on the principles of what we’re doing rather than the exact details of the implementation. If you’re having trouble getting set up or following, please ask in #fanta-discussion on our discord server! I also want to thank all of our patrons on Patreon for helping to make this possible. We really appreciate your support.
[Join us on Discord] and/or [Support us on Patreon]
We’ve started the FANTa project in Unreal. [What is the FANTa project?]
Got a burning question you want answered?
Short questions: Ask a Game Dev on Twitter
Long questions: Ask a Game Dev on Tumblr
Frequent Questions: The FAQ
39 notes · View notes
selfhowcom · 5 years
Link
[UE4] Programming - TSubclassOf by 베르의 프로그래밍 노트
0 notes