Jump to content

Loop velocity


Overkillz

Recommended Posts

Hello dear community. I have a simple question about loops.

I have a table with several messages(it is an example), might around 1000. Well, here is my question, could I output them in X time. I don't know, something like all messages could be drop in 20 seconds

20000 ms / 1000 messages ?

I know the simple method which is

local myTable = { --[[ HERE ARE MY 1000 messages ]]  }

for i,message in ipairs myTable do
	outputChatBox(message)
end

I hope u can understand me. Thanks for reading.

Regards.

Link to comment
  • Moderators
local operationStartTime = getTickCount()

---------------

-- operation --

---------------

outputChatBox("It took " .. (getTickCount() - operationStartTime) .. " ms to execute this operation.")

 

Edited by IIYAMA
Link to comment

Well, I tried to do it but I didn't get u at all.

This is what I have just tried

local myTableA = {"Message1","Message2","Message3","Message4","Message5","Message6","Message7","Message8"}
local tick = getTickCount()
local duration = 5000

function dropMessages()
	for i,message in ipairs(myTableA) do
		 if getTickCount()-tick>=duration/(i) then
			outputChatBox(message)
		end
	end
end
addEventHandler( "onClientResourceStart", getRootElement(), dropMessages)

 

Link to comment
  • Moderators

I wasn't 100% sure what you were asking for so, I thought I gave it a try. The example does it showing how long lua is busy processing the code in between.

 


 

 


But maybe you want to show all messages within X amount of time.

local myTableA = {"Message1","Message2","Message3","Message4","Message5","Message6","Message7","Message8"}
local messageIndex = 1
local totalDuration = 5000
local durationTimer = math.max(math.ceil(totalDuration / #myTableA), 50) -- can't be lower than 50 ms else the timer doesn't work.



addEventHandler("onClientResourceStart", resourceRoot,
function ()
	setTimer(function ()
		outputChatBox(myTableA[messageIndex])
		messageIndex = messageIndex + 1
	end, durationTimer, #myTableA)
end)

Is this what you are looking for instead? (untested)

 

P.s. loops can be delayed by pause the code, but that is dangerous because you have to manually resume them. (experts only recommended) If you want to know that, I am to happy to send you a link.

Edited by IIYAMA
  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...