top of page

Adjust Vector (Blueprint node)

WHAT ARE WE TRYING TO DO HERE? Updating a single value in a vector and passing it along to the next node is really common in unreal blueprint graphs, but it takes up a lot of space. Also, it takes time to place the nodes and connect the wires. You can make a Macro of it, but it's better to put it into a blueprint function library, so you can get it anywhere. Insythor provided some nice condensed, compact nodes for getting and setting the vector elements (and rotator versions too) and I reverse engineered or emulated these in my own C++ function library. Why go to the trouble, if I already have that? Because I don't want to depend on a marketplace plugin getting updated with each unreal version release. The granular nodes (Location.Z) getter and setter are shown in the second part of the image below. These are cool, but then I thought these could be one node.

The image below shows the progression. The top is what comes in unreal by default. [You can also split struct pins on the getter variable, but still, it's wiry].



In the bottom version, it's one node -- I set it up just to see how easy the goal could be reached. Using enums for the vector elements (X,Y,Z,ZY,ZX,YX,XYZ) as a dropdown is nice. Using Math enum for (Add,Subtract,Multiply,SaveDivide) is also nice.


But the node size is still quite chonky because the Enums, when set on the node, take up a lot of space.


When set with variable getters, it's more pretty, but that has less utility unless you have a lot of Vars in your BP (well, that's valid I suppose). Anyway, how great it would be if unreal could be updated so the Enum dropdown padded to its content instead of WAAAAY longer than necessary? Note that the node is using a compact title. This gets hidden by Enum dropdowns, so in this case I put the enum variables at the top and bottom. It would be nicer if unreal let some visual styling be done on the compact node title, to make it align to top instead of center, or align to left or right.


My solution(?) was to ensure the order of input variables keeps the Enum type input pins out of the way of the node label.



HOW TO DO THIS?

Here is the implementation for the blueprint node, added in a "FunFunctionLibrary" C++ class. Note that the relevant info is pasted in from my ongoing set of functions, so it may not be totally reliable as is. Also, note, this is 1.1 as I had to account for doing math with bitmask enum values.

FunFunctionLibrary.h

#pragma once

#include "CoreMinimal.h"

#include "Kismet/BlueprintFunctionLibrary.h"

#include "FunFunctionLibrary.generated.h"


UCLASS()

class UFunFunctionLibrary : public UBlueprintFunctionLibrary

{ GENERATED_BODY()


/* The vector enum (a drop-down on a BP function input pin) to pick combinations of components of the vector struct (X,Y,Z) */

UENUM(BlueprintType)

enum class EVectorComponents : uint8

{

None = 0x00, // 0000

X = 0x01, // 0001

Y = 0x02, // 0010

Z = 0x04, // 0100

XY = 0x03, // 0011 (X | Y)

XZ = 0x05, // 0101 (X | Z)

YZ = 0x06, // 0110 (Y | Z)

All = 0x07 // 0111 (X | Y | Z)

};


// Struct for flags to be considered with bit usage correctly.

struct FComponentFlags

{

static constexpr uint8 None = 0x00;

static constexpr uint8 X = 0x01;

static constexpr uint8 Y = 0x02;

static constexpr uint8 Z = 0x04;

static constexpr uint8 XY = X | Y;

static constexpr uint8 XZ = X | Z;

static constexpr uint8 YZ = Y | Z;

static constexpr uint8 All = X | Y | Z;

};


//The math operation as an enum that we use on a simple cases (like Float plus Float)

UENUM(BlueprintType)

enum class EMathOperation : uint8

{

Add UMETA(DisplayName = "Add"),

Subtract UMETA(DisplayName = "Subtract"),

Multiply UMETA(DisplayName = "Multiply"),

Divide UMETA(DisplayName = "Divide")

};


public:


// Helper function that does the maths using the EMathOperation enum

static inline float AdjustByOperation(float Component, float Value, EMathOperation Operation) {

switch (Operation) {

case EMathOperation::Add:

return Component + Value;

case EMathOperation::Subtract:

return Component - Value;

case EMathOperation::Multiply:

return Component * Value;

case EMathOperation::Divide:

return (Value != 0) ? Component / Value : Component; // Handle divide by zero gracefully

default:

return Component;

}

};


/* Function declaration for "AdjustVector" that passes in the enums above, and a float, and a vector to adjust. Note the CompactNodeTitle is optional, but it determines how the BP node will look on a graph. Note the order of variables in the declaration is Enum, Vector, Float, Enum ... this sets the vertical order of pins on the blueprint node, and is used cosmetically in this example so the Compact Node Label isn't covered by the enumerator variable's dropdown field. */

UFUNCTION(BlueprintPure, Category = "FunFunctions", meta = (DisplayName = "Adjust Vector", CompactNodeTitle = "VMath", Tooltip = "Adjusts selected components of a vector by a float using the given operation.", Keywords = "funf,adjust,modify,vector,math,vm"))

static FVector AdjustVector(EVectorComponents Components, const FVector& Vector, float Value, EMathOperation Operation);


//Private section of body omitted as it's not used for the example...

// private:

};

FunFunctionLibrary.cpp


Comments


Featured Posts
Recent Posts
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page