Pausing before command
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Pausing before command

Started by chaggydawg, 30 April, 2004, 14:14:23

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

chaggydawg

Is there anyway without using a OnTimer()   to pause for a certain amount of time before executing a command. I'm sure many people have had this problem:

curUser:SendData(Botname.." You are being disconnected")
curUser:Disconnect()


the user never recieves the message before disconnection....

I need some sort of

curUser:SendData(Botname.." You are being disconnected")
(PAUSE HERE FOR A SECOND OR TWO)
curUser:Disconnect()

kind of deal, but without using a timer...

Any ideas guys?

nErBoS

Hi,

Well the user is not reciving the message because this is wrong..

curUser:SendData(Botname.." You are being disconnected")

should be...

curUser:SendData(Botname, " You are being disconnected")

Best regards, nErBoS
--## nErBoS Spot ##--

chill

#2
uses the cpu i guess.

function f()
	
	curUser:SendData(Botname.." You are being disconnected") 

	--(PAUSE HERE FOR A SECOND OR TWO) 
	Sleep(1)
	
	curUser:Disconnect()
end



function Sleep(sec)
	sec = sec*1000
	local t1 = clock()
	while clock() < t1+sec do
		--sleep
	end
end

but else I don't have that sort of problem with ptokax

chaggydawg

wouldn't that be for curUser:SendPM      ??

curUser:SendPM(BotName,"message")
curUser:SendData(data)



but anyways that was just an example, I've tried all sort, including SendToAll, and still the person is disconnected way to fast to recieve the data...


chill

no prob, and tell me about the cpu usage if you have time :)


chaggydawg

Perhaps because I am using it during userlogin process, and the hub is waiting for further data or something, I waited about 2 minutes before I closed to see if maybe it just needed to timeout... but no luck there

chaggydawg

#8
ok I did:

t1 = clock()
SendToAll(botname,t1)



in the dataarrival, and I think maybe 1000 was just to high of a number to use hehe, doesn't seem to be milliseconds but seconds

crazybot> 393.366
[09:12] 413.615
[09:13] 419.574
[09:13] 424.481
[09:13] 428.697
[09:13] 436.748
[09:13] 444.449
[09:13] 447.844
[09:13] 448.495
[09:13] 448.495
[09:13] 451.179
[09:13] 465.069


So I'm gonna try again with a much lower number

chaggydawg

yah using a number like 3 is better, it pauses for 3 seconds than continues, CPU usages shoots up to about 60% (atleast on my PC)  while its waiting however, but drops right back down afterward.

chaggydawg

#10
ok this worked perfectly thank you very much chill   :)


Here the chunk I used it in, I did .2 seconds, and it worked fine
elseif ( tonumber(ccTag["V"]) < tonumber(AllowedClientVersions[ccClient]) ) then
				curUser:SendData("<"..secbot..">    You Must Upgrade Your Client To Atleast "..ccClient.." v"..AllowedClientVersions[ccClient])
				local t1 = clock()
								while clock() < t1+.2 do
				--sleep
				end
				curUser:Disconnect()
			end

chill

great but it uses a lot of cpu :(.
and yepp if thaught cpu time was in milliseconds. maybe use a timer,

function Main()

	doLater(2,function() SendToAll("test") end)

end

todoLater = {}

function doLater(sleep,func)
	todoLater = func
	SetTimer(sleep*1000)
	StartTimer()
end

function OnTimer()
	todoLater()
	StopTimer()
end

but, when you want to pass on something like curUser:f(), I guess you would need to access curUser, afterwards, over a table.

chill

bit more advanced :)

function Main()

	SetTimer(1000)
	StartTimer()

	doLater(2,function() SendToAll("test 2") end)
	doLater(5,function() SendToAll("test 5") end)
end

todoLater = {}

function doLater(sleep,func)
	tinsert(todoLater,{ sleep,0,func })
end

function OnTimer()
	foreach(todoLater, function(i,v)
		if (i ~= "n") then
			v[2] = v[2] + 1
			if v[2] == v[1] then
				v[3]()
				tremove(todoLater,i)
			end
		end
	end)
end

Corayzon

yea...thats what i was thinking...in a way...but with some asserting n shit

more like...adding the command to a table with the fields...toWait, Commands, ArgList...

and on the timer...count down the toWait feild...and when lower then 0 dostring will do the trick ;)

chaggydawg

Just when i think i'm starting to know things... I come here and feel stupid all over again   lol

chill

lol chaggydawg, same here :)

