Is it possible to create a struct with dispatcher built in?

Kokuyouseki
Sat, 11 May 2024 14:38:37 +0000
Programming & Scripting

My problem:
Character stat struct:
float Base
float Additive
float Multiplier
float Current

What I want to achieve is:

Whenever a value changes I want “Current” clamped to 0, (Base+Additive)*Multiplier.

Whenever a value changes I want to call an event dispatcher

If only I get the dispatcher thing working I’m already happy (as I can achieve the other through some extra time).
I’ve seen that the FVector and the like structs in the system have some functionality, like operations and the like in them but I saw no dispatcher done and what I’ve managed to find on this topic wasn’t really reassuring so far.

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Stat.generated.h"

/**
 * Containes values for Character Stat data along with dispatch on value change.
 */
USTRUCT(BlueprintType)
struct FStat
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StructTest")
	float Base;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StructTest")
	float Additive;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StructTest")
	float Multiplier;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StructTest")
	float Current;
};

Is it even possible to have a sort of built-in dispatcher in my struct so if I have a 100 I can bind to each of that 100 one by one? For example, character movement buff changes maxspeed so I change speed on the character movement component. Writing a “SetValue” function by hand for each and every variable in my character attributes seems like an utterly ridiculous proposition.

8 posts - 4 participants