Hi, I am a beginner I'm trying to make my character change sprite by mouse direction, but it gave me an error. Here is the script


extends KinematicBody2D

var mysprite = "Sprite"

export(Texture) var spr_left: Texture
export(Texture) var spr_right: Texture
export(Texture) var spr_up: Texture
export(Texture) var spr_down: Texture

func _physics_process(delta):
	if get_global_mouse_position().x < self.position.x:
		var sprite = get_node("Sprite")
		mysprite.set_texture(spr_left)
		pass
	elif get_global_mouse_position().x > self.position.x:
		var sprite = get_node("Sprite")
		mysprite.set_texture(spr_right)
		pass
	if get_global_mouse_position().y < self.position.y:
		var sprite = get_node("Sprite")
		mysprite.set_texture(spr_down)
		pass
	elif get_global_mouse_position().y > self.position.y:
		var sprite = get_node("Sprite")
		mysprite.set_texture(spr_up)
		pass

It says "Invalid call. Nonexistent function 'set_texture' in base 'String'." Where did I go wrong?

In your code, mysprite really is a String (line 3), and so doesn't have the method set_texture. Look back at the code and notice that instead of calling from sprite, you called from mysprite!

@SIsilicon28 said: In your code, mysprite really is a String (line 3), and so doesn't have the method set_texture. Look back at the code and notice that instead of calling from sprite, you called from mysprite!

Corrected from "sprite" to "mysprite". But now has just one sprite (spr_up)

Well according to your code, the Sprite texture can only be either spr_up or spr_down because the second if statement chain will almost always override whatever the first if chain did.

Could you show what the new code looks like?

@SIsilicon28 said: Well according to your code, the Sprite texture can only be either spr_up or spr_down because the second if statement chain will almost always override whatever the first if chain did.

Could you show what the new code looks like?

extends KinematicBody2D

var mysprite = "Sprite"

export(Texture) var spr_left: Texture
export(Texture) var spr_right: Texture
export(Texture) var spr_up: Texture
export(Texture) var spr_down: Texture

func _physics_process(delta):
	if get_global_mouse_position().x < self.position.x:
		var mysprite = get_node("Sprite")
		mysprite.set_texture(spr_left)
	elif get_global_mouse_position().x > self.position.x:
		var mysprite = get_node("Sprite")
		mysprite.set_texture(spr_right)
	elif get_global_mouse_position().y < self.position.y:
		var mysprite = get_node("Sprite")
		mysprite.set_texture(spr_down)
	elif get_global_mouse_position().y > self.position.y:
		var mysprite = get_node("Sprite")
		mysprite.set_texture(spr_up)

I think I know what you're trying to achieve. This code should work for you.

extends KinematicBody2D

export(Texture) var spr_left: Texture
export(Texture) var spr_right: Texture
export(Texture) var spr_up: Texture
export(Texture) var spr_down: Texture

onready var my_sprite = $"Sprite"

func _physics_process(delta):
    var vec_to_mouse = (get_global_mouse_position() - position).normalized()

    if abs(vec_to_mouse.x) > abs(vec_to_mouse.y):
        if vec_to_mouse.x > 0.0:
            my_sprite.texture = spr_right
        else:
            my_sprite.texture = spr_left
    else:
        if vec_to_mouse.y > 0.0:
            my_sprite.texture = spr_down
        else:
            my_sprite.texture = spr_up

@SIsilicon28 said: Would you like me to explain to you what I did? :)

I dunno how to explain. Forgive me

Ok dokay!

  • In line 8, onready is a keyword used to initialize a variable when the node and all of its children are ready! It's similar to writing: func _ready(): my_sprite = $"Sprite"
  • In the same line, $ is a shorthand for get_node.
  • Here's the complicated part. First, I need to know which axis the body is pointing towards. That's what the first if is for. If it's true we're facing the x-axis, else we are facing the y-axis. In both cases, we pin point in what direction we are facing and set the sprite texture accordingly.

@SIsilicon28 said: So yeah. You can use that code for your game.

Thank you, but please don't get mad at me because there is another thing to fix, so please have patience with me. I know I'm becoming annoying. Werdly, it only show spr_right and spr_down. I don't think is something about the script

@MagicFool64 said:

@SIsilicon28 said: So yeah. You can use that code for your game.

Thank you, but please don't get mad at me because there is another thing to fix, so please have patience with me. I know I'm becoming annoying. Werdly, it only show spr_right and spr_down. I don't think is something about the script

You don't have to worry about me being mad over your predicaments. I love to help! You're not being annoying. :)

3 years later