I am trying to set up a top-down adventure game-type thing.
Player clicks a location and the character moves to it using pathfinding. If they tap or click an object or a different character, they move in front of that object/character and trigger an animation, dialogue, etc.
My issue is, I don't know a clean, best practices way to trigger a reaction only if the pathfinding is resolved and the character reaches their destination.
For example, if a player taps a hammer that is across the room. The hammer has a variable that is the X,Y coordinates. The player character uses path finding to move to those coordinates. While the character is moving over, they can tap somewhere else to change directions without consequence. But if they don't and the character reaches the hammer, an animation plays and they pick the hammer up.
What I was trying to do was have a global trigger to distinguish between the character performing an action vs simply moving, and then a variable that is a unique name for each action ('hammer'). So if you tap a blank area they can walk to the variable is set to walk, if they tap an interactive object the variables are set to action and a specific title for that action ('hammer').
So then if they reach the coordinates for that action, while global trigger is set to action and the unique name 'hammer' matches up, it disables player input for a second and triggers the right stuff for hammer.
None of this was particularly working for me, plus it already feels very complicated.
Does anyone have an experience with a project like this? I can try to post a project example later but so far the general path finding is the only thing thats really working.
Develop games in your browser. Powerful, performant & highly capable.
edit: just re-read that actually, seems like you have general pathfinding sorted and that ideas seems fine. Probably just a bug somewhere..
'So then if they reach the coordinates for that action'
Are you comparing positions with 'is equal to' ? That will indeed not work. Watch positions evolve in the debugger.
Notice the numbers after the decimal point. A position is kinda never exact.
Bring all those clickable objects into 1 family.
Bring character and an invisible sprite 'target' into a container. Now each time you pick a character its target is also picked.
Give 'target' an instance boolean 'actionRequired'.
Give 'target' an instance variable 'actionObject'.
Give Character the Lign of sight behavior. Set range to like 50 pixels.
When the player clicks a new target, move 'target' to that position. Find path to 'target'. At the same time.
Set 'actionRequired' to true when clicked on clickable object, else to false.
Set 'actionObject' to the UID of that clickable object.
The action is directly linked to the clickable object, probably in a variable on the family. We pick that object later.
Character has reached endpoint when action object is in range.
Target.'actionRequired' is true ? <-- picks also Character due the container (no performance loss when not true)
Character has LOS on family <--- Character & Target only stay in the picklist if it has LOS
Family.UID =? Target.'actionObject' <----- additional logic
Absolute necessary action: Set Target.'actionRequired' to false, else the whole logic will repeat and repeat until the player chooses a new target.
At this point the logic true/untrue detects if Character arrived at endpoint and clickable object did not run away. It will also trigger if clickable object moved towards the Character, bringing itself into range.
At this point the picklist contains clickable object, so you can use its assigned action to perform the action.
The picklist contains also Character and Target, so you can exclude them from getting clicked with the mouse, else player can send them wandering before the action is done and over with.
Thank you! This works!
Rough demo of this in case anyone else wants to check it out:
https://www.dropbox.com/s/e56t71czd4vx0 ... .capx?dl=0
Useful topic, thanks!