0 votes

(First post and new to everything, forgive me)

I'm setting the framework for a game where a driver can switch vehicles Code is bare bones at the moment just to test how some things work.

Right now a key press should queuefree() the player, shift camera focus to the vehicle and movement controls become enabled for the vehicle. The camera focus will change but isplayer never seems to change to true. If I just declare is_player as true everything works, even up to when I exit the vehicle and spawn the driver back in and lose control of the vehicle again.

extends KinematicBody2D

const PLAYER = preload("res://Player.tscn")
const SPEED = 100
const GRAVITY = 10
const FLOOR = Vector2(0, -1)

var velocity = Vector2()

var is_player = false
var is_dead = false

func _physics_process(delta):

velocity.y += GRAVITY
velocity = move_and_slide(velocity, FLOOR)

if is_dead == false:

    if Input.is_action_pressed("enter_vehicle"):
        $Camera2D.make_current()
        var is_player = true

    if is_player == true:

        if Input.is_action_pressed("move_right"):
            velocity.x = SPEED
            $Sprite.flip_h = false

        elif Input.is_action_pressed("move_left"):
            velocity.x = -SPEED
            $Sprite.flip_h = true
        else:
            velocity.x = 0

        if Input.is_action_pressed("exit_vehicle"):
            var player = PLAYER.instance()
            get_parent().add_child(player)
            player.position = $Exit.global_position
            is_player = false
in Engine by (19 points)

1 Answer

+3 votes
Best answer

Looks like you're referencing 2 different is_player vars. A global one, created outside the function and a local one, created inside the function. I'd assume you want to change this:

    $Camera2D.make_current()
    var is_player = true

to instead be this:

    $Camera2D.make_current()
    is_player = true

Note, I dropped the var that was making that a new, different variable.

by (13,806 points)
selected by

I knew I had to be overlooking something silly. Thanks!

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.