0 votes

When the sprint button is pressed, the value of "maxSpeed" is reasigned in "statSelector" function, but with the same value and the player moves faster, How is that possible if the value is the same?

Array content:

signalData["Player", "walk", bool, Vector2.ZERO]
Vector2.ZERO: normalized vector from input
bool: true, when sprint button is pressed and false when it's not

"PlayerStats" singleton values:

extends Node
const aceleration : int = 500
const maxSpeed : int = 200
const friction : int = 500

extends KinematicBody2D
    signal moveWalk
    var aceleration : float = 0
    var maxSpeed    : float = 0
    var friction    : float = 0
    var positionVar : Vector2 = Vector2.ZERO

func statSelector(signalData):
    if signalData[0] == "player":
        friction    = PlayerStats.friction     
        aceleration = PlayerStats.aceleration  
        maxSpeed    = PlayerStats.maxSpeed     
        if signalData[1] == "walk":
            print(signalData[2])
            if signalData[2] == true:
                maxSpeed = PlayerStats.maxSpeed
            walk(signalData)


func walk(signalData):
    if signalData[3] != Vector2.ZERO:
        positionVar = positionVar.move_toward(signalData[3] * maxSpeed, aceleration)
        emit_signal("moveWalk", positionVar)
    else:
        positionVar = positionVar.move_toward(Vector2.ZERO, friction)
        emit_signal("moveWalk", positionVar)

"Player" scene node script that receive "positionVar" to move

extends KinematicBody2D
onready var actionsNode = get_node("Actions/Movements")

func _ready():
    actionsNode.connect("moveWalk", self, "walkPlayer")

func walkPlayer(positionVar):
    var _velocity = move_and_slide(positionVar)
Godot version v3.5.stable.official [991bb6ac7]
in Engine by (12 points)

Bug is not here
Show assigning stats and whole Input code.

Effectively, the bug was in the input code, i managed to fix it, but i don't how and I still don't know why the bug occurred

New code with bug fixed

"Control" scene node

extends Node    
var signalData : Array = ["player", "signalType", false, Vector2.ZERO, false]
    var signalType : String 
    signal playerInputSignal 

func _physics_process(_delta):
    walkInputDetector()
    sprintInputDetector()

func signalEmitter(_datos):
    emit_signal("playerInputSignal", signalData)

func walkInputDetector():
    var walkInput : Vector2 = Vector2.ZERO
    walkInput.x = Input.get_action_strength("walk_right") - Input.get_action_strength("walk_left")
    walkInput.y = Input.get_action_strength("walk_down") - Input.get_action_strength("walk_up")
    walkInput = walkInput.normalized()
    signalData[1] = "walk"
    signalData[3] = walkInput
    signalEmitter(signalData)

func sprintInputDetector():
    if Input.is_action_pressed("sprint"):
        signalData[2] = true
    else:
        signalData[2] = false
    signalEmitter(signalData)

func dodgeroll():
    if Input.is_action_pressed("dodgeroll"):
        signalData[4] = true
    else:
        signalData[4] = false
    signalEmitter(signalData)

Old code with the bug

extends Node
var signalData : Array = ["player", "signalType", false, Vector2.ZERO]
var signalType : String 
signal playerInputSignal

func _physics_process(_delta):
    walkInputDetector()
    sprintInputDetector()

func walkInputDetector():
    var walkInput : Vector2 = Vector2.ZERO
    walkInput.x = Input.get_action_strength("walk_right") - Input.get_action_strength("walk_left")
    walkInput.y = Input.get_action_strength("walk_down") - Input.get_action_strength("walk_up")
    walkInput = walkInput.normalized()
    signalData[1] = "walk"
    signalData[3] = walkInput
    emit_signal("playerInputSignal", signalData)

func sprintInputDetector():
    if Input.is_action_pressed("sprint"):
        signalData[2] = true
        emit_signal("playerInputSignal", signalData)

a lot happening in process here.
How does playerInputSignal affect Player ?
Your new code emits it twice every frame all the time, while old code emited it once every frame, and another time when sprint was pressed.

The signal "playerInputSignal" is emitted from "Control" carrying the normalized vector, and the sprint false/true data, the signal is received in "Action" scene node, there "playerStatSelector" is called, where the "PlayerStats" singleton values are asigned to the vars, then in "walk" function is calculated velocity, the result is send throug a signal to "Player" scene node, where "moveandslide" is called using the result (sorry for the mess, is my first time coding something)

Actions scene node

extends KinematicBody2D
onready var controlsNode = get_node("Controls")
onready var movementsNode = get_node("Movements")

func _ready():
    controlsNode.connect("playerInputSignal", self, "callMovement")

func callMovement(signalData):
    $Movements.statSelector(signalData)

Please log in or register to answer this 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 Frequently asked questions and 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 [email protected] with your username.