x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How can i assign an actor spawned from server to a client variable?

Hey all, i have some some very basic knowledge of UE4 multiplayer, i'm trying to spawn an AI controlled character from my player character and assign it to a pointer variable, i'm spawning it with a RPC function from BeginPlay like this

 void AEVPlayerCharacter::BeginPlay()
 {
     Super::BeginPlay();
 
     ...other stuff...
     
 
        // Prevent multiple spawn
     if(IsLocallyControlled())
         SpawnAICharacter();    
 }

and here the SpawAICharacter function (and relative RPC).

.h

 class AEVAICharacter* AIPlayer;

 UPROPERTY(EditDefaultsOnly, meta = (AllowPrivateAccess = "true"))
 class TSubclassOf<AEVAICharacter> AIPlayerClass;

 void SpawnAICharacter();
 
 UFUNCTION(Server, Reliable, WithValidation)
 void ServerSpawnAICharacter();

.cpp

 void AEVPlayerCharacter::SpawnAICharacter()
 {
     ServerSpawnAICharacter();
 }
 
 void AEVPlayerCharacter::ServerSpawnAICharacter_Implementation()
 {
     FActorSpawnParameters SpawnParams;
     SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
     FTransform Tr = GetActorTransform();
     AIPlayer = Cast<AEVAICharacter>(GetWorld()->SpawnActor(AIPlayerClass, &Tr, SpawnParams));
     AIPlayer->MovementCompleteDelegate.AddDynamic(this, &AEVPlayerCharacter::OnMovementCompleteCallback);
 }
 
 bool AEVPlayerCharacter::ServerSpawnAICharacter_Validate()
 {
     return true;
 }

but the variable AIPlayer will never be set on client, any suggestion?

Product Version: UE 4.20
Tags:
more ▼

asked Sep 01 '18 at 09:27 AM in C++ Programming

avatar image

ZioYuri78
41 8 12 15

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Shame on me, i had to replicate the variable, i did it previously but for some reasons didn't worked, now i tried again and with a clean rebuild all work fine.

Solution:

.h

 UPROPERTY(Replicated)
 class AEVAICharacter* AIPlayer;

.cpp

 void AEVPlayerCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME(AEVPlayerCharacter, AIPlayer);
 }



more ▼

answered Sep 02 '18 at 04:45 PM

avatar image

ZioYuri78
41 8 12 15

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question