Hello,
I’m tryna make a fullscreen toggle by a button (but also by just changing the value from anywhere if needed) and I landed on using a setter. Problem is is that the setter prevents my bool from changing, and it works when its not a setter function.
extends Node
var forced := false
var fullscreen: bool = false:
set(vari):
if vari:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
elif !vari:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("fullscreen") and !forced:
if fullscreen:
fullscreen = false
elif not fullscreen:
fullscreen = true
print(fullscreen)
I tried that but didn’t work. I also just tried using the fullscreen variable alone and still doesnt work. Idk why but the fullscreen variable just never changes.
var forced := false
var fullscreen: bool = false:
set(vari):
if vari:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
else
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
fullscreen = vari
Then all will work.
However I’d also like to add that this is the wrong tool for the job. Not because it doesn’t work, but because it breaks some assumptions that people usually make about properties. (And by “people” I also mean “you yourself 3 years later after you’ve forgotten all about this piece of code”)
Rember - properties should work as closely as possible like variables. In particular:
Reading and writing a property should be fast. You don’t expect that reading or writing a variable would be slow. Properties shouldn’t be either.
Reading and writing properties should be without side effects. Just because you set a variable, nothing should happen instantly. It might affect some future operation (I mean… that’s kind of the point of variables), but the mere act of setting a variable shouldn’t trigger anything. The worst case is if setting the property to the value it already has triggers something. That WILL confuse and trip people up.
If you find that you need to do slow operations or have side effects - use functions (methods) instead. Property is not the right tool.