Presentation is loading. Please wait.

Presentation is loading. Please wait.

Editable Attributes (1)

Similar presentations


Presentation on theme: "Editable Attributes (1)"— Presentation transcript:

1

2 Editable Attributes (1)
Whenever we add an object to the scene (whether that’s an Actor or a Pawn) We can edit its properties in the Details Panel When we made variables in a C++ class before, they were only accessible in the C++ class itself

3 Editable Attributes (1)
However, we can change that We can make these variables visible in the Unreal Engine Editor There are two places we can make a variable visible In a Blueprint In the Details Panel We can do one, the other, or both

4 Editable Attributes (1)
To test this out, we’re going to make a C++ class in a C++ Unreal Engine project This C++ class will represent a MovingPlatform We’re going to give it points to move between in the editor

5 Editable Attributes (1)
The first thing we need to do is get our class set up with attributes One for the location to move to Another for the duration of its movement The idea is that this platform will move between this location and its starting position With speed based on the duration we’ve given it

6 Editable Attributes (1)
We’ll make the two variables here The location is a FVector Which stores the X, Y, and Z components of the location

7 Editable Attributes (1)
However, just adding these variables isn’t enough We need to flag to Unreal Engine that these variables are usable in the editor That’s done using macros

8 Editable Attributes (1)
Macros are small function-like bits of code we can use literally whenever we want Usually above a variable, function, or class Different macros will do different things Unreal has a bunch of them we can use

9 Editable Attributes (1)
We’ve already seen one of them GENERATED_BODY() Which makes the class usable in Unreal Engine We need to use another if we want to use these variables in Unreal Engine

10 Editable Attributes (1)
The one we’re using is UPROPERTY We put these above the variables we want to include in the editor UPROPERTY macros have quite a few arguments we can include in them Which change how Unreal Engine uses this variable

11 Editable Attributes (1)
The following arguments can be added to the UPROPERTY macro (to change how Unreal Engine uses it) EditAnywhere: the attribute can be viewed and changed in the Details Panel EditDefaultsOnly: we can change the attribute in the Details Panel, but only before running the scene VisibleAnywhere: like EditAnywhere, but we can’t change its value BlueprintReadOnly: can be used in a Blueprint, but not written to BlueprintReadWrite: can both read and write to the attribute in a Blueprint Category: used to put the attribute in a specific category in the Details Panel

12 Editable Attributes (1)
We’ll stick with one mixture of these for all our attributes This means we can edit it both in the Blueprint and the Details Panel Under the menu “Menu Name” We’ll change that depending on the attribute UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Menu Name”)

13 Editable Attributes (1)
Let’s add that macro above both of the variables we made

14 Editable Attributes (1)
Then we’ll compile the C++ class And make a Blueprint for it

15 Editable Attributes (1)
When we add this Actor to the scene and select it We’ll see those variables in the Editor!

16 Editable Attributes (1) Ex
Make a new Empty C++ Unreal Engine project Create a C++ class called MovingPlatform Give it two variables: the location to move to (FVector), and the duration of the move (float) Add the UPROPERTY macro above both variables so that It can be edited anywhere Can be read from and written to in a Blueprint They are put in the category “Platform Setup” Compile your C++ file and make a Blueprint inheriting from it Add one to the scene Make sure you can edit those two variables

17 Editable Attributes (2)
We can give these values default values if we wanted to Those values will appear in the Details Panel Until we change them to different values We do that from the constructor

18 Editable Attributes (2)
In the constructor we can set the value for the duration Can’t do location As we don’t know where this Actor is in 3D space before it’s been added to the scene

19 Editable Attributes (2)
When we compile this and add another of these Actors to the scene Its duration is now the default value we gave it

20 Editable Attributes (2) Ex
Give the Actor a default duration in its C++ constructor Compile the C++ code and add another Actor to the scene Make sure its duration is the same as the default you set in the constructor

21 Making the Platform Move (1)
With the attributes set up, let’s look into making the platform move We’re going to make the platform move only to the destination for now We’ll make it move back again afterwards

22 Making the Platform Move (1)
For this, we need to store its starting position somewhere We’ll make a variable for it Then we’ll set it in the BeginPlay function As we know at that point where the Actor is Then we’ll use the duration to work out how much to move between the start and end location every Tick

23 Making the Platform Move (1)
Let’s add that variable first We don’t need to make this a UPROPERTY As only the C++ file will use it We’ll also add a variable for the current movement time

24 Making the Platform Move (1)
Then, in the BeginPlay function, we’ll give this a value Specifically, the current location of the Actor And we’ll set the current movement time to 0

25 Making the Platform Move (1) Ex
Add two private variables to your C++ class One for the starting location (FVector) One for the current movement time (float) In BeginPlay, give the starting location the current location of the Actor And set the current movement time to 0

26 Making the Platform Move (2)
There are a few ways we could make our platform move For example, we could calculate the distance between the two points Along each of the axes Then calculate the speed based off the duration

27 Making the Platform Move (2)
However, Unreal Engine can take a lot of the trouble off our hands By using the Lerp unction Lerp stands for Linear Interpolation It lets us change a value, between two values, over a period of time

28 Making the Platform Move (2)
So, we could Lerp between the start position and the end position Imagine these positions as 0 and 1 Lerp takes a third value – an alpha – to work out what value to return between these two points

29 Making the Platform Move (2)
Imagine our positions were 10 and 20 An alpha of 0 would return 10, and an alpha of 1 would return 20 Any alpha in between would return a value between them So 0.5 would return 15

30 Making the Platform Move (2)
So we know what the two values for the start and end will be But what about the alpha? That’s where the duration and current movement time come in If we divide the current movement time by the duration, we’ll get a value ranging from 0 to 1 Lerp doesn’t care about going over 1

31 Making the Platform Move (2)
Let’s use this FMath::Lerp function in Tick Where we’ll give it the three needed values First the starting position Then the end position Then the current movement time divided by the duration

32 Making the Platform Move (2)
This isn’t going to do anything unless we increase the current movement time So we’ll add the delta time to it

33 Making the Platform Move (2)
That’s all we need to do! Let’s compile this and give the Actor a position to move to

34 Making the Platform Move (2)
When we run the game, we’ll see the platform move!

35 Making the Platform Move (2) Ex
In the Tick function of the C++ class, use the FMath::Lerp function to return a value Use the starting location as the first argument Use the end location as the second argument Use the current movement time divided by the duration as the third argument Set the Actor’s World Location to the value returned by the Lerp Then increase the current movement time by the delta time Compile your C++ class and run your game You could see the Actor move!

36 EXTRA: Make the Platform Move Back
Can you think of a way of making the platform move back when it reaches the end? Feel free to add as many variables as needed for this Here’s a hint: Lerp works forwards and backwards

37 EXTRA: Make the Platform Cycle
Now that it moves backwards, can you make the platform cycle back and forth You’ll need to make only a few more adjustments to get this working OPTIONAL: Once this is done, can you add the number of times the platform cycles as an editable attribute?

38 END OF UNIT!


Download ppt "Editable Attributes (1)"

Similar presentations


Ads by Google