Download

Get All Actors Of Class + Children in Array

I’m currently building a game that mimics the play style of Harvest Moon. A player interacts with a grid of farm tiles (planting seeds, watering, harvesting, etc). My old system had every tile house every model, every calculation and store variables for stages it may never use. For example, a player may only interact with 60 tiles out of 400 tiles, and every time the player swung their tool, it would go through a loop of Get All Actors Of Class and cull the list based on X, Y coordinates in reference to the tool and player. My rig didn’t have a problem with this, but I thought lesser computers may run into severe performance issues.

So I gutted the entire system.

Instead the system I want to build will have a parent>child hierarchy to save on data. The hierarchy would start with a base grid that is only intractable with a Hoe. Then once it is Hoe’d it’ll destroy the actor, and spawn a Child Actor that will allow it to be seeded. If watered and seeded, the next day that Actor will be destroyed and replaced with a Child Actor which will be the specific plant based on the seed.

Untouched Soil>Hoed Soil>Plant

So every morning I want each of my Actors to do their simple calculations, then, instead of doing a Get All Actors Of Class for every tool swing, I want to do it once everyday and build an array for the player to reference. I’ve tried several times already to build this system and every attempt means 30 minutes or more of re-configuring the rest of my system to work with the array (And each attempt has failed completely ;-; ). I just wanted to see if I could get a second opinion (If there’s a better, easier way) or if this blueprint attached may even work. Thank you.

So GetAllActorsOfClass is actually relatively fast (internally the engine keeps a hashtable for each class, that contains all instances of that class )

In your case, if you are culling based on distance, you could utilize a custom octree (or utilize the built-in culling if it applies)

Btw - couldnt you just have the tile spawn the appropriate actor at the appropriate stage, deleting the old one?

Basically - howing/seeding/planting seems like a state machine. As you traverse from state to state, spawn and delete relevant actors as needed.

:slight_smile:

I think I have something built already that may act like a state machine. I’m not quite sure because I haven’t learned the jargon yet (I’m trying!). What happens now is when you interact with the grid using a tool, it will destroy the actor and spawn a new one based on the tool used. The problem is, the new actor is a Child of the Parent, and is not longer referenced in the Array anymore. Does get All Actors Of Class automatically reference Child Actors too? Or is there something I’m missing? Here’s the BP for the Parent Actor.

hard to say without understanding more of your game needs…

but it looks like each state is spawning the next and this is causing you ownership confusion

Perhaps you should use a Containing Tile class (eg your FarmGrid) to manage the spawned children.

This class is the one that would hold CurrentState, and when you switch states it spawns and destroys the appropriate types of components(or children).

(Store the return value of spawn in a member variable of the FarmGrid)

Also - on a side note - you could use a datatable to define each of the states (e.g. Holds Static Mesh, on other game specific properties)
First, create a new BlueprintStructure and define the properties you want.
Then create a new DataTable from it.
Then FarmGrid can use DataTable to determine what type of class to spawn on state change.
This lets you use a little bit more of data driven approach.

Thanks for the help! I’ll give that a try.