Add string to TArray in other actor

I have two actor classes MyActor and MyActor2, I want to pass a TArray from MyActor to MyActor 2. In MyActor2 I have TArray<
FString> Parsed which is a global variable. The editor is crashing when doing this.

MyActor.h

UPROPERTY(EditDefaultsOnly)
AMyActor2* actor2;
TArray<
FString>
Parsed;

MyActor.cpp

actor2->set(Parsed);

MyActor2.h

TArray<
FString>
Parsed;

UFUNCTION()
void set(const TArray<
FString>& str);

MyActor2.cpp

void AMyActor2::set(const TArray<
FString>& str){

for(int32 nr=0; nr!= str.Num();){
Parsed.Add(str[nr]);
++nr;
}
}

Hi,

First, you shouldn’t make the input argument of your “set” function const because you are modifying your input argument by adding to it. Next, try to change your “set” implementation to the following and it should work fine:

void AMyActor2::set(TArray<FString>& str)
{
    for (auto s : str)
        Parsed.Add(s);
}

Hope this helps.

Thankyou for your time, but it’s still not working. I think its something about initializing the TArray or something

I dont get any errors

I found this:

for (int i = 0; i < 6; i++)
{
UStaffUnit* Newunit = NewObject();
RegularUnits.Add(Newunit);
}

RegularUnits[0]->Initialize(EStaffUnitTypes::Combat);
RegularUnits[1]->Initialize(EStaffUnitTypes::Development);
RegularUnits[2]->Initialize(EStaffUnitTypes::Construction);
RegularUnits[3]->Initialize(EStaffUnitTypes::Support);
RegularUnits[4]->Initialize(EStaffUnitTypes::Intel);
RegularUnits[5]->Initialize(EStaffUnitTypes::Medical);

I think I need to do something like this, with strings.

The actor its fine, I can call other stuff, so If I use a local variable TArray< FString> in set() its working, but I want to set the global variable.

i see actor2 is in default only, how do you set it ? could you wrap your call too it with a check ?
like :

if(actor2)
{
    actor2->set(Parsed);
}

even if you are pretty sure the value isn’t null, add it so you can discard this possibility

anyway, i suggest you to attach visual studio to you game/ editor so you can intercept the crash and see what’s wrong ! :slight_smile:

MyActor2.h
TArray< FString> Parsed;

Parsed is the global variable here that I want to set, but if I do this its working, though I dont want that.

void AMyActor2::set(TArray< FString>& str){
TArray< FString> ParsedLocal;
for (auto s : str)
ParsedLocal.Add(s);
}

I will try to find a way.
Anyway, thankyou for your help.

I could do that, but I am telling you its working.

Do you get any error messages when the Editor crashes? If so then please post them here so that we can help you resolve them.

If the editor crashes I doubt it has to do anything with your string copying. You’re probably trying to dereference a nullptr somewhere in your code. Check in an if-else statement whether your actor2 is valid before calling its member function “set”. Let me know how it goes :slight_smile:

Well, I don’t know what that global variable is, how you defined it and what you’re trying to do with it. Unfortunately, your issue is somewhat vague and given the information you’ve provided, if a null checking isn’t helping either, then I have no more comment to give. Maybe others can help but I think you need to provide more details for us to be able to help you. Good luck debugging your code :slight_smile:

I had not correctly created an instance of MyActor2 causing the reference from my .h file to be null so I added this and its working:
actor2 = GetWorld()->SpawnActor< AMyActor2>((AMyActor2::StaticClass());