BindSpaceUsers (Unreal Engine)
- Declaration
static UCavrnusBinding* BindSpaceUsers(FCavrnusSpaceConnection SpaceConnection, CavrnusSpaceUserEvent UserAdded, CavrnusSpaceUserEvent UserRemoved);
Delegates Options:
DECLARE_DYNAMIC_DELEGATE_OneParam(FCavrnusSpaceUserEvent, FCavrnusUser, User);
typedef TFunction<void(const FCavrnusUser&)> CavrnusSpaceUserEvent;
- Description
Throws events whenever FCavrnusUsers enter/exit the given space.
Note that UserAdded
will be triggered immediately for every user that is already present.
This is the best way to maintain a list of users in, say, a UI panel. If you just want a snapshot of the current users for, say, debugging, consider calling GetCurrentSpaceUsers.
- Blueprint Sample
- Code Sample
The code used in the plugin by the CavrnusAvatarManager class.
CavrnusAvatarManager.h:
#pragma once
#include "CoreMinimal.h"
#include "CavrnusFunctionLibrary.h"
#include "CavrnusAvatarManager.generated.h"
// Class definition
UCLASS(BlueprintType)
class CAVRNUSCONNECTOR_API UCavrnusAvatarManager : public UObject
{
GENERATED_BODY()
public:
// Constructor
UCavrnusAvatarManager();
UFUNCTION()
void SpaceConnected(FCavrnusSpaceConnection SpaceConn);
UFUNCTION()
void OnUserJoined(FCavrnusUser User);
UFUNCTION()
void OnUserLeft(FCavrnusUser User);
UFUNCTION(BlueprintCallable, Category="Cavrnus")
void SwapAvatars(FCavrnusUser User, TSubclassOf<class AActor> AvatarClass);
private:
FString LocalUserConnectionId;
FCavrnusSpaceConnected OnSpaceConnected;
FCavrnusSpaceUserEvent UserAdded;
FCavrnusSpaceUserEvent UserRemoved;
TMap<FString, AActor*> UserMap;
};
CavrnusAvatarManager.cpp:
#include "CavrnusAvatarManager.h"
//===========================================================
UCavrnusAvatarManager::UCavrnusAvatarManager()
{
// Don't want to start relay client by loading CDO
if (!IsTemplate())
{
OnSpaceConnected.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(UCavrnusAvatarManager, SpaceConnected));
UserAdded.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(UCavrnusAvatarManager, OnUserJoined));
UserRemoved.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(UCavrnusAvatarManager, OnUserLeft));
UCavrnusFunctionLibrary::AwaitAnySpaceConnection(OnSpaceConnected);
}
}
void UCavrnusAvatarManager::SpaceConnected(FCavrnusSpaceConnection SpaceConn)
{
LocalUserConnectionId = SpaceConn.LocalUserConnectionId;
UCavrnusFunctionLibrary::BindSpaceUsers(SpaceConn, UserAdded, UserRemoved);
}
void UCavrnusAvatarManager::OnUserJoined(FCavrnusUser User)
{
//Don't spawn an avatar for the local user
if (User.UserConnectionId == LocalUserConnectionId)
return;
FActorSpawnParameters SpawnParams;
UWorld* World = GetWorld();
if (!World) return;
TSubclassOf<AActor> RemoteAvatarClass = GetRemoteAvatarClass();
if (!RemoteAvatarClass)
{
UE_LOG(LogCavrnusConnector, Error, TEXT("No RemoteAvatarClasses specified!"));
return;
}
SwapAvatars(User, RemoteAvatarClass);
}
void UCavrnusAvatarManager::OnUserLeft(FCavrnusUser User)
{
AActor** FoundActor = UserMap.Find(User.UserConnectionId);
if (FoundActor)
{
(*FoundActor)->Destroy();
}
UserMap.Remove(User.UserConnectionId);
}