#Declared Variables are re-declared?!

20 messages · Page 1 of 1 (latest)

dry pond
#

In the game I am making I declare a variable called CanHarvest as false, then later I set it to true. But as soon as it is set to true it is instantly set back to false. I do not know why. Please Help

extends PanelContainer

@onready var texture_rect = $TextureRect
@onready var timer = $Timer

var canHarvest = false
var power
var growTime

func _get_drag_data(at_position):
if get_global_mouse_position().y < 110:
set_drag_preview(get_preview())
return texture_rect

func _can_drop_data(_pos, data):
if get_global_mouse_position().y > 110:
return data is TextureRect

func _drop_data(_pos, data):
texture_rect.texture = data.texture

detect_plant_type()

func get_preview():
var preview_texture = TextureRect.new()

preview_texture.texture = texture_rect.texture
preview_texture.expand_mode = 1
preview_texture.size = Vector2(60,60)

var preview = Control.new()
preview.add_child(preview_texture)

return preview

func detect_plant_type():

if texture_rect.texture == preload("res://assets/carrotSprout.png"):
    power = 1.0
    growTime = 3.0
    
timer.start()

func _on_timer_timeout():
canHarvest = true
print ("hi")

func _on_area_2d_input_event(viewport, event, shape_idx):
if Input.is_action_just_pressed("mouse_click") and canHarvest:
GlobalVar.score += power
timer.start(growTime)
#print ("hi")
func _process(delta):
print (canHarvest)

weak wagon
#

I've just been having a look at this and I can't seem to recreate it.

I'm suspicious about declaring the variable as false (line9) and maybe related to an instantiation or something? Not sure.

If you're around now I have a second and can look if you want through a DM 🙂

But hopefully someone wiser than me spots this

dry pond
#

I think it is an issue with signals, but I am not sure why it is happening, or how to fix it. Edit: I rewrote the script to not use signals, but the same issue exists.

for context though this node/script has several instances in the main scene, and from what I can tell the other instances are what is causing CanHarvest to be set to false, but I do not know why.

weak wagon
#

Ah, this makes sense-- try changing the Resource to Local to Scene (Picture attached).

I had a similar issue with a brick breaker game I'm working on currently 🙂 This helped me!

Edit: If that does not work, attempt the following:

    duplicate()``` 
Which I believe yields the same result but code-launched. There may be some differences, however, but I'm not smart enough to understand them yet 😊
dry pond
#

amazingly neither of those did it, I guess it isnt an issue with the instances?

thankyou for your effort nontheless

weak wagon
#

Do you want to try it with a single instance & see if the issue persists? Just to confrim that theory 🤓

dry pond
#

with one instance the issue is the same

#

But it seems that once i set canHarvest to true it is not only rapidly setting it back to false but also occasionaly back to true again

#

WAIT nevermind i tested it again

#

If i remove all instances except 1 the value does not change back to false

#

so it is an instance issue

weak wagon
#

Question - when changing the Local to Scene setting was it from within the original Scene.tscn file you are instancing OR from an instance in another scene i.e. main game scene ?

dry pond
#

from the original

weak wagon
#

How does a player turn canHarvest = false currently? Or has that not been implemented yet?

dry pond
#

it is just declared as false, eventually once you click (and canHarvest = true) it will be set to false, but that cant happen currently since canHarvest is never true when you click

weak wagon
#

Can you send me the function snipped where that happens? 👀

#

I'm also happy to just jump into a call at worst case and look over your screen if you're comfortable with it - no worries if not ofc 🙂

dry pond
#

sure

#

a call would be great