Corayzon, yepp optimised it a bit

function Main()

	curUser = GetItemByName("dumbo")
	doLater(7,{curUser},
		function(table)
			table[1]:Disconnect()
		end)
	doLater(2,{},function() SendToAll("test 2") end)
	doLater(5,{},function() SendToAll("test 5") end)
	doLater(6,{},function() SendToAll("1 sec till disconnect") end)
end

todoLater = {}

function doLater(sleep,tvar,func)
	tinsert(todoLater,{ sleep,tvar,func })
end

function OnTimer()
	foreach(todoLater, function(i,v)
		if (i ~= "n") then
			v[1] = v[1] - 1
			if v[1] == 0 then
				v[3](v[2])
				tremove(todoLater,i)
			end
		end
	end)
end

chill

just another exsample, with more variables
function Main()


	local string = "Dunno"

	curUser = GetItemByName("dumbo")

	-- curUser will be table[1] in func and string table[2]
	doLater(2,{curUser,string},
		function(table)
			table[1]:SendData(table[2])
		end)

	doLater(7,{curUser},
		function(table)
			table[1]:Disconnect()
		end)
	doLater(2,{},function() SendToAll("test 2") end)
	doLater(5,{},function() SendToAll("test 5") end)
	doLater(6,{},function() SendToAll("1 sec till disconnect") end)
end

todoLater = {}

function doLater(sleep,tvar,func)
	tinsert(todoLater,{ sleep,tvar,func })
end

function OnTimer()
	foreach(todoLater, function(i,v)
		if (i ~= "n") then
			v[1] = v[1] - 1
			if v[1] == 0 then
				v[3](v[2])
				tremove(todoLater,i)
			end
		end
	end)
end

Corayzon

yea...thats kinda what im talkin bout...

but dont u find it a little hard to read?

chill

hmm.. nope think its as simple as can be :), but show me what you had in mind :).

Corayzon

#19
just this

function OnTimer()
	foreach(todoLater, function(i,v)
		if (i ~= "n") then
			v[1] = v[1] - 1
			if v[1] == 0 then
				v[3](v[2])
				tremove(todoLater,i)
			end
		end
	end)
end

i guess its not that bad...its just i dont understand exactly whats kicks ere...and i say that cause
its writin bad...u should rename i,v into names that show u what they mean

and i work with tables and arrays alot dif...as u prob can tell...ill write up my version sometime soon and then post it ere ;)

NotRabidWombat

chill,

You can make that script a lot more versatile with the call function:

call (func, arg [, mode [, errhandler]])
Calls function func with the arguments given by the table arg. The call is equivalent to

       func(arg[1], arg[2], ..., arg[n])
where n is the result of getn(arg) (see Section 6.1). All results from func are simply returned by call.

http://www.lua.org/manual/4.0/manual.html

If you do that it would be much more efficient to pass a reference to functions rather than creating a new one for each event.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

chill

#21
dunno if this is how you meant it, but also a way I'd say
gotta check the options, cause can I like also get it to only store the function inclusive arguments?

curUser..
string = "2 sec"

-- exsample code
doLater(2,{curUser,string},
		function(curUser,string)
			curUser:SendData(string)
		end)


todoLater = {}

function doLater(sleep,tvar,func)
	tinsert(todoLater,{ sleep,tvar,func })
end

function OnTimer()
	foreach(todoLater, function(i,v)
		if (i ~= "n") then
			v[1] = v[1] - 1
			if v[1] == 0 then
				call(v[3],v[2])
				tremove(todoLater,i)
			end
		end
	end)
end

??????Hawk??????

#22
hi peeps :-)


any chance of developing something in line with the  origional request  ???


QuoteIs there anyway without using a OnTimer()  to pause for a certain amount of time before executing a command. I'm sure many people have had this problem:

im still reletavely new to  Lua, and still learning.  but could  make use of this function ..

NotRabidWombat

Not without understanding the threading of API calls through PtokaX.

OnTimer is the only real way to delay events. It's not a bad way either. Why not use it?

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Corayzon

i been thinking hard bout this one...

you guys know how in some scripts the connection\disconnection methods can take a long time to process and they hold up the hub until they are...

well im thinking of something as follows...

u guys prob know bout in process components and out of process components...wellz here is the idea...

on the connection methods, you could delay the command with a timer, then when the command is called pass it as a out of process method so then when it executes it doesnt hold up the server process

is this possable???

thankz to the smarty pants that answers this one in advance =]

SMF spam blocked by CleanTalk