This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi,

Sorry for silly question (this is my first 4 hrs with Godot).

I'm unable to access set_pos method from Node2D object

Error message:
Invalid call. Nonexistent function 'set_pos' in base 'CanvasLayer'.

Snippet

extends KinematicBody2D

var motion = Vector2(0, 0)
const SPEED = 1000
const GRAVITY = 300
const UP = Vector2(0, -1)
const JUMP_SPEED = 4000
const WORLD_LIMIT = 8000

signal animate

func _ready():
    pass

func _physics_process(delta):
    apply_gravity()
    jump()
    move()
    animate()
    move_and_slide(motion, UP)
    touch_controls()

func touch_controls():
    #   get_node("Camera2D/LeftCanvas").position = $Camera2D.get_camera_position()
#   $Camera2D.get_camera_position()
    var viewport = get_node("/root").get_children()[0].get_viewport_rect().size
    var viewport_res = get_node("/root").get_children()[0].get_viewport_rect().size
    var viewport_scale = 600/viewport.y

    $Camera2D/LeftCanvas/LeftControls.set_pos(Vector2(0, 500))
    print(viewport_res)


func animate():
    emit_signal("animate", motion)

func apply_gravity():
    if position.y > WORLD_LIMIT:
        end_game()
    if is_on_floor():
        motion.y = 0
    elif is_on_ceiling():
        motion.y = 2
    else:
        motion.y += GRAVITY

func end_game():
    get_tree().change_scene("res://Scenes/GameOver.tscn")

func jump():
    if Input.is_action_pressed("Jump") and is_on_floor():
        motion.y -= JUMP_SPEED

func move():
    if Input.is_action_pressed("Right") and not Input.is_action_pressed("Left"):
        motion.x = SPEED
    elif Input.is_action_pressed("Left") and not Input.is_action_pressed("Right"):
        motion.x = -SPEED
    else:
        motion.x = 0
in Engine by (14 points)

1 Answer

0 votes

You have a couple of problems here:

  • There is no method called set_pos() in Godot - not since Godot 3.0 was released. Names were changed: pos to position, rot to rotation, etc. I suspect you may be looking a very old tutorial.

  • You're trying to set a position on a CanvasLayer. CanvasLayer objects don't have a position. https://docs.godotengine.org/en/latest/classes/class_canvaslayer.html

  • You're not using move_and_slide() correctly. In order for is_on_floor() to work properly, you must apply constant gravity, not remove it when on the ground. move_and_slide() returns a modified velocity vector for just this reason.

Since you're new to Godot, I highly recommend you look at the official docs. The best place to start is here:

https://docs.godotengine.org/en/latest/getting_started/step_by_step/index.html

And there is information about using kinematic bodies here:
https://docs.godotengine.org/en/latest/tutorials/physics/physics_introduction.html
https://docs.godotengine.org/en/latest/tutorials/physics/using_kinematic_body_2d.html

by (22,191 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.