0 votes

https://files.fm/f/4dg84ghff

heres a link to the video.

The weapon is an inheritable scene that is inherited by other weapons. They all have the code to flip the sprite image horizontally towards to the direction of the mouse from the player body. It also has the code that allows it to move around the player which works well but for some reason the flip doesnt work when it is instanced by the player but when played as just the scene (F6) it works properly as seen from the start of the video.

edit:
my bad, newbie here sorry, here's the code for the player spawning the weapons.
extends KinematicBody2D

const BASED_MOVEMENT_SPEED = 10 * 16

var desired_velocity = Vector2.ZERO
var velocity = Vector2.ZERO
var direction = Vector2.ZERO
var weight = 0.3
var weapon_change = "sword";
var weapons: = {
    sword = preload("res://Scenes/Weapon.tscn"),
    rock  = preload("res://Scenes/Rock.tscn"),
    claw  = preload("res://Scenes/Claw.tscn")
    }

onready var target = Vector2.ZERO
onready var weapon_position = $weapon_manager

func _process(delta):
    _movement_input()
    _weapon_movement()

func _weapon_change():
    if (Input.is_action_just_pressed("Switch_Item1")):
        weapon_change = "sword"
    elif (Input.is_action_just_pressed("Switch_Item2")):
        weapon_change = "rock"
    elif (Input.is_action_just_pressed("Switch_Item3")):
        weapon_change = "claw"

func _movement_input():
    direction =  Vector2.ZERO
    direction.x = -int(Input.is_action_pressed("ui_left")) + int(Input.is_action_pressed("ui_right"))
    direction.y = -int(Input.is_action_pressed("ui_up")) + int(Input.is_action_pressed("ui_down"))

    desired_velocity = direction.normalized() * BASED_MOVEMENT_SPEED
    velocity = velocity.linear_interpolate(desired_velocity, weight)
    velocity = move_and_slide(velocity)

func _weapon_movement():
    _weapon_change()
    for weapon in weapon_position.get_children():
        weapon.queue_free()
    var weapon_instance = weapons[weapon_change].instance()
    weapon_position.add_child(weapon_instance)
    target = get_global_mouse_position()
    weapon_position.look_at(target) 

And Here's the script for the weapon that is instanced by the player

    extends Node2D

onready var target = Vector2.ZERO
# Called when the node enters the scene tree for the first time.
func _process(delta):
    target = get_global_mouse_position()
    if target.x > self.position.x:
        get_node("Sprite").set_flip_v(false) 
    elif target.x < self.position.x:
        get_node("Sprite").set_flip_v(true)
Godot version 3.2.2
in Engine by (12 points)
edited by

1 Answer

0 votes

You should better post your code instead of videos :)
Show at least code of flipping in weapon class and code of moving around the player

by (1,110 points)

my bad, i just added the code to the original question.

I think I can seen what is going on
Weapon_movement() function is in process(), every frame You create instance, add child and remove it. What we see in video is not one weapon, but thousands of weapons being created and destroyed in a blink, so the inherited code to flip sprite according to mouse position never happens.

You need to move lines of code with removing weapons, creating instance and adding child into weaponchange() function, and only leave lines about lookat() in weapon_movement().

Also You don't have to check for input in process(), there is a built-in input(event) function, and it should be enough for your weapon_change()

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.