Ok, I have this rrrrrrealy wierd thing going on.
I have few NPC’s derived from ACharacter
.
I’m moving them around with AAIController->MoveToLocation()
. Overall this thing works fine, I know how to get all the controlles, how to find needed NPC from a bunch etc.
But today I’ve rewrited few methods and suddenly found out that NPC’s not moving.
Trying to figure this out, I started Editor from Visual Studio, then first started game with standalone mode - nothing. Then started it in a preview window, to check code step by step. And the’s when character moved. Ok, I closed editor, that ran under VS debugger and tried again in self-sustained one.
But I found out, that my NPC wouldn’t move only when I start it in standalone mode! In other modes it worked.
After that I cleared Binaries, thought this might help, but no, It all started over again.
I realy don’t get what could be wrong, so here’s my code, maybe someone could help or reproduce if it’s a bug:
gameMode.h
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "dayFlow.h"
#include "GameFramework/GameMode.h"
#include "TheBarGameMode.generated.h"
class ATheBarGameMode : public AGameMode
{
GENERATED_BODY()
........
// this is a collection of structures with spawned NPC references and their ID's, see definition in dayFlow.h
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "NPC")
TArray<FNPC_Class> NPCs_in_Bar;
........
};
dayFlow.h
.............
USTRUCT(BlueprintType) // Char comes event ID=1
struct FNPC_Class
{
GENERATED_USTRUCT_BODY()
public:
int NPC_ID;
ACharacter * Char_Ref;
};
.............
UCLASS()
class THEBAR_API UDayFlow : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
..............
// returns reference to specific spawned actor
UFUNCTION(BlueprintCallable, Category = "NPC")
static ACharacter* GetNPCReference(int Char_ID, TArray<FNPC_Class> NPCs_in_Bar);
// moves character to certain location,
UFUNCTION(BlueprintCallable, Category = "Events")
static bool executeEventMoveToLocation(FEventCharMoves moveEvent, TArray<FNPC_Class> NPCs_in_Bar);
...........
};
dayFlow.cpp
............
ACharacter* UDayFlow::GetNPCReference(int Char_ID, TArray<FNPC_Class> NPCs_in_Bar)
{
return NPCs_in_Bar.FindByPredicate([Char_ID](FNPC_Class & item) {return item.NPC_ID == Char_ID; })->Char_Ref; // returns NPC reference;
}
bool UDayFlow::executeEventMoveToLocation(FEventCharMoves moveEvent, TArray<FNPC_Class> NPCs_in_Bar)
{
ACharacter* NPC = GetNPCReference(moveEvent.char_ID, NPCs_in_Bar);
// spawns default controller (if not, there is no controller for NPC to control)
NPC->SpawnDefaultController();
// cast controller recieved in NCP in Bar Class, derived from AAIController class
ABarNPCAIController * moveController = Cast<ABarNPCAIController>(NPC->GetController());
if (moveController == nullptr) return false;
// moves character to location defined by TargetLocation component of moveEvent
moveController->MoveToLocation(*moveEvent.targetLocation);
return true;
}
............
So, what is going on here. When NPC spawnes at my location I pass it’s reference to NPCs_in_Bar struct aswell as it’s unique ID.
When I need to move certain NPC I find that NPC in array of all NPC’s spawned by it’s ID, with GetNPCReference()
methid, which returns it’s reference.
What I do is, put blueprint node executeMoveToLocation
in gameMode blueprint, plug NPCs_in_Bar array into it and a FEventCharMoves struct which only includes character ID and an FVector targeted location.