#Calling variable from another module works for one function but not another. Why?

1 messages · Page 1 of 1 (latest)

delicate brook
#

For some reason I get the error, "Expected closing ")" after call arguments." for the last line in the function:

func _process(delta: float) -> void:
    if Input.is_action_just_pressed("melee"):
        is_attacking = true
        melee(is_jumping: bool, is_falling: bool)

is_falling and is_jumping are defined and called from another module. They work, only the melee function is busted.

func _physics_process(delta: float) -> void:
    gravity_component.handle_gravity(self, delta)
    movement_component.handle_horizontal_movement(self, input_component.input_horizontal)
    jump_component.handle_jump(self, input_component.get_jump_input(), input_component.get_jump_input_released())
    animation_component.handle_move_animation(input_component.input_horizontal, gravity_component.is_falling, jump_component.is_jumping)
    animation_component.handle_jump_animation(jump_component.is_going_up, gravity_component.is_falling)
    animation_component.melee(gravity_component.is_falling,jump_component.is_jumping)

If I remove the ": bool" from the melee function call, it gives the error, "Identifier "is_jumping" not declared in the current scope."

If I leave it as "melee()", it tells me it expects 2 arguments.

I can make it kind of work by defining the variables again locally and not call on a different module, but I want to keep my code clean and other functions are calling on the same variables successfully. For example:

func handle_move_animation(move_direction, is_jumping: bool, is_falling: bool) -> void:
    handle_horizontal_flip(move_direction)
  
    if move_direction != 0 and not is_jumping and not is_falling and not is_attacking and not is_look_down and not is_look_up:
        sprite.play("run")
                
    elif not is_attacking:
        sprite.play("idle")

Could someone please explain why calling the same variable from another module works on one function and not the other?

dusk thicket
#

Can you show the whole script?

#

And a screenshot of the error in action.

delicate brook
#
class_name AnimationComponent
extends Node

@export_subgroup("Nodes")
@export var sprite: AnimatedSprite2D

#var move_direction: float
var is_attacking: bool
var is_look_up: bool
var is_look_down: bool


func _process(delta: float) -> void:
    if Input.is_action_just_pressed("melee"):
        is_attacking = true
        melee()
        
    if Input.is_action_pressed("look_up"):
        is_look_up = true
    
    if Input.is_action_just_released("look_up"):
        is_look_up = false
    
    if Input.is_action_pressed("look_down"):
        is_look_down = true
        
    if Input.is_action_just_released("look_up"):
        is_look_down = false
        
        
func handle_horizontal_flip(move_direction: float) -> void:
    if move_direction == 0:
        return
    
    sprite.flip_h = false if move_direction < 0 else true

func handle_move_animation(move_direction, is_jumping: bool, is_falling: bool) -> void:
    handle_horizontal_flip(move_direction)
  
    if move_direction != 0 and not is_jumping and not is_falling and not is_attacking and not is_look_down and not is_look_up:
        sprite.play("run")
        
    if move_direction != 0 and not is_jumping and not is_falling and not is_attacking and not is_look_down and is_look_up:
        sprite.play("look up")

    if move_direction != 0 and not is_jumping and not is_falling and not is_attacking and is_look_down and not is_look_up:
        sprite.play("look down")
        
    elif not is_attacking:
        sprite.play("idle")
        
func handle_jump_animation(is_jumping, is_falling) -> void:
    if is_jumping and not is_attacking:
        sprite.play("jump")
    elif is_falling and not is_attacking:
        sprite.play("fall")

func melee(is_jumping: bool, is_falling: bool) -> void:
    if is_attacking and not is_falling and not is_jumping and not is_look_up and not is_look_down:
        sprite.play("ground attack side")
        is_attacking = false


        

dusk thicket
#

melee takes 2 parameters, you are calling it without any

delicate brook
#

There's a lot more if statements under melee, but I went over the character limit. The rest of the function is below. I has it's own set of issues.

func melee(is_jumping: bool, is_falling: bool) -> void:
    if is_attacking and not is_falling and not is_jumping and not is_look_up and not is_look_down:
        sprite.play("ground attack side")
        is_attacking = false

    if is_attacking and is_look_up and not (is_falling and is_jumping):
        sprite.play("ground attack up")
        is_attacking = false
    
    if is_attacking and is_jumping and not is_look_up:
        sprite.play("jump attack side")
        is_attacking = false

    if is_attacking and is_look_up and not is_jumping:
        sprite.play("jump attack up")
        is_attacking = false
        
    if is_attacking and is_look_down and is_falling:
        sprite.play("fall attack down")
        is_attacking = false
        
    if is_attacking and is_falling and not is_look_down and not is_look_up:
        sprite.play("fall attack side")
        is_attacking = false
        
    if is_attacking and is_falling and is_look_up:
        sprite.play("fall attack up")
        is_attacking = false
        
    is_attacking = false
        
func reset_animation():
    if not is_attacking and not is_falling and not is_jumping:
        if move_direction != 0:
            sprite.play("run")
        else:
            sprite.play("idle")
        
    if not is_attacking and is_jumping or is_falling:
        if is_jumping and not is_attacking:
            sprite.play("jump")
        elif is_falling and not is_attacking:
            sprite.play("fall")
dusk thicket
#

I mean, first you should fix the displayed error by using parameters on the call.

delicate brook
dusk thicket
#

ah sorry, they where pretty small in my screen.
You have to pass values as parameters.

Like: melee(true, false)
Or a variable of the given type. Whatever evaluates to the type it asks for. (2 booleans in this case)

delicate brook
#

The other 2 ways I wrote it was

melee(is_jumping: bool, is_falling: bool)
melee(is_jumping, is_falling)

I expected the top one to work.

#

That's how I used it in the other function.

#

handle_move_animation

dusk thicket
#

The bottom one would work if you had a is_jumping and is_falling variable defined.

delicate brook
#

I know. I managed to get it to kind of work doing that, but it caused other issues with it being redundant and I want to keep my code as clean as I can manage, which isn't saying much.

dusk thicket
#

The one above is pretty clean. Proper spacing does a lot of the work in organization.

delicate brook
#

I mean, I SHOULD be able to call on the same variables in one function that works with another function. At least, I would think.

dusk thicket
#

One thing i noticed, this here does nothing. Since values are not passed as references.

dusk thicket
#

If both scripts have the same variables, then yes. But each script is isolated from the rest.

#

Except for cases of inheritance.

delicate brook
#

It actually does work. Ground attack animations are the only ones that do work.

#

Well, they did when I had the variables defined in the same script.

#

The functions that work "handle_jump_animation" and "handle_move_animation" are both in this script with my melee function, and they call on the same variables from another script. They should work. What am I missing?

dusk thicket
#

No, they are taking those variables from the function parameters.
That is why they can reference is_jumping and is_falling. They are part of the function.

#

Even if said variables where present, since they have the same name. It would use those inside the function instead.

#

That is why it is important to name everything differently. Otherwise you end up with "shadowing" (where it isn't sure which of the ones with the same name to use)

delicate brook
#

How are they not part of the melee function?

#

I'm using them in the exact same way.

dusk thicket
#

They are. But when you call you need to give them a value

#

You define their name, but not what they contain.

#

Their contents are defined when you call.

#

If you did handle_jump_animation(true, true)
Now is_jumping and is_falling would both be true during that call.

delicate brook
#

But handle_jump_animation works as written. It gets the bool value from another script.

#

This script worked until I added the melee function

dusk thicket