Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python in Godot

Tags:

python

godot

I understand that this may be a niche question but I'm trying to understand how to work in Godot with Python. I'm using the PythonScript Library Version 0.5 and I have the following code:

from godot import exposed, export
from godot import *


@exposed
class Node2D(KinematicBody2D):

    speed = 50
    
    def _physics_process(self,delta):
        velocity = Vector2.ZERO
        if Input.is_action_pressed('ui_up') == True:
            velocity.y -= 1*speed
            
        if Input.is_action_pressed('ui_down') == True:
            velocity.y += 1*speed
            
        move_and_slide(velocity)

In it's current state, when I run this, it throws "NameError: name 'move_and_slide' is not defined" although move_and_slide is clearly listed in the KinematicBody2D Methods.

Thanks a lot in advance for your feedback and please let me know if I can clarify this question further.

like image 668
Difio Avatar asked Oct 24 '25 10:10

Difio


1 Answers

I actuall found the issue! I needed to add a self. in front of the self.move_and_slide(). Correct code below:

from godot import exposed, export
from godot import *


@exposed
class Node2D(KinematicBody2D):

    
    
    def _physics_process(self,delta):
        speed = 50
        velocity = Vector2.ZERO
        if Input.is_action_pressed('ui_up') == True:
            velocity.y -= 1*speed
        
        if Input.is_action_pressed('ui_down') == True:
            velocity.y += 1*speed
            
        self.move_and_slide(velocity)

I also needed to move speed in the physics_process function, of which I'm currently not 100% sure why it wasn't noticed outside.

like image 101
Difio Avatar answered Oct 27 '25 00:10

Difio