Calling function into another

extends Sprite2D

var frequency_global = false
var height_global = false
@onready var texture_rect: TextureRect = $TextureRect
var my_global = true

func _ready() :
frequency_global = false
height_global = false
_on_h_slider_2_value_changed(frequency_global)
_on_h_slider_value_changed(height_global)
if height_global== true && frequency_global == true:
texture_rect.modulate = Color(255,255, 0, 255)

func _on_h_slider_value_changed(value):
if value == 4 || value == 5 || value == 6:
height_global == true

func _on_h_slider_2_value_changed(value):
if value == 5 || value == 6 || value == 7:
frequency_global == true

Question

I am receiving an error of “Invalid operands ‘bool’ and ‘int’ in operator ‘==’.” in
if value == 5 || value == 6 || value == 7:
I am quite new so I am struggling, I wanted to know how to fix it! Thank you all!

So, you’re doing this:

_on_h_slider_2_value_changed(frequency_global)

And frequency_global is set to false, which is a boolean.

That means that in this function:

func _on_h_slider_2_value_changed(value):
  if value == 5 || value == 6 || value == 7:
    frequency_global == true

We’ve passed in a bool as the value parameter, which you then try to compare to various integers. The error message is telling you that that doesn’t make sense.

Which value is it you actually want to compare to those numbers?

1 Like

I want to compare the values of my horizontal slider so that if it falls within the specified range of 5-7 it will return true and if it returns true on the other horizontal slider with another range it will change colors.

If that does not make sense please let me know and I’ll be happy to clarify!

It does make sense, but… Ok, so this code is attached to a Sprite2D node, right? At least it says extends Sprite2D at the top, so I assume it must.

So, is _on_h_slider_2_value_changed meant to be called when a signal is sent from the slider itself? If so, why are you calling it in the _ready function on the Sprite2D? What is supposed to happen there?

I guess I didn’t really need it to be called, I was just trying to get the boolean value of true or false from _on_h_slider_2_value_changed.

Well, you could call it with a number as input:

_on_h_slider_2_value_changed(7) # or whatever number you want

Or you can just set those boolean values directly.

Also, I just noticed that you said you wanted to check whether the value is within a range - for that, you’ll need to use comparison operators like <, <=, >= or >, rather than ==. Some examples:

if x == 5 || x == 7:
  print("x is exactly 5 or exactly 7")
if x >= 5 && x <= 7:
  print("x is between 5 and 7 (inclusive on both ends)")
if x > 5 && x <= 7:
  print("x is greater than 5 and is smaller than or equal to 7")

I think I am getting a little bit closer to what you are suggesting, is this what you were thinking?

I tried to run this and for some reason the sprite still does not change color after the user places it to the correct color on the slider

You can paste code between three tick marks. Press the </> button on a new like so

```
type or paste code here
```

Your new screenshot doesn’t include some vital lines. The two global variables you want to change were changed by calling this function, nothing special needed to happen.

func _on_h_slider_value_changed(value):
    if value == 4 || value == 5 || value == 6:
        height_global == true #should be one equals sign for assignment

func _on_h_slider_2_value_changed(value):
    if value == 5 || value == 6 || value == 7:
        frequency_global == true #also should be one equals sign

After calling these functions, if the passed value parameter was 5 < x < 7 your globals would be set to true. These functions do not return anything so they cannot be used as conditions in an if statement.

func _ready() -> void:
    _on_h_slider_value_changed(7)
    _on_h_slider_2_value_changed(8)
    if frequency_global and height_global:
        texture_rect.modulate = Color.YELLOW

The confusing part is where 7 and 8 come from, what values do you really want? where do these values come from?


func _ready() -> void:
	_on_h_slider_value_changed(6)
	_on_h_slider_2_value_changed(6)
	if height_global and frequency_global:
		texture_rect.modulate = Color.YELLOW

func _on_h_slider_value_changed(value):
	if value >= 5 && value <= 7:
		height_global = true
		return height_global

func _on_h_slider_2_value_changed(value):
	if value >= 5 && value <= 7:
		frequency_global = true
		return frequency_global

This is what I have now, I see what you meant by needing that code. My only issue is that it immediately turns yellow when the game begins and I want it to wait for the user’s input of say 6 and 6 on the sliders to turn yellow.

Should I use another function instead of

func _ready() -> void:

Thank you for your help I really appreciate it!

You probably want to change the color as part of the connected signal, not _ready

func _on_h_slider_value_changed(value):
	if value >= 5 && value <= 7:
		height_global = true
		if height_global and frequency_global:
			texture_rect.modulate = Color.YELLOW

func _on_h_slider_2_value_changed(value):
	if value >= 5 && value <= 7:
		frequency_global = true
		if height_global and frequency_global:
			texture_rect.modulate = Color.YELLOW

Thank you so much! This was stumping me for the longest!