So whenever I run the code I get the error Invalid get index ‘global_position’ (on base ‘Nil’) and I basically just don’t know what that means or how to go about fixing it because I am an absolute noob and couldn’t find anything too specific online. Tried moving the var around a bit but still the same.
The part of the code with global_position:
extends CharacterBody2D
var SPEED = 50
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
var player
var chase = false
func _physics_process(delta): #Frog gravity.
velocity.y += gravity * delta
if chase == true:
var direction = (player.global_position - self.global_position).normalized()
if direction.x > 0:
get_node(“AnimatedSprite2D”). flip_h = true
else:
get_node(“AnimatedSprite2D”). flip_h = false
velocity.x = direction * SPEED
move_and_slide()
Thanks for the reply, I tried adding player = “Player” and now it says it is invalid on base string instead, tried adding str (I think it defines strings) to it and that just made 10 more problems so is there a better way to go about this or am I just being stupid?
I assume the player variable is supposed to be a reference player scene / node. Can’t really tell how to add the player without seeing your scene tree.
Looking at the whole of your script, it looks like it’s an NPC or enemy.
In this case you need to set the reference to the player to get its properties.
(a null check alone won’t make this work).
Assuming your player nodes is within the same scene tree as this node, I’d suggest adding the following method before your _physics_process:
func _ready():
for node in get_tree().get_nodes_in_group("Player"):
player = node
Then also make sure your player node is part of a group called “Player”.
This way, every entity with this script can find and set a reference to the player.
Godot Game Development – Crash Course for Beginners on youtube by free code camp dot org if you would like to look at the tree, its what I have been following. Although I have had to modify the code a bit as his didn’t work for me. Np though thanks for weighing in.
Thanks a lot for the info! as mentioned in response to Monday my code is essentially the same as the Godot Game Development – Crash Course for Beginners on youtube by free code camp dot org, though have modified this section to suit me better. Its an enemy frog I’m trying to get to find and chase the player, his section on it is around 1:02:24 if your interested. As far as I am aware player node is in same tree and also in a group called player as outlined in tutorial, will give your method a go and let you know how it goes. Sorry for the paragraph and thanks for the reply!
Yeah, I tried it as in the video and for some reason it didn’t work for me so someone recommended I try it some different things. Also think I tried that but will def try it again in case I am being silly!
UPDATE: So gonna be putting my coding adventures on pause at the moment, haven’t been able to get round to trying all these ideas but certainly will when I resume. Thanks all for the suggestions!