0 votes

For some reason it crashes my game when my character gets into range for my enemy to attack. It has no problem detecting the player, but I keep getting this error that states...

Invalid type in function 'getangleto' in base 'KinematicBody2D (Elite.gd)'. Cannot convert argument 1 from Nil to Vector2.

I've listed portions relevant to this script below. I hope someone can help!

func Attack():
canfire = false
speed = 0
fire
direction = (getangleto(playerposition)/3.14)*180
get
node("TurnAxis").rotation = getangleto(playerposition)
var skill = load("res://Enemyranged.tscn")
var skill
instance = skill.instance()
skillinstance.skillname = "Enemyranged"
skillinstance.firedirection = firedirection
skill
instance.rotation = getangleto(playerposition)
skill
instance.position = getnode("TurnAxis/CastPoint").getglobalmouseposition()
skillinstance.origin = "Enemy"
get
parent().addchild(skillinstance)
yield(gettree().createtimer(0.6), "timeout")
can_fire = true
speed = 0

func onArea2Dbodyentered(body):
if body == player:
playerinrange = true
state = "Attack"
print("You're in range: ", playerinrange)

func onArea2Dbodyexited(body):
if body == player:
playerinrange = false
state = "Stand"
print("You're out of range: ", playerinrange)

func SightCheck():
if player
inrange == true:
var space
state = getworld2d().directspacestate
var sightcheck = spacestate.intersectray(position, player.position, [self], collisionmask)
if sightcheck:
if sight
check.collider.name == "Camera":
playerinsight = true
playerposition = player.getglobalposition("Camera")
state = "Attack"
else:
player
in_sight = false
state = "Stand"

in Engine by (12 points)

Is Attack() being called before SightCheck? Then player_position will not be set.

Use the code tag for better code.

1 Answer

0 votes

From the error, it seems that your player_position variable is null when your Attack function is called (at least on this particular call).

by (10,896 points)
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.