For my code to work I have to add "self" to the method call. Trying to figure out when that should be required.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DVCode

On the code below if I don’t put ‘self’ on the move_and_slide() call I get an error that says
“Function “move_and_slide()” not found in base self.”
Which is confusing, because when I happened to add self to the call, the code starts working exactly as expected!

The root node in this scene is a CharacterBody2D. This is a small test project I’m doing to learn Godot. CharacterBody2D seems to replace KinematicBody2D in Godot 4?

   func _process(delta):
	self.velocity = Vector2(10,0)
	print (position)
	print (self.position)

hmm, you shouldn’t need to add self, that sounds like a bug
could you show more of your script? and maybe make a bug report at Sign in to GitHub · GitHub

rune-scape | 2022-12-14 08:02

Try this in beta 7.

magicalogic | 2022-12-14 08:13

You say the root node in this scene is CharacterBody, but what node is this script attached to, and what does it say at the top of the script after “extends…”?

SteveSmith | 2022-12-14 12:42

The script is only attached to the scene that has the CharacterBody2D. It extends ‘Node2D’.

I am doing something that is less conventional by instantiating everything by code on my main ‘World’ scene (Node2D, extends Node). The World scene has no children showing in the tree.

I’ll try adding my CharacterBody2D to my world scene, and see if that makes a difference.


DVCode | 2022-12-14 15:49

The script is only attached to the scene that has the CharacterBody2D. It extends ‘Node2D’.

There’s your problem. It needs to extend CharacterBody2D if you want it to have the method move_and_slide.

SteveSmith | 2022-12-14 15:52

this is embarassing… I was using v4b7. I had downloaded beta 8, but never extracted it. Sorry.
So I just tried in actual v4 beta 8, and it behaved exactly the same. I haven’t tried it in 3.5 yet, maybe I’ll do that. I figure if it works there, it might be a bug.
As a beginner I probably shouldn’t be using the betas anyway. It’s just me wanting latest and greatest, although 3.5 can do more than I’ll ever need.

DVCode | 2022-12-14 15:58

Ah, thanks. Now that I think about it, that makes sense.
So when I explicitly use ‘self’ then it would reference CharacterBody2D, but without ‘self’ the implied ‘self’ is Node2D, which doesn’t have that method.

I think I know how that happened, and will be more careful in the future.
Thanks for your time, i learned something here!

DVCode | 2022-12-14 16:01