# How to modify a Component based on Authority in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [September 30, 2015, 8:37pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905 "2015-09-30T20:37:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [September 30, 2015, 8:37pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/1 "2015-09-30T20:37:59Z")

</div>

Hi there. I basicly try to call a function in C++ which does the exactly same as this Blueprint does:

!60930-

(It should set the color of a cube on the clientside to green and on the serverside to red)

My problem is, that I need to call this function from a C++ script, which is called only on the server. In a second step I need to get a specific component and set a Value. Unfortunately I failed at both of these steps.

I spent hours trying out different ways to achieve it, but it just won’t work. I could really need some help!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [October 5, 2015, 1:49pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/2 "2015-10-05T13:49:45Z")

</div>

When do you want this to happen? Arbitrarily throughout the game? At startup? ???

Anyway, the easiest way is to have a “SetColour” function that you call on the server that calls a “Multicast\_SetColour” function that will do what you have in the bp.

---

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [October 5, 2015, 6:43pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/3 "2015-10-05T18:43:04Z")

</div>

First off: Thank you for the answer!

The actor will be spawned at runtime…  
I tried to do what you said. Here is the Code:

AuthorityTest.h

```
#pragma once

#include "GameFramework/Actor.h"
#include "AuthorityTest.generated.h"

UCLASS()
class AURA_API AAuthorityTest : public AActor
{
	GENERATED_BODY()

public:
	//Constructor
	AAuthorityTest();

	//Variables
	UPROPERTY(EditAnywhere, Category = Main)
		UMaterialInstance* ClientMat;
	UPROPERTY(EditAnywhere, Category = Main)
		UMaterialInstance* ServerMat;
	UPROPERTY(EditAnywhere, Category = Main)
		UStaticMeshComponent* MeshComp; //Doesn't show up!

	//Functions
	virtual void BeginPlay() override;
	UFUNCTION()
		void SetColour();
	UFUNCTION(NetMulticast, Reliable, WithValidation)
		void Multicast_SetColour();
		virtual void Multicast_SetColour_Implementation();
		virtual bool Multicast_SetColour_Validate();
};

```

AuthorityTest.cpp

```
#include "Aura.h"
#include "AuthorityTest.h"

AAuthorityTest::AAuthorityTest()
{
	PrimaryActorTick.bCanEverTick = false;
	bReplicates = true;
}

void AAuthorityTest::BeginPlay()
{
	Super::BeginPlay();
	
	UE_LOG(LogTemp, Warning, TEXT("Begin called"));

	if (this->HasAuthority())
	{
		SetColour();
	}
}

void AAuthorityTest::SetColour()
{
	Multicast_SetColour();
}

void AAuthorityTest::Multicast_SetColour_Implementation()
{
	if (HasAuthority())
	{
		UE_LOG(LogTemp, Warning, TEXT("Called on Server"));
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Called on Client"));
	}
}

bool AAuthorityTest::Multicast_SetColour_Validate()
{
	return true;
}

```

Right now it does show “Begin called”, “Called on sServer”, “Begin called”. It doesn’t execute the function on the client though.

An other problem is, that I don’t get the StaticMeshComponent to show up in the defaults. Which is why I can’t select on which mesh I want to swap the material.

---

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [October 5, 2015, 6:44pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/4 "2015-10-05T18:44:00Z")

</div>

Seems like I can’t use comments with code and only answer the question directly.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [October 6, 2015, 7:12pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/5 "2015-10-06T19:12:49Z")

</div>

Hrm. How are you spawning the actor at runtime?

---

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [October 8, 2015, 1:52pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/6 "2015-10-08T13:52:45Z")

</div>

Okay I figured it out.

**How to call the function on the server and the client:**  
You can use replicated variables which trigger a RepNotify-Function/Event. It’s explained here: [RepNotify](https://wiki.unrealengine.com/Network_Replication,_Using_ReplicatedUsing_/_RepNotify_vars)

**How to access a StaticMeshComponent:**  
A StaticMeshComponent won’t show up in the defaults, but it will be accessible in the Blueprint. To link the component, you have to simply set the connection in the construction-graph.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [October 8, 2015, 2:56pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/7 "2015-10-08T14:56:09Z")

</div>

This might work for you, but you should be aware of the differences between replication and rpcs. RPCs should be used for gameplay important things. Replicated variables aren’t guaranteed to be replicated to clients within a reasonable timeframe or in any specific order, only that given enough time they should update on the client.

You should still look into why RPCs were not working for you, because if they weren’t it’s probably symptomatic of a larger issue.

You can read more here

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

---

<div class="post-metadata">

**Author:** ![Sydon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sydon](https://forums.unrealengine.com/u/Sydon)\
**Post date:** [October 9, 2015, 8:27am UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/8 "2015-10-09T08:27:02Z")

</div>

It doesn’t make a difference if I spawn the actor at runtime or not. As soon as I start the RPC only on the server, it won’t get called on the client (As I did in the code).

- Server-RPCs seam to work perfectly for me.
- With Client-RPCs I just don’t know what to use them for. They will only be called if the Client owns them, which doesn’t seem very useful in a Server-Listener scenario.
- Multicast-RPCs seam to do exactly the same as the Server-RPC’s

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [October 9, 2015, 3:47pm UTC](https://forums.unrealengine.com/t/how-to-modify-a-component-based-on-authority-in-c/333905/9 "2015-10-09T15:47:18Z")

</div>

> With Client-RPCs I just don’t know  
> what to use them for. They will only  
> be called if the Client owns them,  
> which doesn’t seem very useful in a  
> Server-Listener scenario.

What would you expect these to do? That is their function.

> Multicast-RPCs seam to do exactly the  
> same as the Server-RPC’s

Multicast-RPCs get called everywhere when called from the server. This is what you want to call from the server if you want it to be called on all clients.
