76 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			76 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | using UnrealSharp.Engine; | ||
|  | using UnrealSharp.Attributes; | ||
|  | using UnrealSharp.BusyRabbit; | ||
|  | using UnrealSharp.GameplayAbilities; | ||
|  | using UnrealSharp; | ||
|  | using UnrealSharp.CoreUObject; | ||
|  | 
 | ||
|  | namespace UI.Level.Controllers; | ||
|  | 
 | ||
|  | [UMultiDelegate]  | ||
|  | public delegate void OnUIAttributeChange(FName AttributeName, float NewValue, float OldValue); | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | [UClass] | ||
|  | public class UBusyRoleStateController : UPW_UIController | ||
|  | { | ||
|  |     [UProperty()] | ||
|  |     public TMulticastDelegate<OnUIAttributeChange> OnAttributeChangeDelegate { get; set; } | ||
|  | 
 | ||
|  | 
 | ||
|  |     protected override void OnUIControllerInitialized() | ||
|  |     { | ||
|  |         if (UGameplayStatics.GetPlayerController(0) is ALevelPlayerController PC) | ||
|  |         { | ||
|  |             ABusyPlayerRole Role = PC.ControlledRole; | ||
|  |             if (AbilitySystemLibrary.GetAbilitySystemComponent(Role) is UBusyAbilitySystemComponent ASC) | ||
|  |             { | ||
|  |                 RegisterAttributeListener(ASC, "Health"); | ||
|  |                 RegisterAttributeListener(ASC, "Hunger"); | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public float GetControlledAttribute(FName AttributeName) | ||
|  |     { | ||
|  |         if (UGameplayStatics.GetPlayerController(0) is ALevelPlayerController PC) | ||
|  |         { | ||
|  |             ABusyPlayerRole Role = PC.ControlledRole; | ||
|  |             if (AbilitySystemLibrary.GetAbilitySystemComponent(Role) is UBusyAbilitySystemComponent ASC) | ||
|  |             { | ||
|  |                 FGameplayAttribute Attribute = UBusyAscLibrary.GetAttribute(Role.Attributes, AttributeName); | ||
|  |                 return ASC.GetGameplayAttributeValue(Attribute, out bool IsFound); | ||
|  |             } | ||
|  |         } | ||
|  |         return 0; | ||
|  |     } | ||
|  |      | ||
|  |     public float GetRoleAttribute(ABusyPawnBase Pawn, FName Attribute) | ||
|  |     { | ||
|  |         return 0; | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     protected void RegisterAttributeListener(UBusyAbilitySystemComponent ASC, FName AttributeName) | ||
|  |     { | ||
|  |         TDelegate<OnBusyAttributeChange> Delegate = new TDelegate<OnBusyAttributeChange>(); | ||
|  |         Delegate.BindUFunction(this, "OnAttributeChange"); | ||
|  |         ASC.BindEventToAttributeChange(typeof(UBusyPlayerRoleAttributeSet), AttributeName, Delegate); | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  |     [UFunction()] | ||
|  |     protected void OnAttributeChange(FName AttributeName, float NewValue, float OldValue) | ||
|  |     { | ||
|  |         OnAttributeChangeDelegate.Invoke(AttributeName, NewValue, OldValue); | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  |     protected override void OnUIControllerDestroy() | ||
|  |     { | ||
|  |         base.OnUIControllerDestroy(); | ||
|  |     } | ||
|  | } |