derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
761 stars 39 forks source link

Expression guard: "mood == hunger" throws "Invalid operands to operator ==, String and float." I'm not using a float... #140

Closed MoogieOuttaMyDepth closed 3 weeks ago

MoogieOuttaMyDepth commented 3 weeks ago

Don't know why this is happening all of a sudden, I did have things working a while ago, but then I realised I was still handling too much of my transitioning logic in code and remembered I could just use expression guards instead.

But then when I set the expression property like this: statechart.set_expression_property("mood", urgent_mood)

And check it with: "mood == hunger"

It throws an error like I'm trying to use a float. But "mood" is a String and "urgent_mood" is statically typed as a String, so I don't understand where this issue could be coming from.

I've followed the stack trace to my "decide_mood()" function and it only ever uses the "urgent_mood" variable, or the placeholder string "Fine". It never uses a float.

Feels buggy, don't know what else to check or try.

Full error:


E 0:00:01:0150   expression_util.gd:19 @ evaluate_expression(): (guard in /root/Main/Dino_Lowpoly/StateChart/Root/Logic/Passing Time/Idle/To Seeking Food) Expression execute error: Invalid operands to operator ==, String and float. for expression: mood == hunger
  <C++ Source>   core/variant/variant_utility.cpp:1091 @ push_error()
  <Stack Trace>  expression_util.gd:19 @ evaluate_expression()
                 expression_guard.gd:18 @ is_satisfied()
                 transition.gd:74 @ evaluate_guard()
                 state_chart_state.gd:271 @ _process_transitions()
                 compound_state.gd:126 @ _process_transitions()
                 compound_state.gd:126 @ _process_transitions()
                 parallel_state.gd:89 @ _process_transitions()
                 state_chart.gd:148 @ _run_changes()
                 state_chart.gd:127 @ set_expression_property()
                 protobird.gd:145 @ decide_mood()
                 protobird.gd:96 @ _on_idle_passing_time()
                 state_chart_state.gd:112 @ _state_enter()
                 compound_state.gd:65 @ _state_enter()
                 compound_state.gd:175 @ _handle_transition()
                 atomic_state.gd:15 @ _handle_transition()
                 state_chart.gd:197 @ _do_run_transition()
                 state_chart.gd:174 @ _run_transition()
                 state_chart_state.gd:234 @ _process()
MoogieOuttaMyDepth commented 3 weeks ago

I found the problem. I didn't really know how to go about the syntax of the expression, so I neglected to surround the property and value with "" string quotations. Properly formatting the expression as

"mood" == "hunger"

Fixes the errors. Although, it should probably not complain about them being floats.

derkork commented 3 weeks ago

Well if you have an expression property named hunger which is a float then I think it's the right thing for the evaluation to say "errm you are comparing strings with floats here, this will never work". Otherwise your expression would be silently returning false every time and you would have no clue what the problem is.