kubecz3k / FiniteStateMachine

FSM plugin for Godot
MIT License
116 stars 22 forks source link

Updating Physics code in state? #19

Open Zerokami opened 6 years ago

Zerokami commented 6 years ago

Is the update() function called every frame during state?

I want to do _physics_process(delta).

How do I call it in my state?

For example if I want to apply gravity in Falling state and maybe play a falling animation how would I do that?

Is this stuff even supposed to happen in the State Machine? Or is this plugin only supposed to provide methods like transitions or move between states or call function on calling state etc?

kubecz3k commented 6 years ago

well I have some states that even do integrate_forces so it should be ok :) In short inside FSM inspector there is Update mode and you can choose between process, fixed and manual. Manual gives you the most flexibility if you need flexibility, since in this mode you will need to manually call update(deltaTime) function (however you want). And fixed is the same as physics

Zerokami commented 6 years ago

In short inside FSM inspector there is Update mode and you can choose between process, fixed and manual

How do I choose between process, fixed etc.

I am not seeing any options for this in the example?

Edit: I found it in the inspector on the bottom right.

But it seems it does that for all child nodes.

What if I want both _physics_process and update like in a normal node.

Or maybe different one for each state. For example _physics_process for one state and fixed for another state?

kubecz3k commented 6 years ago

https://i.imgur.com/jxZSaBu.png

Zerokami commented 6 years ago

But it seems it sets that for all child nodes.

What if I want both _physics_process and update like in a normal node.

Or maybe different one for each state. For example _physics_process for one state and fixed for another state?

kubecz3k commented 6 years ago

Manual mode is there for rescue. Set processing mode to manual and update the fsm the way you want (in fixed when in some state, in process for others)

Zerokami commented 6 years ago

How do I use manual mode?

Can you give some example code?

Do you mean that I can call _physics_process(delta) in state code if I set to manual?

kubecz3k commented 6 years ago

When you are using manual mode, the FSM will not be updated automatically, you need to do it yourself (so you can do update it in the thread you want). So for example you could have something like this in your code (pseudocode):

func _physics_process(deltaTime):
   if(fsm.getState()==fsm.STATE.WALKING):
      fsm.update(deltaTime);

func _process(deltaTime)
   if(fsm.getState()!=fsm.STATE.WALKING):
      fsm.update(deltaTime);

In that code above, state WALKING will be updated in physics_process and all other states will be updated in process