Getting an error in my code Please Help me !!

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

I am making a 2D platformer game for a game jam and later on publishing it on Google Play Store.
In this game, we control a Game Boy character jumping around platforms dodging enemies and entering a portal to finish the level. I have made 5 types of enemies in which 4 of them are ground enemies, and one is a flying one. I decided to make this flying one to follow the player.
I watched this video and done this so far.

But when I run the scene in the “Level” Node(Where everything is), it would show an error.
Invalid get index ‘global_postion’(on base:KinematicBody2D(Memo.gd))

PLEASE SOLVE MY PROBLEM

Main Player script (Memo)

var velocity = Vector2.ZERO
var coin = 0
const gravity = 35
const jump = -1070
const SPEED = 290


#Movement of Memo
 func _physics_process(delta):

if Input.is_action_pressed("right"):
	velocity.x = SPEED
	$Sprite.play("Walk")
	$Sprite.flip_h = false
elif Input.is_action_pressed("left"):
	velocity.x = -SPEED
	$Sprite.play("Walk")
	$Sprite.flip_h = true
else:
	$Sprite.play("Idle")
	
if not is_on_floor():
	$Sprite.play("Airjump")
	
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = jump

velocity.y = velocity.y + gravity 
velocity = move_and_slide(velocity,Vector2.UP)

velocity.x = lerp(velocity.x,0,0.2)


func _on_Area2D_body_entered(body):
get_tree().change_scene("res://Level1.tscn")

Player Detection Zone

var player = null

func can_see_player():
return player != null

func _on_PlayerDetectionZone_body_entered(body):
player = body



func _on_PlayerDetectionZone_body_exited(body):
player = null

Flying enemy script (Flying Eye)

export var direction = 1
export var ACCELERATION = 300
export var MAX_SPEED = 50
export var FRICTION = 200

onready var PlayerDetectionZone = $PlayerDetectionZone

 var velocity = Vector2.ZERO
enum{
IDLE,
WANDER,
CHASE
 }
  var state = CHASE


 func _ready():
if direction == -1:
	$AnimatedSprite.flip_h = true

func _physics_process(delta):

if is_on_wall():
	direction = direction * -1
	$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h

match state:
	IDLE:
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
		seek_player()
	
	WANDER:
		pass
	
	CHASE:
		var player = PlayerDetectionZone.player
		if player != null:
			var direction = (player.global_postion - global_position).normalized()
			velocity = velocity.move_toward(direction * MAX_SPEED, 
  ACCELERATION * delta)
			
velocity = move_and_slide(velocity)

 func seek_player():
if PlayerDetectionZone.can_see_player():
	state = CHASE

what line of code is the error referring to?

Millard | 2020-10-19 05:08

When I run the main level where everything is like the player, enemies, tiles, etc
it shows me an error of

Invalid get index ‘global_postion’(on base:KinematicBody2D(Memo.gd))

in this line of code
Flying enemy script (Flying Eye)

CHASE:
        var player = PlayerDetectionZone.player
        if player != null:
            var direction = (player.global_postion - global_position).normalized() # IN THIS LINE OF CODE
            velocity = velocity.move_toward(direction * MAX_SPEED, 
  ACCELERATION * delta)

Siddharth Pushkar | 2020-10-19 05:33

Please reply

Siddharth Pushkar | 2020-10-19 09:56

:bust_in_silhouette: Reply From: Asthmar

Looks like a simple spelling error

you have var direction = (player.global_postion - global_position).normalized()

should be

var direction = (player.global_position - global_position).normalized()

glad someone found the problem. curious though, I don’t see anywhere in your code that you checked to make sure the body you collide with is your player, so your enemy could be chasing a lot of stuff. Also, could someone explain the error to me? It says Invalid get index ‘global_postion’(on base:KinematicBody2D(Memo.gd)). I know what all the rest means, but it looks like its trying to get the global position of Memo.gd. Is thats whats happening or do I not understand how the error information works?

Millard | 2020-10-19 16:17

From my understanding Memo is his player movement script, and the the error had to do with trying to get the players position.

Asthmar | 2020-10-19 16:26

my bad. I still don’t see any code that checks to make sure the player is the player. All it seems to do is make sure that something is in the area detection node. It doesn’t check the player name or anything. huh.

Millard | 2020-10-19 16:31

still it doesn’t seem to be causing him any problems.

Millard | 2020-10-19 16:33

oh yea he needs to put his player in a group maybe. But he probably only has one body in his scene, so for now it wont cause problems.

Asthmar | 2020-10-19 16:43

yeah that was my guess. thanks.

Millard | 2020-10-19 17:24