Boolean condition not changing on key press

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

(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 queue_free() the player, shift camera focus to the vehicle and movement controls become enabled for the vehicle. The camera focus will change but is_player 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"):
		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
			velocity.x = 0
		if Input.is_action_pressed("exit_vehicle"):
			var player = PLAYER.instance()
			player.position = $Exit.global_position
			is_player = false
:bust_in_silhouette: Reply From: jgodfrey

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:

    var is_player = true

to instead be this:

    is_player = true

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

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

Kutkulio | 2020-02-06 02:11