+2 votes

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.
https://www.youtube.com/watch?v=R0XvL3_t840&list=PL9FzW-m48fn2SlrW0KoLT4n5egNdX-W9a&index=16

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
in Engine by (38 points)

what line of code is the error referring to?

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)

Please reply

1 Answer

+2 votes
Best answer

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()
by (451 points)
selected by

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?

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

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.

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

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.

yeah that was my guess. 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 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 webmaster@godotengine.org with your username.