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"

[Solved]Possession Error of second player in c++

Hey guys I'm having problems trying to possess the second player in my game. I was able to do this in blueprints with no problem but I decided to go full c++(refusing to use bp) and I got everything working except for this. The end of the code is pretty much what I need help with.

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "ProjectBlue.h"
 #include "GM_DeathMatch.h"
 #include "SpawnLocations.h"
 #include "MyCharacter.h"
 
 AGM_DeathMatch::AGM_DeathMatch(const FObjectInitializer &Objectinitializer) :Super(Objectinitializer)
 {
     static ConstructorHelpers::FObjectFinder<UBlueprint> PlayerOB(TEXT("Blueprint'/Game/Player/Player'"));
     if (PlayerOB.Object)
     {
         PlayerBP = (UClass*)PlayerOB.Object->GeneratedClass;
     }
 }
 
 void AGM_DeathMatch::BeginPlay()
 {
     for (TObjectIterator<ASpawnLocations> Spawners; Spawners; ++Spawners)
     {
         const FBox box = Spawners->GetComponentsBoundingBox();
         FVector point = FMath::RandPointInBox(box);
         SpawnLocations.Add(point);
     }
     UGameplayStatics::CreatePlayer(GetWorld(), -1, false);
     FRotator spawnRotation = FRotator(0.0f, 0.0f, 0.0f);
     Index = FMath::RandRange(0, SpawnLocations.Num()-1);
     FVector randChoice = SpawnLocations[Index];
     FActorSpawnParameters SpawnParams;
     SpawnParams.Owner = this;
     SpawnParams.Instigator = Instigator;
     UWorld* const World = GetWorld();
     if (World)
     {
         AMyCharacter* Character = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
         Character->PIndex = 0;
         SpawnLocations.RemoveAt(Index, 1, true);
         for (int32 i = 0; i < SpawnLocations.Num()-1; i++)
         {
             int32 firstIndex = FMath::RandRange(0, SpawnLocations.Num()-1);
             int32 secondIndex = FMath::RandRange(0, SpawnLocations.Num()-1);
             int32 spareIndex;
             if (firstIndex != secondIndex)
             {
                 spareIndex = firstIndex;
                 SpawnLocations[firstIndex] = SpawnLocations[secondIndex];
                 SpawnLocations[secondIndex] = SpawnLocations[spareIndex];
             }
             
 
         }
         Index = FMath::RandRange(0, SpawnLocations.Num() - 1);
         FVector randChoice = SpawnLocations[Index];
         AMyCharacter* Character2 = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
         APlayerController* firstController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
         APlayerController* secondController = UGameplayStatics::GetPlayerController(GetWorld(), 1);
         firstController->Possess(Character);
         secondController->Possess(Character2);
     }
 }

If I take out "secondController->Possess(Character2);" I'm able to control the first player but if I leave it in it throws one of those exceptions error in visual studio.

In bp I had it searching for "all actors of class" and then possessing them but idk how to do that in c++ when the player itself is a blueprint (I know how to get the reference to bp through code but don't know how to search through all of them).

The problem looks like the index for the player controller is not available or something like that for the second player. How would I fix this?

Btw it's for splitscreen.

PS I'm sorta new-ish to c++ atleast when it comes to UE4

Product Version: Not Selected
Tags:
more ▼

asked Apr 04 '15 at 07:02 PM in C++ Programming

avatar image

ArgenisXD
3 3 7 7

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

1 answer: sort voted first

Edited Code:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "ProjectBlue.h"
 #include "GM_DeathMatch.h"
 #include "SpawnLocations.h"
 #include "MyCharacter.h"
 
 AGM_DeathMatch::AGM_DeathMatch(const FObjectInitializer &Objectinitializer) :Super(Objectinitializer)
 {
     static ConstructorHelpers::FObjectFinder<UBlueprint> PlayerOB(TEXT("Blueprint'/Game/Player/Player'"));
     if (PlayerOB.Object)
     {
         PlayerBP = (UClass*)PlayerOB.Object->GeneratedClass;
     }
 }
 
 void AGM_DeathMatch::BeginPlay()
 {
     for (TObjectIterator<ASpawnLocations> Spawners; Spawners; ++Spawners)
     {
         const FBox box = Spawners->GetComponentsBoundingBox();
         FVector point = FMath::RandPointInBox(box);
         SpawnLocations.Add(point);
     }
     UGameplayStatics::CreatePlayer(GetWorld(), 1, true);
     FRotator spawnRotation = FRotator(0.0f, 0.0f, 0.0f);
     Index = FMath::RandRange(0, SpawnLocations.Num()-1);
     FVector randChoice = SpawnLocations[Index];
     FActorSpawnParameters SpawnParams;
     SpawnParams.Owner = this;
     SpawnParams.Instigator = Instigator;
     UWorld* const World = GetWorld();
     if (World)
     {
         AMyCharacter* Character = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
         SpawnLocations.RemoveAt(Index, 1, true);
         for (int32 i = 0; i < SpawnLocations.Num() - 1; i++)
         {
             int32 firstIndex = FMath::RandRange(0, SpawnLocations.Num() - 1);
             int32 secondIndex = FMath::RandRange(0, SpawnLocations.Num() - 1);
             int32 spareIndex;
             if (firstIndex != secondIndex)
             {
                 spareIndex = firstIndex;
                 SpawnLocations[firstIndex] = SpawnLocations[secondIndex];
                 SpawnLocations[secondIndex] = SpawnLocations[spareIndex];
             }
 
 
         }
         Index = FMath::RandRange(0, SpawnLocations.Num() - 1);
         FVector randChoice = SpawnLocations[Index];
         AMyCharacter* Character2 = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
 
         UGameplayStatics::GetAllActorsOfClass(GetWorld(), PlayerBP, PlayerArray);
         int32 Itr2 = 0;
 
         for (int32 Itr = 0; Itr < PlayerArray.Num(); Itr++)
         {
             
             AMyCharacter* PlayerTest = Cast<AMyCharacter>(PlayerArray[Itr]);
             PlayerTest->PIndex = Itr;
             GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Black, FString::FromInt(PlayerArray.Num()));
             UGameplayStatics::GetPlayerController(GetWorld(), PlayerTest->PIndex)->Possess(PlayerTest);
         }
     }
 }

The Problem was when I called the create player function I had spawn pawn as false...

more ▼

answered Apr 05 '15 at 01:30 AM

avatar image

ArgenisXD
3 3 7 7

(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