coroutine.wait() is yielding

Hello, I’m trying to make a coroutine that runs on an endless cycle waiting for 1 second after each cycle but the coroutine.wait() function appears to be yielding instead of waiting.

Here’s what I’m doing


local TestThread = coroutine.create( function()
	while ( true ) do
		print('test')
		coroutine.wait( 1 )
	end
end )
coroutine.resume(TestThread)

I create a coroutine that is a function with a while true do statement, this should make the coroutine endlessly repeat.
In order to not lock up my game and/or get a stack overflow I am calling coroutine.wait()
“test” prints once into the console and never again, when I call coroutine.resume(TestThread) it will print “test” into the console once more.
If I print the status of the coroutine it is suspended which is supposed to only be the case after a yield is called.

It was to my understanding that this code should endlessly repeat “test” into the console once every 1 second.
What am I doing wrong here?

Because coroutine.wait returns after x seconds.
You want to call yield()

coroutine.wait probably doesn’t do what you think it does because it’s a garryfunction.



-- lua/includes/extensions/coroutine.lua
function coroutine.wait( seconds )

	local endtime = CurTime() + seconds
	while ( true ) do

		if ( endtime < CurTime() ) then return end

		coroutine.yield()

	end
	  
end


It expects coroutine.resume to be called on it at intervals…
I’m guessing it’s only intended to work inside coroutines managed by nextbot.

Okay thank you now that makes sense but that means I must constantly call coroutine.resume() ?

I was looking inside the nextbot files for when it actually called coroutine.resume() but it was nowhere to be found, it must be called somewhere else or internally by the game…

Yep - I’d create a timer to call coroutine.resume at 1 second intervals (or however long you want to pause execution for) and abandon using coroutine.wait altogether.



local TestThread = coroutine.create( function()
	while ( true ) do
		print('test')
		coroutine.yield()
	end
	timer.Destroy ("CalculateMeaningOfLife")
end )

local sleepDuration = 1 -- second
timer.Create( "CalculateMeaningOfLife", sleepDuration, 0, function()
	coroutine.resume( TestThread )
end )


Thank you very much my good sir, the cake is not a lie! haha I tried…

I attempted to make a ‘coroutine.hold()’ function but it just locked up my game


function coroutine.hold( seconds )
	
	local endtime = CurTime() + seconds
	local hold = true
	
	while ( hold ) do
		
		if ( endtime >= CurTime() ) then
			hold = false
		end
		
	end
	
end

It was a valiant effort no doubt.

Thank you all again for your time.

You can use SysTime() for that.
Your game will still lock up, but not forever this time.

That would work if it didn’t lock up at all, I suppose that wouldn’t happen if a coroutine actually created a new thread.

The timer trick is doing the job, and quite well I might ask, but I might also ask myself why use a coroutine at all here and just stick all the code inside the timer