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"

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; }
}

Product Version: UE 4.15
Tags:
more ▼

asked Dec 28 '17 at 12:04 AM in C++ Programming

avatar image

BestWarrior
34 3 3 8

avatar image Firefly74 Dec 28 '17 at 12:05 PM

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 ! :)

avatar image BestWarrior Dec 28 '17 at 12:18 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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());

more ▼

answered Dec 28 '17 at 04:03 PM

avatar image

BestWarrior
34 3 3 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Dec 28 '17 at 12:43 AM

avatar image

Vizgin
1.5k 10 10 18

avatar image BestWarrior Dec 28 '17 at 11:42 AM

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

avatar image Vizgin Dec 28 '17 at 11:45 AM

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.

avatar image BestWarrior Dec 28 '17 at 11:50 AM

I dont get any errors

avatar image BestWarrior Dec 28 '17 at 11:54 AM

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.

avatar image Vizgin Dec 28 '17 at 12:00 PM

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 :-)

avatar image BestWarrior Dec 28 '17 at 12:01 PM

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.

avatar image Vizgin Dec 28 '17 at 12:11 PM

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 :-)

avatar image BestWarrior Dec 28 '17 at 12:16 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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