Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-11-03 17:12:07

Kikikan
Member
From: Hungary
Joined: 2015-08-10
Posts: 204

[Question] How can I include a timer?

So I would like to do a countdown, when someone hit the coin. I made the timer (with interval of 1000) and I made the coin script. When someone hits the coin, he will get the crown, get +1 win, but then nothing happens. What did I do wrong?
Coin script:

            if (m.Type == "c")
            {
                string username = users[m.GetInt(0)];
                if (coinpickedup == 0)
                {
                        addWins(users[m.GetInt(0)], +1);
                        con.Send("say", "/pm " + username + " Nice job! You won!");
                        say("The coin has been hit by: " + username + " You have 10 seconds to pick up the coin! But that is not counted as a win.");
                        con.Send("say", "/givecrown " + username);
                        coinpickedup = 1;
                        timeLeft = 10;
                        timer1.Enabled = true;
                }
            }

Timer script:

public void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft = timeLeft - 1;
                say("You have got " + timeLeft + " seconds left");
            }
            else
            {
                con.Send("say", "/reset");
                coinpickedup = 0;
                timer1.Enabled = false;
            }

        }

Offline

#2 2015-11-03 18:07:26

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Question] How can I include a timer?

Use timer.Elapsed, don't really know what tick does (too lazy to google right now) Just know that you need to you elapsed.

I assume you know how to assign the timer's .elapsed function to one that you've made?

Offline

#3 2015-11-03 18:25:37

Kikikan
Member
From: Hungary
Joined: 2015-08-10
Posts: 204

Re: [Question] How can I include a timer?

den3107 wrote:

Use timer.Elapsed, don't really know what tick does (too lazy to google right now) Just know that you need to you elapsed.

I assume you know how to assign the timer's .elapsed function to one that you've made?

Yes, thanks //forums.everybodyedits.com/img/smilies/smile

Offline

#4 2015-11-03 18:46:18, last edited by Zumza (2015-11-03 18:50:16)

Zumza
Member
From: root
Joined: 2015-02-17
Posts: 4,645

Re: [Question] How can I include a timer?

You could create another thread that sleeps for that x minutes.


Edit:

using System.Threading;

Thread thread = new Thread(delegate() {
       Thread.Sleep(x * 60000); // 1 minute is 6 * 10 ^ 4 milliseconds
       connection.Send("say", "/reset");
}));
thread.Start();

Everybody edits, but some edit more than others

Offline

#5 2015-11-03 19:04:46

Processor
Member
Joined: 2015-02-15
Posts: 2,246

Re: [Question] How can I include a timer?

Zumza wrote:

You could create another thread that sleeps for that x minutes.


Edit:

using System.Threading;

Thread thread = new Thread(delegate() {
       Thread.Sleep(x * 60000); // 1 minute is 6 * 10 ^ 4 milliseconds
       connection.Send("say", "/reset");
}));
thread.Start();

-1 Using Thread.Sleep is much more unreliable compared to timers, and: why reinvent the wheel?


I have never thought of programming for reputation and honor. What I have in my heart must come out. That is the reason why I code.

Offline

Wooted by:

#6 2015-11-03 23:15:32

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,852

Re: [Question] How can I include a timer?

den3107 wrote:

Use timer.Elapsed, don't really know what tick does (too lazy to google right now) Just know that you need to you elapsed.

I assume you know how to assign the timer's .elapsed function to one that you've made?

I ask you to cite or something. I've always seen Tick used, it makes sense, and it works.
http://stackoverflow.com/questions/1013 … tick-event talks about different timers... perhaps you were referring to something else? This code looks like winform auto generate!

Zumza wrote:

You could create another thread that sleeps for that x minutes.

interesting approach. Probably should interject a suggestion per keeping track of the thread... in case it needs to be dropped or something.... not sure what to tell you I guess. I'd probably do this... because I'm too lazy to make timers in the editor.

Processor wrote:

-1 Using Thread.Sleep is much more unreliable compared to timers, and: why reinvent the wheel?

and now the politically correct people make the entrance
http://stackoverflow.com/questions/8815 … so-harmful
we really don't need the precision, but quiescent threads are apparently a problem. I guess waithandles look nice

Offline

