#changing values in autoload script crashes the game

1 messages · Page 1 of 1 (latest)

twilit tiger
#

also, just realized i forgot an uppercase when trying to change the Money value, but it doesn't change anything either

sage oar
#

Autoloaded scripts are loaded into the scene tree next to root whether they are attached to a Node or not

twilit tiger
#

well yeah at first it was just a script but since it kept crashing i tought attaching it to a node could change something, it did not

#

and just tried changing from a Dictionnary from simple vars but it also crashes the game when trying to change the values

sage oar
#

As for the crash, I don't know what causes it gdthinking

#

where are you modifying the value ?

twilit tiger
#

i have another scene with a script attached to a 3dnode

#

it's supposed to change the value when i click on a cube

#
    mouse_in = true
    print("quoi")
    body.set_material_override(ShaderOn)
    while mouse_in == true:
        if Input.is_action_just_pressed("Left_click"):
            Stats.money +=1 ```
sage oar
#

Ah, a wild while loop appears

#

can you replace Stats.money += 1 by a simple print statement to make sure it's not the while loop that is crashing the game ?

#

I think that you can try and use an if loop instead if it's the culprit

twilit tiger
#

actually, seems like it crashes when my mouse enters one of the cube, so it's not even the fact of clicking on it i think

sage oar
#

I think that it makes sense because whenever your mouse enters the Area and the signal is triggered the while loop is set off gdthinking

twilit tiger
#

onready var body = $Area/Mesh/Shader
onready var ShaderOn = preload("res://Terrain/Materials/CubeHover.tres")
onready var ShaderOff = preload("res://Terrain/Materials/CubeDefault.tres")
var mouse_in = false

func _ready():
    body.set_material_override(ShaderOff)
    pass # Replace with function body.

func _on_Area_mouse_entered():
    mouse_in = true
    print("quoi")
    body.set_material_override(ShaderOn)
    while mouse_in == true:
        if Input.is_action_just_pressed("Left_click"):
#            Stats.money +=1
            print("abc")

func _on_Area_mouse_exited():
    mouse_in = false
    print("feur")
    body.set_material_override(ShaderOff)

, here is the full script

twilit tiger
#

omg you were right

#

seems like replacing the while with an if solved the crash

sage oar
#

it loops as long as the mouse_in statement is true, and it is always true when the mouse enters the area

#

Nice !

twilit tiger
#

tough it's doesn't print when i click on it💀

sage oar
#

is Left_click properly defined as an action in the project settings ?

twilit tiger
#

seems like it

sage oar
#

I know what the problem is, this signal is only triggered once, the moment the mouse enters the Area, so it only checks for the action once in that instant

twilit tiger
#

oh yeah that makes sense

#

then how could i detect an input while the mouse is in the cube without creating an infinite loop again?

sage oar
#

you can move that block of code in a function that loops continously like _process

#

this block : ```
if mouse_in == true:
if Input.is_action_just_pressed("Left_click"):

Stats.money +=1

        print("abc")```
twilit tiger
#

omg it works

#

thank you ferris

sage oar
#

yay !