Array not indexing items

OK! Thank you! I was really confused about where I should actually be making the logic for the turns and all that. Inside a widget didn’t seem right.

SO I’m going to cast to a gameinstance BP.

Here’s my idea, and tell me if I’m wrong about anything:

Spawn Players

  1. in my “foreachloop” body of my ATFYCR widget I want it to not only set the text for the vertical box on the right, I also want it to spawn a new actor each time it loops (one for each index item in the “players” array).
  2. Maybe index those players into a seperate array (character reference)?
  3. I also need to figure out a way to keep the actors from spawning on top of one another

Turn Rotation:
(note: I may want to do something before the turn rotation to randomize the order of the players)

  1. create some sort of multigate or some sort of flow control that lets the game know which player’s turn it is, and resets when the last index item takes their turn. (this part will be a whole mess of trial and error because I have no idea how to get this working)

Space Info:

  1. create sruct for all the variables I’m going to need
    example:
    SpacePosition (targetpoint)
    SpaceType (Text)
    Penalty (Integer)
  2. create array of structs with the same number of index items as SPACES on the board, and input the variables for each struct
  3. hopefully I will be able to get the data from the struct depending on the number the player lands on

Am I on the right track?

I haven’t done any turn based stuff (well I did make a simultanious turns for a scrapped project) so it’s not like I know exactly what will be a great idea or what might be a dead end implementation but it all sounds reasonable to me.

As for handling how the character and widget are informed that it is their turn, I can think of 2 different ways to do that. There’s probably other ways.
The first method is that the turns controller, that I will call whatever is in charge of the turns, has two separate arrays of characters and widgets.
The other is that when the play button is pressed inside the main widget, it informs the turns controller about the event, passing in the created names and widgets array. Then, as the Turns controller spawns the character, it also gives the character the widget.

Do note that either or both methods may have drawbacks that I’m not realizing at the moment.

For the turn rotation, it doesn’t have to be complicated. Also, the array class has a shuffle function. Of course if more than one array is used, then they need to be syncronized.

I tried spawning actors in the loopbody. They needed a transform, so I got the player controller and got his world transform.

It spawned the right amount of actors up to 4 but didn’t spawn anymore if there were more players. But the good thing is, it seemed to spawn them apart from one another.

Next I tried spawning them at a target point instead.

I have target points all across the game board. 0 being the starting block.

I used get all actors of class, and selected targetpoint. Then got “0” from the array of targetpoints. But this didn’t seem to work at all for some reason.






You’ll probably have trouble spawning a bunch of actors at the same spot, if they are set to collide with each other. In the spawnActor node you can see some collision handing. I guess you have it to either default or “try to adjust location”. which it does for 4 characters but then fails to do it with more.
Try spawning them in a line, just to be sure that it’s working. something like

spawn line.png

“I used get all actors of class, and selected targetpoint. Then got “0” from the array of targetpoints.”
First, if the “target point” are those blue blocks, and if that blue block is at the center of the blueprint, the character will attempt to spawn inside it, which just isn’t a good idea. You’ll want to spawn them some distance above the target point I suppose.
Second, if you “get 0 index” from the array, you always get the same point. though not necessarily “targetpoint0”. How you’re supposed to choose spawning point I can’t tell since I don’t know how you want them to spawn. If you just want to randomize it, you could save the array that you get from “get all actors” as a variable. Then choose a random index (look up random int), do the spawn stuff, then remove that index and choose a new random index until all characters have been spawned.

ok, I will try spawning them in a line like you said. I created the target points one by one on top of the blue blocks. If possible I would spawn them in a grid, then adjust the sizes of the blocks so the maximum amount of players would fit on all of them. I even considered making 16 target points on every block for each player’s position… but I’m sure there’s a better way to do this than that.

hm… maybe I could create a BP with 16 target points and then use that on every block… or something.

I don’t know tbh, I’m probably just gonna experiment a bit… Let me know if you have a suggestion for a good way to handle this!

A square grid system is pretty easy to do. But if you want pent or hex grid, I can’t help but there’s probably tons of information on it.
Depending on your grid needs, it can just be points, or you can spawn actors at those points. It sounds like you want actors. So if you’re fine with just placing actors down, evenly then the following will do the trick:

Try making a new actor class that just has a default cube in it.
Though even if you save the references in an array, it’s pretty difficult with that setup alone to say at what “coordinate” a specific actor is, or get the actor at coord x,y.
That’s where I have used the map/kvp/dictionary. If you don’t know what that is, a Key-value-pair is almost like an array, but has a unique key associated with data.
So if you create a struct that has two int members in it; x and y, you create a map with that struct as key. You can tell that map to get you the data where the key is X=x and Y=y. The data could be a reference to the spawned actor.

