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"

Issue with dereferencing and = operator.

I was working with a TArray of pointers and I had some trouble. The TArray is of size 1000 and my goal is to copy the first 500 to the second 500. Originally I did it like this

 for (int i = 500; i < 1000; i += 1) {
     CreatureList[i] = CreatureList[i - 500];
     CreatureList[i]->Mutate();
 }

Here CreatureList is a TArray, but since an element of CreatureList is a pointer, its passing that pointer and when the function Mutate is called, both the creature at index i and index i - 500 reflect the changes of the Mutate function.

Note: The elements of CreatureList must be pointers because I need them to be polymorphic and from what I understand polymorphism only works with pointers in c++ (Correct me if I'm wrong about that)

I discovered a solution outside of unreal engine which worked perfectly fine.

 for (int i = 500; i < 1000; i += 1) {
     *CreatureList[i] = *CreatureList[i - 500];
     CreatureList[i]->Mutate();
 }

But when I use this syntax in unreal engine I get an error saying "UCreature &UCreature::operator=(const UCreature &) declared at line 27 of Creature.h is inaccessible" In my header file, line 27 is

 GENERATED_BODY()

Does anybody have any insight into why I'm not allowed to use this operator here? If there are any alternatives or workarounds, that would be great as well. I'm making an evolution simulator, and if every creature is mutating instead of a specific one in the population, then that would cause problems. Thanks!

Product Version: UE 4.21
Tags:
more ▼

asked Mar 06 '19 at 01:06 AM in C++ Programming

avatar image

アイカモン三五七
1 1

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

1 answer: sort voted first

Try replacing line:2 with CreatureList[i] = DuplicateObject(CreatureList[i-500], outer, newName)

(See UObjectGlobals.h for reference.)

Instantiating an UObject has too many things happening under the hood. That's why they overload the operator "=" to prevent users from copying by themselves.

more ▼

answered Mar 06 '19 at 06:11 PM

avatar image

madturtleGGinin
346 1 5 4

avatar image アイカモン三五七 Mar 06 '19 at 06:59 PM

@madturtleGGinin Thank you. I think that worked but it'll be a little bit of time before I can check it since I have to work through the rest of the basic project architecture. So I wrote the DuplicateObject function like this->

 DuplicateObject<UCreature>(CreatureList[i-500], this);

and I wanted to check something before I procede. Does this guarantee that if CreatureList[i-500] is a type that is a subclass of UCreature, does that mean itll duplicate it as that subclass? or will the template argument < UCreature> make it copy over as a UCreature only?

avatar image madturtleGGinin Mar 06 '19 at 07:14 PM

The return value is UCreature but you should be able to cast it to its subclass like

UDog* dog = Cast<UDog>(CreatureList[i]);

Is that what you're asking? Or do you have specific task you need to achieve?

avatar image アイカモン三五七 Mar 06 '19 at 07:19 PM

Yeah I think so. To clarify my question further, I mean that if UDog is a subclass of UCreature and I do this:

 UDog* dog = NewObject<UDog>();
 UCreature* creature = DuplicateObject<UCreature>(dog, this);

will the variable creature be a dog since its duplicating a UDog pointer? or will dog be duplicated as a UCreature since that's the template argument of the DuplicateObject function?

(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