Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Evan Symonds | |
Old Version | Published before Godot 3 was released. |
Hi, I’m very new to the godot engine (and pretty new to coding altogether) and I’m trying to piece together a very small game. I’m making a very basic ai zombie that I’m hoping to be able to clone. I wrote this script:
extends KinematicBody2D
var speed = 150
func _ready():
set_fixed_process(true)
pass
func _fixed_process(delta):
var move = Vector2()
var body = get_node("player")
if body.get_global_pos() != 0:
move = body.get_global_pos() - self.get_global_pos()
move(move)
and I’m getting the error : ‘Attempt to call function ‘get_global_pos’ in base ‘null instance’ on a null instance.’ I have looked up what it means and people seem to say the path is wrong when that comes up however I have checked it and it is correct.
Please do help and point out all the things I have done wrong as I would love to learn more about this great engine.
Thanks
P.S I have indented all the lines correctly it is just showing up strange
I have fixed your indentation and caps lock issue on the title, to keep indentation on text just select it and use the “code sample” button.
As for the null instances, these are usually null instances, check the tree on the remote inspector while running to be sure the nodes are where you are looking for, in this case, “player” needs to be children of the node that use the script.
eons | 2017-06-16 17:12
Thank you for fixing those things.
I’m confused, how do you find the position of a sibling node then?
Thank you!
Evan Symonds | 2017-06-16 17:15
There are many ways to “navigate” the tree, you can get_tree().get_root()
that gives you the main viewport where all the scenes are added (current and “autoloaded”), then you can use get_node
with a notation like a terminal/console, ‘“scene_root/node1/child1”’.
Is possible to get the sibling of a node with get_parent().get_node("sibling_name")
or (since get node is relative) get_node("../sibling_name")
.
I prefer to use groups so the script won’t depend on the scene structure or even node names, I add the player to the group “player” then get_tree().get_nodes_in_group("player")[0]
(with a size check before accessing “0” if you want it to work when there is no player around)
eons | 2017-06-16 17:44
Thanks, the …/ thing seems to work fine but now it is doing the null instance on get_global_position. Any idea how to fix this.
Sorry for bugging you but I couldn’t find the info anywhere else online.
Evan Symonds | 2017-06-16 17:48
When it stops, check the debugger, remote inspector and look if the node you were trying to get exists (same parent and the exact name).
eons | 2017-06-16 19:27
I just want to find out how to send the position between two kinematic bodies on the same level of the tree. I want my ‘zombie’ to be able to know where my player is. Thanks for helping.
Evan Symonds | 2017-06-16 19:33
How about sending a signal? There’s a video in this series that shows you how.
https://www.youtube.com/playlist?list=PLsk-HSGFjnaFISfGRTXxp65FXOa9UkYc5
whooshfrosted | 2017-06-16 22:21