Code: Select all
function LayerScript(moho)
	if state == nil then
		state = 0
	end
	if not count then
		count = 0
	end
	print (count, "State = ", state,  "  Active tool: ", moho:CurrentTool())
	count = count + 1
	local function doState1 (x)
		print ("state 1 " .. x)
		return true, 2
	end
	local function doState2 (x)
		print ("state 2 " .. x)
		return true, 3
	end
	local function doState3 (x)
		print ("state 3 " .. x)
		local v3 = LM.Vector3:new_local()
		v3 = moho.layer.fTranslation:GetValue(moho.frame)
		v3.x = 0
		moho.layer.fTranslation:SetValue(moho.frame, v3)
		return true, 0
	end
	local disp ={doState1, doState2, doState3}
	local rtx
	local name = moho.layer:Name()
	if state == 0 then
		if moho.layer.fTranslation.value.x ~= 0 then
			state = 1
		end
	else
		rtx, state = disp[state](name)
	end
	moho:UpdateUI()
end
when "whatever it is that it's looking for" happens (here the layer moves off x=0) it moves out of its idle state (state 0) and starts the (here) 3-step process to put things right.
the critical item here is that the state<n> subroutines MUST return the next state to process. This can be determined algorithmically - e.g. state 1 might need to decide if state 2 or state 3 should be next (here I've kept it simple -- it's always the next one)
I've not tried to use external functions or to have "state" set by external means, although there's no reason why not (e.g. rather than this layerscript deciding to move out of "idle", some external force can nudge it)
I **should** have made an "idle" function that returns "idle" or "begin"... IOW the "If state == 0 etc" should have gone into a function that returns 0 if still nothing or 1 if there's stuff to do ... but I didn't! (mea culpa!)
===
Hope that's helpful.