Help needed for the 'mega challenges' of the Simple Shooter C++ game

Hi all,

I have just finished the C++ Unreal course and I have gone through the video where the mega-challenges are presented. I decided to tackle them starting with creating more guns and being able to cycle through them while playing, but I am not understanding arrays correctly and I am unable to continue.

Here’s what I have done so far:

I have created GunArray as shown below inside the ShooterCharacter class, which I intend for it to be an array where I will store the 2 blueprints for my guns in the editor:

imagen

I have created a blueprint class based on the C++ class Gun, just like we did for the BP_Rifle, and called it BP_Pistol. I have added both to GunArray in the editor, like this:

imagen

I have created another array, where I intend to store the pointers to the different guns I spawn (I suspect somewhere here is where the mistake is made):

imagen

And now what I would like to do at BeginPlay is the same that was done in the course for a single gun, but for an array: I would like to save the pointer to the spawned gun. I am attempting to do it like this, but it does not compile, and I get a squiggly line over the equal sign:

imagen

What am I doing wrong? Isn’t this what we did when we only had one gun?

This is the tooltip error shown:

And perhaps more importantly, is this the best way to go about implementing this?

I have set the mouse scroll wheel as an input, and my objective later will be to cycle through the index of this array based on the scroll wheel value (I have observed in the log that every wheel action is either + or -1).

Thank you all for your time,

Aaron

The variable names you have chosen are a little misleading. GunArray isn’t an array of guns, it’s an array of gun classes i.e. an array of types of gun not an array of Gun instances. (I would have chosen GunClasses and Guns, personally. “Array” is a bit redundant, I can see that from the type).

The problem you have is twofold - well you one, but if you get that to compile you would immediately run into another problem.

  1. You’re trying to spawn an entire array of AGun using a single gun class (TArray is also not an actor), then assign that entire array to a single AGun element.

    Given SpawnActor<T>(...), it’ll return T* which should help explain your error message. You have a AGun* and you are trying to assign a TArray<AGun>* to it. That should read SpawnActor<AGun>(GunArray[Index])

  2. If you fix that you’ll compile but end up crashing when you try to play, why? Because ActiveGunArray is empty, all indexes are invalid as there are no elements. You need to use Add to create them and to avoid reallocations (arrays will allocate a block of memory then re-size once that block is filled) you can first use Reserve so it’ll have enough memory for all the guns you will end up spawning.

    ActiveGunsArray.Reserve(GunsArray.Num());
    // rest of your code spawning them
    for (...)
    

This solved it!

It has helped me a lot to understand why I was not doing this correctly. Thanks a lot for your help Dan!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms