FiniteStateMachine icon indicating copy to clipboard operation
FiniteStateMachine copied to clipboard

In transitions nodes, can you access to variables in source and target state?

Open salvob41 opened this issue 6 years ago • 7 comments

If I set some variables in a specific state, I would like that all other states cannot see the above mentioned variables. But the transition need that variable to decide to move to another state or not. Is this possible?

I saw in the code there is "targetStateNode" and a commented "sourceStateNodes", how can I access to those nodes? Is the only way a get_node("-path-to-the-node") ?

P.S.: Sorry for the spam in the issues. I am using this plugin and I think is wonderful, Good work

salvob41 avatar May 17 '18 13:05 salvob41

may I suggest here https://github.com/kubecz3k/FiniteStateMachine/blob/master/addons/net.kivano.fsm/content/FSMTransition.gd#L50-L56 to create a dictionary instead of an array where the key is the StateID and the value is its path. So it can be easily accessed in the prepare function in Transitions nodes

salvob41 avatar May 17 '18 13:05 salvob41

I am doing this in a trasnition script in order to access something defined in the source State:

func prepare(inNewStateID, inArg0 = null, inArg1 = null, inArg2 = null): 
	#you can optionally implement this to reset transition when related state has been activated
	end_turn = false
	source_node = get_node("../../States/"+inNewStateID)
	
	

func transitionCondition(inDeltaTime, inParam0=null, inParam1=null, inParam2=null, inParam3=null, inParam4=null): 
	#YOU MUST IMPLEMENT TRANSITION CONDITION CHECK HERE: Return true/false
	print(source_node.something)
	return end_turn

but maybe, might be cool having a reference in the transition node? maybe?

salvob41 avatar May 17 '18 13:05 salvob41

Quick reply: most of the variables that are not internal for state I usually define inside logicRoot. For the exceptions I know which states are linked with given transition and just do something like this in the preprare: fsm.getStateFromID(fsm.STATE.STATE_ID) (alternativelly fsm.getStateFromID(inNewStateID). Please take a moment to learn all the api that is delivered by FSM node

kubecz3k avatar May 17 '18 14:05 kubecz3k

talking about learn all the api. Is there a wiki or a documentation of the API, and examples?

I am using a lot this plugin, I could find some time to make a demo (even if there is a youtube you made).

I am struggling right now to call a State manually, actually.

salvob41 avatar May 18 '18 09:05 salvob41

I don't want to go OFF-TOPIC here in this issue... but to enter a state manually, I guess you have to do something like:

fsm.getStateFromID(Name_of_the_State).enter() I guess ?

salvob41 avatar May 18 '18 09:05 salvob41

I answered in #24 (fsm.setState(fsm.STATE.STATE_ID)). Basically there is no wiki or any other documentation... there are some notes in FSM.gd at the top (the most ordinary/common usage is described there). But there are also other methods inside fsm that are not internal and can be used outside, I will give you brief description of them below: changeStateTo(stateID) <- changes state to the new one, don't allow to change to the same state setState(inStateID) <- same as above but you can force change to the same state in which you are already (haven't tested this particular use case but from the code it looks like it should work) getStateID <- current state id stateTime() <- how long current state is active in seconds getState() <- return current state (actual object) getStateFromID(inID) <- returns actual state object for given id getTransitionWithID(inID)) <- returns actual transition object for given id getLastlyUsedTransition() <- returns actual transition object which was lastly accomplished lastlyUsedTransitionID() <- returns transition id which was lastly accomplished getActiveTransitions() <- returns array of all transitions that are linked with current state getStates() <- array with all the state objects hasStateWithID <- returns true if there is state with given id getTransitions <- array with all the transitions objects hasTransition(inID) <- getPrevStateFromHistory(inHowFar) <- can be used if you want to check the history of active states (0 means last state, 1 means penultimate one etc) stateCall(inMethodName, inArg0=null, inArg1=null, inArg2=null) <- call a function in current state update(inDeltaTime, param0=null, param1=null, param2=null, param3=null, param4=null) <- you need to use this one if your fsm is not updating automatically (you decide when to update fsm (normally it happens every frame)) init(arg0, arg1, arg2,arg3, arg4) <- use it only if you choosen 'init manually' in inspector. Sometimes it's good to delay initialization a little for example because you need some variables that are setup in ready() of logicRoot (automatic initialization is done before logicRoot ready). Or because you want to pass some variables into stateInit or transitionInit

kubecz3k avatar May 18 '18 11:05 kubecz3k

feel free to ask further questions :)

kubecz3k avatar May 18 '18 11:05 kubecz3k