Need help with (simple) click to move/ RTS style movement

recently started to make a (very small scale) RTS but I’ve bumped up against something rather fast: there’s very little out there tutorial-wise for click-to-move in defold.

all I’ve managed to find myself is a tutorial that uses animations for it (which isn’t fit for purpose since I can’t cancel the animation halfway through and have the character move elsewhere) and one using Astar pathfinding that’s overkill for what I need (not many obstacles in my game) and doesn’t have the most clear presentation anyway.

All I have managed to do on my own so far is get the mouse coordinates and print them when I click. any help would be extremely appreciated.

1 Like

If you’re not using this asset as your starting point for creating an RTS game in defold, you should..

thanks for the reply, but I’m the type of insane individual who likes to make my own assets. the games I make feel more personal that way.

dont use those assets but see how they implemented the movement, learn from the example scripts

I create a sample “game” based on that asset pack a few years ago:

HTML5: Warbattles-RTS 0.0
Code: GitHub - britzl/warbattles-rts: RTS sample project for the Defold game engine

Ah, yes, the example project included with the assets will also be helpful!

oh, I thought it was just sprites. sorry about that. I’m looking through it now
this looks like what I want:

local function walk_path(self, unit)
	local pos = go.get_position(unit.id)
	local step = table.remove(unit.path, 1)
	if step then
		local to = pos + step
		msg.post(unit.id, "moving", { to = to })
		go.animate(unit.id, "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, 1 / unit.data.speed, 0, function()
			walk_path(self, unit)
		end)
	end
end

is removing stuff from the table how you interrupt the ongoing animation? I can’t figure out why else that table.remove(unit.path) is there.

edit: this isn’t actually that different from the animation based one I found before, now that I’m figuring it out. two things: my sprite disappears when the animation is over, and as mentioned before idk how to interrupt it so I can click to change the direction while it’s moving

function init(self)
	msg.post(".", "acquire_input_focus")
end



function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		local mouse_pos = vmath.vector3(action.x,action.y,0)
		local obj_pos = vmath.vector3(go.get_position().x,go.get_position().y,0)
		go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, mouse_pos, go.EASING_LINEAR, 0.5, 0)
		print(action.x,action.y)
		print(obj_pos.x,obj_pos.y)
	end
end

this is what I’ve got now, after pruning out the parts I couldn’t figure out or don’t need to use. can’t figure out why the sprite would go invisible at the end.

You can interrupt go.animate using go.cancel_animations.

Alternatively, you can move your game object in the update function using speed , direction, velocity, dt etc

Probably because of z-fighting. If your background’s Z is 0, your sprite (actually your game object) might end up behind it.
Try using 0.1 for the Z value to your mouse_pos to test it.

you were right about the Z fighting, that was exactly it. and as it turns out, for some reason, the animation cancel isn’t a problem anymore. something I stole from that asset pack example you guys provided me with must work differently from the other one I found. and thanks for the video, I’ll look at that asap.

thank you everyone! :+1:

1 Like