First game problems. (snake type game - GD 4.2.1)

Godot Version



Hi, I’ve started learning Godot. On my first target I’ve choosen snake game but it’s axolotl with fish.
I launched some tutorial videos but… they are on Godot 3.0, on 4.0 version and I can’t find snake with TileMap.
I wrote this code and started to analyze but after some hours still don’t know what causes my error
“Nonexistent function size in base int” I’ve tried to look for repair for this error but every try finished with new errors…
It’s 28-29 line’s fault. Please for help. If you have better tutorials for Godot 4.0 for beginners I will be pleased too.

PS. I know about what “int” is etc but i don’t have knowledge how to repair this error in Godot. I am a little confused :confused:

My code:

on line 29, the insert function doesn’t return the new array, it only returns an error code (int) which you assigned to axolotl_body, that’s causing the issue

I think something like this would work instead:

body_copy.insert(0, new_head)
axolotl_body = body_copy
1 Like

if you are starting, force yourself to declare typed variables and don’t lose that good habit.

if instead of this

func move_axolotl(move_direction):
    var body_copy = axolotl_body.slice(0, axolotl_body.size() - 2)
    var new_head = body_copy[0] + move_direction
    axolotl_body = body_copy.insert(0, new_head)

you had done this

var axolotl_body:Array[Vector2] = [Vector2(5, 5), Vector2(4, 5), Vector2(3, 5)]
func move_axolotl(move_direction:Vector2):
	var body_copy:Array[Vector2] = axolotl_body.slice(0, axolotl_body.size() - 2)
	var new_head:Vector2 = body_copy[0] + move_direction
	axolotl_body = body_copy.insert(0, new_head)

you will get

Error at (29,20) Value of type “int” cannot be assigned to a variable of type “Array[Vector2]”.

It may cost you a little more to type the variables, but it will avoid certain errors and clarify others

Thanks for help.
@Monday Your solution works only partially. The code runs once, but the next loop causes another error and crash.
"Out of bounds get index ‘0’ (on base:‘Array[Vector2]’)
Maybe I have made too much mess in the code. I tried to add some own solution. :sweat_smile:

@Batiste thanks for the tips. I will try to develop my skills :slight_smile:

if i understand correctly you’re trying to remove the last element of axolotl_body with slice? in that case i should be size() - 1 not -2.
it can be done like this, too: var body_copy = axolotl_body.slice(0, -1)

edit: also you can assign the result of slice back to to axolotl_body, no need to create a new array: axolotl_body = axolotl_body.slice(0, -1)
but if you think it’s cleaner your way then it’s fine

Thanks, now it works. I fought like this at first but the tutorial was wrong.

1 Like

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