How to add objects to an array

Hi all

What feels like forever, i have been trying to find a way to add an object to an array

When the player starts the game, one type of object will spawn from the beginning, lets call it object A. I tried to set it so that when the player clicks the buy button, object B is added to the array, so now either object A or B will spawn. This is one of my many many attempts:

This calls the custom event

Sorry if this is a mess, its went haywire since ive been trying everything, but this is what i have so far for adding the actual object to the array (stone 1 is in the minerals array, im trying to add stone 2)

It’s not very clear to me what you are trying to do, can you explain your end goal in more detail? You add to arrays using the Add node like you are doing above.

Your add stone2 event is redundant though - you are using GetAllActorsOfClass, then adding the class of all found actors to an array. Meaning every element in that array will be the same,.

To add something to an array, it needs to be the same type as the array. So if you have ObjectA and ObjectB, they can’t be in the same array unless it’s an array of actors, and not an array of ObjectA’s (Or B’s) specifically.

Hi Mosel3y

The end goal is to basically add the object “stone2” to the array called Minerals so it will spawn in game.

The first screenshot is the event graph in the widget where the button is.
The second screenshot is the event graph inside “Sphere”, which is an object.
I was told to use GetAllActorsOfClass a while back because I couldn’t cast between the 2 different blueprints

When the sphere is clicked on in game, it explodes into minerals (found in the minerals array), which in this case is only Stone1 for now. This works the way it should.
By clicking on the buy button, i want Stone2 to be added to the array, allowing it a chance to spawn with Stone1

I have little knowledge of how arrays, actors, classes etc all work. So far i have been treating each class as if it itself is a separate object.

I have work now, so ill play around with it more later today

Classes are not objects, in that they don’t “exist” in your game, they are essentially descriptions definitions of what an object of that class would be. An object of that class, an actor, is what actually exists in your game, an instance of that class. When you add a variable you will see you can add it as a class, or as an reference. The reference is to reference a specific object of that class that exist (or will exist) in your game, and the class is the definition of it. If you wanted to use SpawnActorFromClass node, or GetAllActorsOfClass, you could plug the class variable into those nodes, or, you could use them on compare class nodes, but it doesn’t actually exist as an object in your game world.

So here is my understanding of what you are doing - you are using this mineral array to control what class of stones can spawn from a sphere. And there are two types of stones at the moment. Initially only Stone1 will be spawned, but if the player buys it, Stone2 can also spawn, so this class needs to be added to the array of Minerals that can be spawned.

Here is what I have, an array of actor classes, with one element, the Stone1 class. This is in Sphere blueprint class.

The event in your widget is fine and should stay the same. Although I will point out this will only work on one Sphere actor in your game. If you want to add stone2 to all Sphere actors, or to a specific Sphere actor, you will have to do it differently. I am not sure which you want.

To add Stone2 to the array, all you have to do is this:

130693-s2.png

Minerals array now contains Stone1, and Stone2 class.

That was so simple compared to what i was doing, i over complicated it alot

Thanks for the help :slight_smile: however as you mentioned, stone2 will only spawn for one of the spheres and then it goes back to only spawning stone1. How would i get it to spawn for all the spheres, would it be a simple task? Im not even sure what to look for in a tutorial.

Again thanks for the help, this project is due in June and this is the last piece of coding (i hope) that i need to learn

No problem. So when Stone2 is bought, it should be added to all the spheres?

You could do it like this (In you buy stone widget):

This is fantastic! :smiley:

Hopefully the last thing ill need to pester you with, this works great, but i have the spheres spawning throughout the game. The code works for the spheres that have already spawned, but for the spheres that spawn AFTER i buy it, only stone1 will spawn

i hope this is also an easy fix :stuck_out_tongue:

Ah, in that case you will need to store the array elsewhere, somewhere where all the spheres can access it.

GameMode, GameInstance, or your pawn would work, GameMode and Pawn won’t work perfectly if you are opening new levels though, as the value will be reset. GameInstance will persist across level changes. I’ll show how to do it in game mode though. But the same will apply wherever you decide to add it.

So in your custom GameMode class, add a new variable same as before, the Minerals, an array of actor classes. Add a custom event in your GameMode, named AddStone2. It should do the same as in the Sphere - add Stone2 to the Minerals array.

On your Button clicked event, do this:

Obviously, cast to your own GameMode class.

Now, in your sphere on the spawn event, instead of using the sphere’s own Mineral array, use GetGameMode, cast to your GameMode, and drag off from that and get Minerals.

Another way would be to make the Minerals array in your sphere blueprint editable and exposed on spawn, and pass in the Minerals value from whatever object is spawning them. I;m not sure how you are spawning the minerals though. The above should work fine.

Found a problem connecting up the very last node

The array i made in my gamemode (clicking) is purple so the minerals list is an actor array, but when i drag off from the game mode it comes up as object. I tried looking for a SpawnObject node but couldnt find one

I think you must have made the mineral array in your game mode an Array of Object classes, instead of an array of actor classes. Can you check that?

Yea my mistake, it all works now :slight_smile:

Ive learnt so much, i feel so relieved i can actually get on with the project now that these walls have been broke down

I really really appreciate your time on helping me with these, thank you :slight_smile:

No problem, glad it’s working!