I attempted to follow what you mentioned in your newest answer, but I am receiving an error only when I am running the game. Here's the error.
Invalid call. Nonexistent function 'set_position' in base 'Nil'.
and additional error I found that related to line 6 of Global.gd
E 0:00:00.972 _load: Resource file not found: res://.
<C++ Error> Condition "!file_check->file_exists(p_path)" is true. Returned: RES()
<C++ Source> core/io/resource_loader.cpp:282 @ _load()
<Stack Trace> Global.gd:6 @ change_scene()
CollisionShape2D.gd:7 @ _on_Area2D_body_entered()
I'm still learning the language but does this mean it is basically not connected to something? Here is my script for Global.gd, which is also placed in autoload. My error is at player.setposition(playerpos) or line 7
extends Node
var player
func change_scene(new_scene, player_pos):
get_tree().change_scene(new_scene)
player.set_position(player_pos)
And here is my script for my collisionshape2d:
extends CollisionShape2D
export (String, FILE, "*.tscn") var world_exit
export(Vector2) var player_pos
func _on_Area2D_body_entered(body):
Global.change_scene(world_exit, player_pos)
I think I'm forgeting a step but unsure, If needed here is the player script as well.
extends KinematicBody2D
const PlayerHurtSound = preload ("res://Player/PlayerHurtSound.tscn")
export var ACCELERATION = 500
export var MAX_SPEED = 100
export var ROLL_SPEED =125
export var FRICTION = 500
export var ATTACK_MOVEMENT = 100
enum {
MOVE,
ROLL,
ATTACK
}
var state = MOVE
var velocity = Vector2.ZERO
var roll_vector = Vector2.DOWN
var stats = PlayerStats
var player
onready var animationPLAYER = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get('parameters/playback')
onready var swordHitbox = $HitboxPivot/SwordHitbox
onready var hurtbox = $HURTBOX
onready var blinkAnimationPlayer = $BlinkAnimationPlayer
func _ready():
randomize()
stats.connect("no_health", self, "queue_free")
animationTree.active = true
swordHitbox.knockback_vector = roll_vector
func _physics_process(delta):
match state:
MOVE:
move_state(delta)
ROLL:
roll_state()
ATTACK:
attack_state()
func move_state(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
roll_vector = input_vector
swordHitbox.knockback_vector = input_vector
animationTree.set("parameters/IDLE/blend_position", input_vector)
animationTree.set("parameters/RUN/blend_position", input_vector)
animationTree.set("parameters/ATTACK/blend_position", input_vector)
animationTree.set("parameters/ROLL/blend_position", input_vector)
animationState.travel("RUN")
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
animationState.travel("IDLE")
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
move()
if Input.is_action_just_pressed("ROLL"):
state = ROLL
if Input.is_action_just_pressed("ATTACK"):
state = ATTACK
func roll_state():
velocity = roll_vector * ROLL_SPEED
animationState.travel("ROLL")
move()
func attack_state():
velocity = roll_vector * ATTACK_MOVEMENT
animationState.travel("ATTACK")
move()
func move():
velocity = move_and_slide(velocity)
func roll_animation_finished():
velocity = velocity * 1
state = MOVE
func attack_animation_finised():
state = MOVE
func _on_HURTBOX_area_entered(area):
stats.health -= area.damage
hurtbox.start_invinciblity(0.6)
hurtbox.create_hit_effect()
var playerHurtSound = PlayerHurtSound.instance()
get_tree().current_scene.add_child(playerHurtSound)
func _on_HURTBOX_invincibility_started():
blinkAnimationPlayer.play("START")
func _on_HURTBOX_invincibility_ended():
blinkAnimationPlayer.play("STOP")