I am rewriting my game in godot due to recent comments made by the unity ceo. I do not wish to give that man any money. So I am learning gdscript and I am hung on the differences between unity coroutine and gdscripts. Here is my coroutine I had in unity.

`private IEnumerator MovePlayer(Vector3 direction)
    {
        isMoving = true;

        float elapsedTime = 0;

        origPos = transform.position;
        targetPos = origPos + direction;

        while(elapsedTime < timeToMove)
        {
            transform.position = Vector3.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
            elapsedTime += Time.deltaTime;
            yield return null;
        }

        transform.position = targetPos;

        isMoving = false;
    }`

What this code is doing is lerping the players position to the next tile over in the tilemap. I start the coroutine with this code

`void Update()
    {
        if(Input.GetKey(KeyCode.W) && !isMoving)
        {
            StartCoroutine(MovePlayer(Vector3.up));
        }
        
        if(Input.GetKey(KeyCode.A) && !isMoving)
        {
            StartCoroutine(MovePlayer(Vector3.left));
        }

        if(Input.GetKey(KeyCode.S) && !isMoving)
        {
            StartCoroutine(MovePlayer(Vector3.down));
        }

        if(Input.GetKey(KeyCode.D) && !isMoving)
        {
            StartCoroutine(MovePlayer(Vector3.right));
        }
    }`

Now in gdscript this is what I have so far but it doesnt seem to be lerping the positions. This is the same move coroutine as above

`func move(delta):
	ismoving = true
	var elapsedTime = 0
	
	last_position = position
	target_position = last_position + movedirection
	
	if elapsedTime < timetomove:
		position = lerp(last_position, target_position, (elapsedTime / timetomove))
		elapsedTime += delta
		yield() 
		
	position = target_position
	ismoving = false
`

This is the process function I have running for the coroutine

`func _process(delta):	
	if get_node("../Cam") != null:
		tween = get_node("../Cam").tweenGetter()
	
	if !tween:
		if Input.is_action_pressed("ui_left") && !ismoving:
			movedirection = Vector2(-1, 0)
		
		elif Input.is_action_pressed("ui_right") && !ismoving:
			movedirection = Vector2(1, 0)
			
		elif Input.is_action_pressed("ui_up") && !ismoving:
			movedirection = Vector2(0, -1)
			
		elif Input.is_action_pressed("ui_down") && !ismoving:
			movedirection = Vector2(0, 1)
		
		else:
			movedirection = Vector2.ZERO	
		
		
		var co = move(delta);
		while co is GDScriptFunctionState && co.is_valid():
			co = co.resume()
		
		updateSprite()
		
		if movedirection != Vector2.ZERO :
			updateAnimation("Walk")
		else:
			updateAnimation("Idle")`

The player overall does move but does not lerp which is the issue. Maybe im not calling the coroutine correctly or I have a misunderstanding of them in gdscript. In unity I always considered them a async function. If someone could help out and explain to me what Im doing wrong that would be great.

Ok I got it working, the lerp was actually working it was just hard to see since my tiles are 16x16 pixels. As for the coroutine I still am not sure how they work in gdscript.

By the look of it, the issue is this line:

var co = move(delta);

That's making a new coroutine (with 0 elapsed time) every frame.

You could move co out into the class then protect making the new coroutine by doing it in the input checks, like how your c# version did with StartCoroutine.
Like:

if Input.is_action_pressed("ui_left") && !ismoving:
			movedirection = Vector2(-1, 0)
			co = move(delta)

The while loop to call resume also may be an issue. From my understanding, that's going to keep resuming the coroutine until it completes, which is effectively making the coroutine turn into a normal blocking function. Changing the while to an if (so it only resumes once per frame) should work.

(Note: I've only used Godot coroutines once (a few minutes ago), so actual results may vary) 🙂

    Welcome aboard fellow unity refugee, I saw the writing on the wall unfortunately ages ago with the security breach they had earlier and barely addressed so I made the switch and started re-writing all of my projects within GDScript and I'm making solid progress now. I feel like with my C# experience I should probably chime in, been making solid progress with my own project even though it's annoying I had to do it in the first place, I was initially looking at keeping my biggest project in Unity while I kept learning Godot.

    The node system is pretty quirky and different to what you will find compared to Unity and you will probably be better off studying up entirely on GDScript and rewriting your code to fit the engine from scratch rather than necessarily trying to directly convert from C# to GDScript. It would be great if there was some kind of built-in system where we could just dump a unity project in and it does everything for us but that's just wishful thinking and could easily break everything. Some code you can convert from C# pretty straightforward but I have found it much better to research tutorials and code to do with GDScript and restart the learning process.

    Yes, this is a pain, yes this is the Unity CEOs fault, but I'm telling you this so you potentially don't waste time as I have trying to convert things that are a struggle to convert. The good news is though that Godot does have some features in it that has simplified various processes. I like for example the fact that you can create Raycast nodes and debug everything very accurately as well as access them with less code, you also have the center container which works like magic for dealing with UI scaling/positioning and so on.

    If you've got any questions feel free to ask me, again, I highly recommend throwing away the C# brain and coming at GDScript fresh, it does just do things pretty differently in some cases compared to Unity and takes some getting used to.

      Kojack Thanks so much for the explination, I do appreciate it. I will have to try this in code.

      Lethn Thanks for the info, yeah once I saw the unity thing I was like im not giving this person any money lol. One question I do have is during debug is there a callstack I can view? Thats the only thing I have been missing so far. As far as gdscript it seems like a pretty easy language to learn. Im not having any troubles with it yet except obviously the coroutines which @Kojack supplied information on. I am enjoying GODOT so far, I was able to make really good progress in the last few days and am looking forward to working more with it.