Need help with loop command

Need help with loop command

Postby Leeizazombie » 12 Jul 2013, 00:00

So this is my first time using a loop thing and i'm having problems trying to turn it off, here is my attempt:
Code: Select all
using System;
using System.Threading;

namespace MCDzienny
{
   public class Cmdrtime : Command
   {
      public override string name { get { return "autotime"; } }
      public override string shortcut { get { return "at"; } }
      public override string type { get { return "other"; } }
      public override bool museumUsable { get { return false; } }
      public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
        public override void Use(Player p, string message)
       {
            int number = 0;
            if (message == "")

                while (number < 1)
                {
                    Player.GlobalMessage("$time");
                    Thread.Sleep(120000);

                }

            else if (message == "off")
                number = 1;
       

           
        }
   

      public override void Help(Player p)
      {
         Player.SendMessage(p, "/time - Does stuff.  Example command.");
      }
   }
}


So yeah the time shows as long as nnumber is less than 1, but my problem is changing it, I tried:
Code: Select all
    {
            int number = 0;
            if (message == "")

                while (number < 1)
                {
                    Player.GlobalMessage("$time");
                    Thread.Sleep(120000);

                }

            else if (message == "off")
                int number = 1; 
        }

But it results in an error, any suggestions?
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with loop command

Postby Conor » 12 Jul 2013, 02:38

Your error is probably coming from the if statement - you left a gap so I think it may have messed up. That is why I always use curly brackets {} as I find it clearer to understand.

Also, there is a little problem here. When you use the command for a second time, it calls a new instance of it, this means that the 'number' variable from the previous time you used the command is unchanged. So, effectively, you cannot stop the while loop once it has started.

There is a solution, you can use a static variable to check what state the loop is in. I would personally use the System.Timers.Timer class, but I'll code this your way using the loop to help you understand it :). I am going to use a bool value (true or false) rather than an integer.

Code: Select all
using System;
using System.Threading;

namespace MCDzienny
{
   public class Cmdrtime : Command
   {
       public override string name { get { return "autotime"; } }
       public override string shortcut { get { return "at"; } }
       public override string type { get { return "other"; } }
       public override bool museumUsable { get { return false; } }
       public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }

       public static bool showTime = true;

       public override void Use(Player p, string message)
       {
            if (message == "" && !showTime)
            {
                 showTime = true;
                 while (showTime)
                 {
                     Player.GlobalMessage("$time");
                     Thread.Sleep(120000);
                 }
            }
            else if (message == "off")
            {
                showTime = false;
            }       
            else
            {
                Help(p);
            }
        }
   
       public override void Help(Player p)
       {
           Player.SendMessage(p, "/time - Does stuff.  Example command.");
       }
   }
}


That should work as intended I think :? But it isn't as reliable as using a timer, you should take a look into it <ok>
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with loop command

Postby Leeizazombie » 12 Jul 2013, 03:16

hmm anytime I use /autotime or /at it shows the help for the command, also can you please explain the !showTime part in:
Code: Select all
if (message == "" && !showTime)
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Need help with loop command

Postby Conor » 12 Jul 2013, 03:22

Leeizazombie wrote:hmm anytime I use /autotime or /at it shows the help for the command, also can you please explain the !showTime part in:
Code: Select all
if (message == "" && !showTime)


Try changing the starting value of the showTime variable to false.

So
Code: Select all
public static bool showTime = true;

To
Code: Select all
public static bool showTime = false;


That way you must use the command to start the timer, and it doesn't cause this unwanted outcome you're having now.

As for this:
Code: Select all
if (message == "" && !showTime)


First I'm checking if they have put nothing in the message, if so, we want to enable the time updates.

Secondly, I'm checking to see if the time updates are already active. If they are already active, we don't need to start a new while loop as it already exists in a previous use of the command.

'!showTime' is the same as writing 'showTime == false'.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Need help with loop command

Postby Leeizazombie » 12 Jul 2013, 03:25

Ohh! thats actualy very good cause then it prevents spam :D
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron