derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
734 stars 35 forks source link

Closure Function for Guard Expression #73

Closed eilx closed 6 months ago

eilx commented 7 months ago

Would there be any issues using a closure for a guard expression? This feels odd to me and likely unnecessary to do, but perhaps I'm overthinking it.

# Set the Property
var value := true
state_chart.set_expression_property('closure_fn', func(): return value)

# Guard Expression
test_fn.call() == true

Apologies if this is the wrong place for this.

derkork commented 7 months ago

Using a closure is totally possible. Just make sure that the property name matches what you call in the guard expression. E.g.

state_chart.set_expression_property('test_fn', func(): return value)

# Guard Expression
test_fn.call()

If you just want to access a property of the current object you can also set the full object as an expression property, e.g.

var value = false

func _ready():
    state_chart.set_expression_property("me", self)

and then use this expression in your guard:

me.value