transition.moveTo setting up a queue for multiple moveTo

I am trying to make a control system with the classic 4 direction movement. where you push the up button and the character moves X distance up and stops. If I push the up button and then the right button before the first transition is complete I don’t want the second transition to interrupt the first, but I want it to go into a queue that will trigger the second transition once the first is complete.

I have thought out some code and figured this would work, which it mostly does, but the issue is that I cant seem to prevent new transitions from interrupting the one that is currently running.

If you guys could look at my code and help me out that would be great

ALSO: I think I would like the queue to have a limit of 2 in order to prevent a build up of a ton of moves that the player could input.

if someone could help me figure that out as well I would be very grateful.

moveQueue = {} local function moveHero(event) transition.moveTo( hero, moveQueue[1]) end local function removeFromQueue(event) table.remove( moveQueue, 1 ) if moveQueue ~= nil then moveHero() end end local function touchArrows(event) if event.target == leftArrow then table.insert( moveQueue, {x = -60, time = 1000, onComplete = removeFromQueue()} ) elseif event.target == rightArrow then table.insert( moveQueue, {x = +60, time = 1000, onComplete = removeFromQueue()} ) elseif event.target == upArrow then table.insert( moveQueue, {y = -60, time = 1000, onComplete = removeFromQueue()} ) elseif event.target == downArrow then table.insert( moveQueue, {y = +60, time = 1000, onComplete = removeFromQueue()} ) end end

That looks reasonable but you probably need to change this:

table.insert( moveQueue, {x = -60, time = 1000, onComplete = removeFromQueue()} )

to:

table.insert( moveQueue, {x = -60, time = 1000, onComplete = removeFromQueue } ) --\<----- take off the ()

When you include the () it says run the function and assign the return value of the function to onComplete. When what you want is to set onComplete to the address of the function to run.

Rob

Thanks Rob, I’ll try that out tonight.

I guess for the queue limit I can just put a condition on the table.insert line checking if moveQueue has 2 objects or not.

That looks reasonable but you probably need to change this:

table.insert( moveQueue, {x = -60, time = 1000, onComplete = removeFromQueue()} )

to:

table.insert( moveQueue, {x = -60, time = 1000, onComplete = removeFromQueue } ) --\<----- take off the ()

When you include the () it says run the function and assign the return value of the function to onComplete. When what you want is to set onComplete to the address of the function to run.

Rob

Thanks Rob, I’ll try that out tonight.

I guess for the queue limit I can just put a condition on the table.insert line checking if moveQueue has 2 objects or not.