0 votes

Hi i have an issue while i was following a video tutorial that shows how to make a skill bar switch.
I get the error Invalid Operands 'Int' and 'String' in operator '+' every time i run the game,
this is the line of code that prompts the error
var skilltexture = load("res://graficos/Skills/" + skillname + ".png")
help?
(graficos is my assets folder)
heres the code
i tried to command the skillname to be converted to a string for this purpose using load("res://graficos/Skills/" + str(skillname) + ".png"

but then i got the error invalid type in Vector2 constructor. cannot convert argument 1 from nil to float.

I really dont know how to proceed, im still a beginner
extends RigidBody2D

var axe_speed 
var life_time = 3
var damage 
var skill_name 


#var fire_direction
#func _init(_skill_name):
# skill_name = _skill_name


func _ready():
 match skill_name:
  "Spear":
   damage = 40
   axe_speed = 200
  "Axe":
   damage = 30
   axe_speed = 400
 var skill_texture = load("res://graficos/Skills/" + skill_name +  ".png")
 get_node("Projectile").set_texture(skill_texture)

 apply_impulse(Vector2(), Vector2(axe_speed, 0).rotated(rotation))
 self_destruct()

func self_destruct():
 yield(get_tree().create_timer(life_time),"timeout")
 queue_free()

func _on_Taxe_body_entered(body):
 get_node("CollisionPolygon2D").set_deferred("disabled", true)
 if body.is_in_group("Enemies"):
  body.on_hit(damage)
 self.hide() 


and in the player script  i have this 


func skill_loop():
 if Input.is_action_pressed("shoot")and can_fire == true:
  can_fire = false
  throwing = true
  speed = 0
  get_node("PlayerAxis").rotation = get_angle_to(get_global_mouse_position())
  var axe_instance = axe.instance()
  axe_instance.position = get_node("PlayerAxis/axePosition").get_global_position()
  axe_instance.rotation = get_angle_to(get_global_mouse_position())
  axe_instance.skill_name = selected_skill
  get_parent().add_child(axe_instance)
  yield(get_tree().create_timer(rate_of_fire),"timeout")
  can_fire = true
  throwing = false
  speed = 70
asked Dec 14, 2019 in Projects by Erraco (17 points)

you have to use str(skillname). Where do you get the error invalid type in Vector2 constructor. cannot convert argument 1 from nil to float. ?

in this line

apply_impulse(Vector2(), Vector2(axe_speed, 0).rotated(rotation))

i Writed as Xtremezero said and declared the var skill_name = " " but then i got the error invalid type in Vector2 constructor. cannot convert argument 1 from nil to float.

1 Answer

+1 vote
Best answer

in the first declaration in line 4 , you wrote this :

var skill_name

but godot can't assume what's the type of it , so instead you should write

var skill_name = ""

This should fix it , (I hope)

answered Dec 14, 2019 by Xtremezero (288 points)
selected Dec 14, 2019 by Erraco

i tried as you said and i get the same error like if i tried to command the skillname to be converted to a string for this purpose using load("res://graficos/Skills/" + str(skillname) + ".png" in the line 21.

then i get this error in the line 24 invalid type in Vector2 constructor. cannot convert argument 1 from nil to float.
applyimpulse(Vector2(), Vector2(axespeed, 0).rotated(rotation))

apply_impulse obviously expects a float variable as first argument, not an empty Vector2().
Did you write that function on your own? Could you please show it?

I declared the variable axespeed as float and the skillname var as Xtremezero said
and now the code works
No, the code is not my own, I have been following the Game Development Center tutorial about skill bars
Thank you guys so much for helping!

ah glad you got it by yourself , I totally forgot to reply to the 2nd part of the question ^^

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.