Trouble filling array

I’m generating a map with land, sea, and city tiles, then setting different materials for them and it’s all great but I need to put the cities in an array - and nothing seems to work. Can’t use a foreach in the tile’s BP, that creates an infinite loop. I tried taking the tiles immed. after the city material is set, but it only ever puts 1 thing in the array, when there should be a dozen or so. I tried doing a for-loop from the board BP, but my ‘Is a City’ variable isn’t coming over. Is there a way to select by material? How would you do it? Grazzi!

1 Like

First try moving whole logic to begin play instead of constructor. Constructor script does not guarantee that anything exists outside actor where constructor script is. And this is major problem with construction scripts either you see it all in editor (and risk bogus errors), or you do it on begin play and do not see results in editor until game is running.

And Maybe tackling this problem upside down will be easier.

Instead of making logic for each tile in single place, make that logic inside each tile. So make SINGLE tile actor.

So tile has wx,wy dimensions, from that you can calculate int X and Y index, then make it auto snap to closest coordinate that its in grid.

Since tile actor now knows its X and Y index it can identify self.

Make dispatcher that sends "hey tile X,Y should be mountain. And the tile happily becomes mountain (or whatever).

Also You can use gameplay tags as more evolved enumerator.

So each tile will have tag like: TileTypes.ForrestBiome.Hills or TileTypes.CityBiome.Buildings.Church.verA

Then you can add to dispatcher and give tag that must match: TileTypes.CityBiome.Buildings , and you tell that this event is only for buildings in city.

Same can be done for actions, like turning on off lights, changing material etc.

Game Tags are great because they are hierarchical and you can address exact tag or bigger groups of them.

Ps.
Try blueprintable editor widget tools. You can make widgets that can run as extra editor tools. Reall great way to automate tasks.

And Look at wave function collapse:

Very cool! Thanks for that

I’m surprised no one who makes tutorials has used tags so far, so I haven’t learned them yet, but luckily they don’t use the constructor either! So I am using Begin Play, and also a single tile BP.

Can you have a look at this? From the branch to the Set Material, this works, and all my cities show up with this material in gameplay, no for-loop needed. It seems like adding them to an array should work too, but no? Or have I set it up wrong? Thanks in advance

There are multiple “niuances” in blueprints (all because object oriented code, and load order):

  • code in editor is not code that actually runs. Well it rune, but editor has its own copy of actor. Then it spawns similar actors in level. So not always copy in editor is same as actor in level. You can see that when debugging with breakpoints.
  • similar thing with referencing and copying/adding to array. It is different to add copy, and add reference top actor or material. One makes copy then adds object another just remembers one more reference.

From your code i GUESS you are adding reference to actor to some array, not actual actor.

You can instead use GetALLActors (of class) and get same array done for you. This is better because sometimes unreal unloads some actors.

I don’t think I can use Get All actors because I only want those instances that are set to “City”. Maybe I can use that in a new function and branch it with “City”, thanks!

  • oh wait, I’ve already tried that :sweat:

vs

Wouldn’t the above mean that each city actor has its own array with only itself in the array? Impossible to tell more from this snippet :person_shrugging:


If you’re:

generating a map with land, sea, and city tiles

You’d normally have a separate Manager Actor that governs who is who and where they belong. Often referred to as a Spawner.

" - each city actor has its own array?"

I don’t know UE well enough but I assumed that all would go into the same array, in the same manner that they’re all getting the same material assigned? Or is there a new material being created for each instance too?

( this is the tiles BP, instanced for each tile, and on Begin Play they’re assigned different attributes for land / sea etc. )

a2cba280c7ff88f1a727cfa5cdce018331f7dd9b_2_689x198

If this script is in the Tiles BP, then you’re adding the Tile actor instance (self) to the Cities array which would make even less sense, I guess? Again, we know nothing about how you structured it at this point.

! Why does it make less sense to add… can you not add instances to arrays? That would explain everything

You can add instances.

  • you have a tile BP
  • you have a city BP

The script you have adds Tiles to the Cities. At least that’s what I’ve gathered from pic and description. Chances are we simply do not understand what you’re trying to do vs what you’re doing.


Shouldn’t the Tile BP add Cities to the Cities array?

No I don’t have a city BP ; only Tiles that have a City boolean checked - those are the ones I want to put in an array

Each tile has its own Cities array. The Tiles do not share the Cities array. Every single variable is instanced. Each tile adds itself to the Cities array that it owns.

If you have 10 tiles that are cities, each tile has an array with 1 city in it (which is also a tile). The array thing would only make sense if a tile could have more than 1 city - then you could ask a tile: hey, how many cities are there in that tile?

OK that’s news to me!

If I print this is what I get ; seems silly that I can’t get them into an array, but I get it now.

But I am having trouble reading the variable from other BPs, maybe I should just figure out what I’m doing wrong there, then I should be able to make an array, Thanks!

Screenshot 2024-10-14 at 1.07.00 AM

That’s why you have a Manager Actor. It would create tiles and separates them into Cities / Lakes / Forests and whatnots. There is only one Manager and it owns the arrays. Reference this manager in the Game Mode and you can always access it from anywhere.

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