#7 2015-11-03 23:49:22

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Question] How can I include a timer?

hummerz5 wrote:
den3107 wrote:

Use timer.Elapsed, don't really know what tick does (too lazy to google right now) Just know that you need to you elapsed.

I assume you know how to assign the timer's .elapsed function to one that you've made?

I ask you to cite or something. I've always seen Tick used, it makes sense, and it works.
http://stackoverflow.com/questions/1013 … tick-event talks about different timers... perhaps you were referring to something else? This code looks like winform auto generate!

He used the winforms timer, I don't remember exactly where I read it, but the System.Timers.Timer object is preferred over the System.Windows.Forms.Timer object.

hummerz5 wrote:
Processor wrote:

-1 Using Thread.Sleep is much more unreliable compared to timers, and: why reinvent the wheel?

and now the politically correct people make the entrance
http://stackoverflow.com/questions/8815 … so-harmful
we really don't need the precision, but quiescent threads are apparently a problem. I guess waithandles look nice

As the url stated (kudos to the research), it's simply a waste of your memory and takes a terrible amount of time (for a computer) to create and destroy, as for that the sleep function simply isn't accurate, but indeed, that accuracy isn't very noticeable in this context.
Now the waste of memory might also not be a big problem in the current context, but imagine Zumza were to want to do programming for a living later on, it'd be best to learn the correct way as soon as possible, since it's harder and harder to do stuff different the older you become.

Offline

Wooted by:

#8 2015-11-04 04:24:09

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,852

Re: [Question] How can I include a timer?

^Right, the threads could be a bit taxing

just that processor didn't really come out and say that
just kinda a halfway "abandon all hope" thing
just saying
just

Offline

#9 2015-11-04 23:43:27, last edited by Tomahawk (2015-11-04 23:45:13)

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,836

Re: [Question] How can I include a timer?

den3107 wrote:

takes a terrible amount of time (for a computer)

200,000 cycles = 0.083ms on a 2.4GHz CPU. Oh the horror.

People should lower their standards. While it's fun to imagine we're all professional programmers, it's not stack overflow and it's not commercial software.
Suggest [with code] a few simple alternate solutions and let OP decide what method to use, instead of raging at unnoticeable time intervals, tiny memory usages and each others' suggestions.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

Wooted by:

#10 2015-11-05 01:06:10

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Question] How can I include a timer?

Tomahawk wrote:
den3107 wrote:

takes a terrible amount of time (for a computer)

200,000 cycles = 0.083ms on a 2.4GHz CPU. Oh the horror.

People should lower their standards. While it's fun to imagine we're all professional programmers, it's not stack overflow and it's not commercial software.
Suggest [with code] a few simple alternate solutions and let OP decide what method to use, instead of raging at unnoticeable time intervals, tiny memory usages and each others' suggestions.

As I stated earlier, it's best to learn the correct way sooner or later (since myself I obviously too am far from any grade of programmer).
the thing is, when it's easy to do it in another more efficient way with a little research (where sometimes that method might even be easier!), it's best to just do that little research.

P.S. I did give an answer to the OP which he said worked.

Offline

#11 2015-11-05 01:12:22

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,852

Re: [Question] How can I include a timer?

"yes thanks"
Either the person is staunchly incapable of writing messages of appreciation (or messages at all), or we haven't implemented a fixed solution.

Even if we have solved it, the implementation is still exogenous.

Offline

#12 2015-11-05 12:50:55, last edited by Zumza (2015-11-05 12:53:37)

Zumza
Member
From: root
Joined: 2015-02-17
Posts: 4,645

Re: [Question] How can I include a timer?

This topic became more an "Fast write and easy to maintain code VS Highly optimised and hard to interpret code" topic.
It is a great pleasure to know that your code is an art piece. This is one of the satisfaction for being a programmer.

I usually prefer writing something in between this two parts when I share the code with someone else.
Highly optimised code depends very much on what the final algorithm must do. By example maybe @Kikikan doesn't need a timer.


Everybody edits, but some edit more than others

Offline

Zumza1446724255555546

Board footer

Powered by FluxBB

[ Started around 1714651716.8414 - Generated in 0.105 seconds, 12 queries executed - Memory usage: 1.55 MiB (Peak: 1.73 MiB) ]