Scripting Help - Simple but not?

Post Reply
Demlar
Posts: 4
Joined: Wed Nov 16, 2016 6:19 am

Scripting Help - Simple but not?

Post by Demlar »

I am finding that that if i put a for loop with tempTimers inside, it all just executes the commands at the same time..

here is what i got something like this
In my script
Code: [show] | [select all] lua
function doCommand(x,s,cmd)
    for i=1, x do
        tempTimer(s,function()
            send(cmd)
        end)
    end
end
In my Keys bound to F1
19 = amount of times to send the command, 5 is the delay, and last is the actually string to be sent.
Code: [show] | [select all] lua
doCommand(19,5,"k d")

I simple want to execute a certain number of commands lets say 10 times, with a delay between the command. I'm sure i'm doing this incorrectly, as I'm new to mudlet. Please show or link me information, don't merely say something like an Iteration of a trigger, as I said I'm new gotta explain a bit more.
Thank you for your time.
-Demlar

Saros
Posts: 12
Joined: Sat Oct 15, 2016 10:10 pm

Re: Scripting Help - Simple but not?

Post by Saros »

I am as new to mudlet as you, but I think they are all executing at the same time because you are making them all at the same time and with the same duration. According to this mudlet wiki link when you make them all at the same time, you need to make the duration of subsequent timers increase by the delay...

The first would be 5 seconds from now.
The second would be 10 seconds from now.
Etc

Perhaps inside your loop.. s = s*i ?

Not tested...
Code: [show] | [select all] lua
function doCommand(x,s,cmd)
    for i=1, x do
        s = s*i
        tempTimer(s,function()
            send(cmd)
        end)
    end
end

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Scripting Help - Simple but not?

Post by Jor'Mox »

I use this function that I made... though the setup is a bit different. It allows for any sequence of commands to be sent, with delays between them as desired. Like this: sendQueue("cmd 1",5,"cmd 2","cmd 3",10,"cmd 4") In that example, it would send cmd 1, wait 5 seconds, then send cmd 2 and cmd 3, then wait 10 seconds, and then send cmd 4.
Code: [show] | [select all] lua
local runQueue
function runQueue(tbl)
    local info = table.remove(tbl,1)
    if info then
        local run = function()
                send(info[2])
                runQueue(tbl)
            end
        if info[1] ~= 0 then
            tempTimer(info[1], run)
        else
            run()
        end
    end
end

function sendQueue(...)
    local tbl = {}
    local args = arg
    for k,v in ipairs(args) do
        if k % 2 == 1 and type(v) ~= "number" then
            table.insert(args,k,0)
        end
    end
    for k = 1,#args,2 do
        tbl[(k + 1) / 2] = {args[k],args[k+1]}
    end
    runQueue(tbl)
end

Demlar
Posts: 4
Joined: Wed Nov 16, 2016 6:19 am

Re: Scripting Help - Simple but not?

Post by Demlar »

the question I am asking is not about how to get a free script, but how to do it with a timer that works every time. The problem is the loop, it finishes then executes everything at once. So timing in the current setup I have doesn't work what so ever.

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Scripting Help - Simple but not?

Post by Jor'Mox »

Yes. Your initial approach simply won't work. Any loop, no matter how constructed, will run essentially all at once (and if it makes a long time will cause Mudlet to freeze until it is finished), and all of the created timers will fire based on how long it has been since they were created. So either you can make a bunch of timers with progressively longer times, as in the first person suggested, or you can use recursion, such that when the first timer goes off, it creates the next one (a variation on what I posted). I didn't feel like writing a script that was unique to your situation, so I copy and pasted something I had on hand that demonstrated the general principal, and could potentially be used to do what you wanted. You are, of course, free to take it or leave it.

Demlar
Posts: 4
Joined: Wed Nov 16, 2016 6:19 am

Re: Scripting Help - Simple but not?

Post by Demlar »

So doing it sequentially like you suggested, works, however I wanted to validate it more, actually having the timer / loop wait and then kill the timer when im done. This is what I am seeing after a few tests.

Timer Report:

timers current total: 28
tempTimers current total: 27
active timers: 27

it feels as though I need to validate the timers are actually disappearing...

Demlar
Posts: 4
Joined: Wed Nov 16, 2016 6:19 am

Re: Scripting Help - Simple but not?

Post by Demlar »

okay so I got it working correctly, instead s = s*i, i did tempTimer(s*i, send...). by doing s = s*i it seemed to be the equivalent of s *=s*i, my timers where incredible large, by doing s*i seemed to work better. this seemed to fix the issue. I can't believe how long it took to start this thread, almost a week n half, not exactly fast haha. Either way, I appreciate the help. Thank you.

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Scripting Help - Simple but not?

Post by SlySven »

Sorry about the delay in getting your (first) post up here but all new users have to have their first post reviewed by an Administrator or Moderator so that Spam Bots :twisted: cannot, after getting past the anti-bot checks (which are not 100% effective :cry:) fill the forums with unrelated junk/illegal/obscene postings. In the last few days I've had to bin over 500 such posting (and banned - I cannot delete the new members that are - the posters) and finding the odd needle that is a real person, genuinely interested in Mudlet in the haystack that is all the c**p that, hopefully our esteemed users don't get to see, does take a little while... :D :geek:

Post Reply