I’ll just screenshot my setup which is a 3d grid but if you only need a 2d grid, it’s easy to adjust.

The cell size is how far apart the actors should spawn, practically determining the size of the cells. The location variable is inserted as the location to spawn the actor. The map has some other functions than the array. Instead of get(), it’s find().
In my case the data is another struct, but it could as well just be an actor reference.

so my idea of placing target points around the map and moving the players to them isn’t possible?
you’re suggesting making a grid instead for the spaces?
I was able to get the dice roll and teleport to target point working… but that was in the level blueprint… which is hard to work with.

Also, I tried spawning the actors in a line, and now it spawns about double the amount of players there are. I’m not understanding why this simple spawning actors isn’t working.

Well, it’s probably possible you just need to think about what you want to do and implement the solution.
You mentioned a grid, so I showed some ways to make it.
As for the level blueprint, during my 1 year of learning ue4, I’ve never touched it and don’t really plan to either.

did they take out the ability to add images on this forum?

Well, I’ve been messing with this a bit. My problem is still spawning characters at target point 0. I need to do this before I can even move forward to the turn rotation.

For whatever reason, I can’t get this to spawn the right amount of actors (1 for each loop of the “Players” array)

I’ve moved the dice roll logic, and all my target point and player movement variables into a game instance blueprint. This is where I hope to put most of the game’s logic.

I even think I managed to get my target points from my level blueprint to my gameinstance. Now the only problem I’m having is spawing these players. There can be up to 16 players… so I just need to figure out how to spawn them in a 4x4 grid.

I tried creating a BP with 16 target points, but I can’t shake the feeling that that is not the correct way for me to do this

also, no matter what I try, it spawns more actors than I tell it to. Very frustrating


It just always spawns more actors than I want.

I put a print string at the end. so I see it’s spawning a few entries of the index multiple times. Does anyone have any idea why this is?

I thought it would only execute the spawn actor once for every entry of the array

ALSO I changed the Collision Handling Override… and it seemed to spawn the correct amount of actors now… but the print string was still telling me that extra players were spawned…

If you changed the collision so that the actor does not block itself practically, then they may be spawning inside each other. To see actors in the world, you can look at the world outliner (top right by default).

The issue is probably one of two: either the wrong number of elements is in the players array (some duplicates) or that function has some logic issue. In order to determine what may be wrong you need to post the whole function.

To debug, print the length of the players array before the loop, and inside the loop print each element.

so are you saying I will be able to see the actors that spawn during runtime in the world outliner while it’s simulating?

I will try your method of debugging… but here are the images of pretty much all my BPs. If there’s anything I left out let me know:

  1. Create Player Input Fields (GameStartFixed)
  2. Add to “Players” Array (GameStartFixed)
  3. Set Player Names And Spawn Player AIs, Then Update Player Names and Set Player Pawns in GameInstance
  4. Print Player Names To Screen (ATFYCRGameInstance)
  5. Attempt
  6. Result

Yes, the world outliner will display spawned actors during PIE (Play in editor).

What happens after that screenshot ends? Is that where the “atfcyr” widget is created? If so, you’re creating a new widget on every loop, each time incrementing the players to the new instance.
So in the example of 3 players showing 6 actors, you can see that when the first widget is created, It only has one player name, outputting name 0 to the screen. The next has 2 players, outputting 0 then 1 to the screen. And so on.

Though this could have happened inside any class, I would avoid putting game logic inside widgets.

oh ****… that needs to be on “completed”, not loop body
ooh this might be the root of the problem. I’ll try to fix this later
and I’ll see about doing my spawning in a different BP (not a widget)

so I got this system working, and the turn rotation. But I forgot to compile before simulating and it somehow permanently crashed my project (won’t open anymore: Why is my engine crashing everytime I try to edit my project - Platform & Builds - Unreal Engine Forums)
fortunately most of my BPs are right here for me to reference lol

Ok, I’ve rebuilt this project and I’m at another roadblock.
And I can’t seem to find an answer anywhere…

I want to have the camera follow the player as he moves to his next destination.
My first attempt I used a move to location node on the player controller. He moved to the correct location and the camera followed him correctly. The only issue with this was I didn’t have a way of determining when the character finished moving to the destination. So I considered using an AI controller so I could use the built in ai components to determine when he completes his move.
I unpossesses the player controller
And possessed with the ai controller
Now the character moves, but the third person camera does not follow the character anymore.
So I’m just looking for a solution.
I need either a way to continue using the camera in the third person BP while the ai controller is controlling the character
Or a way to create a seperate camera in the level to switch to that will follow the active player along his path.
I can’t figure out how to do either of these things.

Here is my trail of destruction:
Forum Post
Answerhub Post 1
Answerhub Post 2

I even have the turn rotation ready. but I need to sort this out first.