Making a snake clone. Seeking advice on best ways to increase snake's size and have it follow the head in a snake-like fashion

Currently I have three actors, SnakeHead, SnakeBody, and NomNom. Both SnakeHead and SnakeBody are simple cubes. NomNom is a sphere.

SnakeHead is the player. Upon overlapping a NomNom the NomNom explodes, SnakeHead adds one to a length variable and also spawns a SnakeBody and attaches it to the SnakeHead. Right now I have some simple logic to keep spawning each consecutive SnakeBody -100 world units from the last in the X direction just to show that my logic is working.

I have no idea what the ideal method would be to actually follow the head in a snake-like fashion in Blueprint. Would be grateful for any suggestions and I’m happy to provide any info I may have left out. :slight_smile:

Hm, interesting. Just some thoughts of mine. Could be crap xD

I just thought about a list, where every element knows the one infront and behind him.

So you would make every element of the snake in the same BP but inside the BP you decide what part of the snake it is by looking into the list. Every part that has someone in front and behind is a body part and head/tail dont have someone in front /behind.

Also inside the BP i would make a case for body and tail to follow the one infront. So the tail isnt following the head, but the “guy” infront of him, And that guy is following the one infront etc.

A list like that could be done with 3 arrays. One array for the body part, one array for the infront and one array for the behind one. They don’t need to be in the right order of 0 - n, because you can always add the part somewhere inside that array and just tell them what there next part is. They only need to share the same index. A body with it’s infront and behind guy would be saved for example to index 7. In all 3 arrays the same index!

For your snake it would be stack like, so it’s not that hard. If you add one, you just look what the tail is, set the tails behind guy to the new one and the new ones infront guy to the old tail.

If you change the model depending on the “do i have someone behind me” it would change the model as soon as a new tail is generated.

If you’re going to be making it like the original snake game (in that the blocks instantly move to the next grid tile rather than moving smoothly), the simplest way to do it is actually to take the block at the tail end of the snake and move it to the front (the current head position + one block’s size in the current movement direction), making it the new head.

This way is much easier than moving every one of the blocks forward one space, especially when “forward” can be different for each block. Doing it this way, there’s no distinction between the snake’s body and head segments.

Oh wow, that’s perfect, elegant, simplicity. Can’t believe it didn’t cross my mind, I’ll see how I go with that. Thank you!

Hmm. This is a little hard for me to follow without being at the PC and trying it out. I can grasp most of it but I’ll really need to sit with this idea and try to implement it to see how it goes, I’ll give it a go tonight! In the meantime I’m going to ask this question again, but with a focus on seeing if I can get the SnakeBody to simply follow the head (I have an example this time, too) to see what other ideas come up before I head to bed tonight.

Thanks for your suggestion, it sound feasible, I just have to see how it works to make sure it will be suitable for what I’m after. I do like the initial solution in this thread, but I’m not sure it’s exactly the solution I’d like to implement; I’m not sure I want my snake moving in a grid-like fashion, but we’ll see. :slight_smile:

With this list it could work without gridlike movement. I dont know how fluently it will move, but you can just let a body part follow the one in front if hes more than a few units away. Thinking about it i would make the head an exrta part that represents the player character and is controlable with left and right.

The 3 arrays are one with a pointer to the body/tail parts and 2 int arrays with zhe index of the behind or infront body part inside the first array.

So if you have 3 body parts in 0,1 and 2, then your 0 is the head and has a 1 in thr behind array. The 1 is a body part and has a 2 in behind and a 0 infront and finally the tail has a 1 infront array and nothing in the behind. Or a -1 for NULL since int arrays cant be empty.

But these are just some thoughts. Test it. Maybe it works xD