XNA Creators Club Online
Page 1 of 1 (19 items)
Sort Posts: Previous Next

C# wait command

Last post 4/20/2007 10:20 AM by Detrumentum. 18 replies.
  • 4/3/2007 11:36 AM

    C# wait command

    Hey again, its been a while =)

     Just a quick question, I've been looking around for a simple wait command (similar to the one in VB) for c#. Google isn't being much help (for once) and those books of mine got caught up in the mail ='(

    Thanks in advance.

  • 4/3/2007 11:52 AM In reply to

    Re: C# wait command

    Answer
    Reply Quote

    What do you want to wait for? There are many different synchronization mechanisms; most of which are suited to a particular purpose.

    To answer your specific question, have a look at the System.Threading.Thread class. The Sleep method probably does what you want (I'm not sure because I don't know what the VB Wait command is).

     

    Stephen Styrchak | XNA Game Studio Developer
  • 4/3/2007 11:53 AM In reply to

    Re: C# wait command

    Namespace: "System.Threading"

     Assembly: "mscorlib"

    Function: public static void Sleep ( int millisecondsTimeout )

    Usage: Thread.Sleep( 1000 ); // Sleep the current thread for one second.

     

  • 4/3/2007 12:02 PM In reply to

    Re: C# wait command

    I tried using Thread.Sleep in my game but it made it seem more like lag than a pause. (I was using it upon restart of the game)  I chucked it because of the lag feel.  Maybe if I had rearranged the code some it might have been better, but I guess I didn't care enough.
    http://www.freewebs.com/campelmxna/ - C# and XNA tutorials
    The only stupid mistake is the one you make twice
  • 4/3/2007 12:07 PM In reply to

    Re: C# wait command

    Oh, I'm just making a kinda half-assed timer consisting of a while loop. It looks something like this

    while ((TImer != 5) | (UserResponse != "Stop"));

    {

     Timer = Timer + 1;

    }

     I just wanted it to count to a certain number (five seconds, probably) or have the user type stop to continue to the next section of the game (if the timer = 5, the game ends in a gore-filled awsome fashion, or if the player types stop he avoids his death).

    This obviously has its flaws, for example the timer finishes in about 0.0000000001 seconds. I was simply wanting to add a wait (or sleep, I guess it is called in C#) so that it wouldn't go by so quick.

    PS: a VB wait command is the Visual Basic equivalent version of just standing around and doing nothing. Shaped like so:

    wait(number of milliseconds you want your program to do nothing); 

    Anyway, thanks for the help guys =).

  • 4/3/2007 12:28 PM In reply to

    Re: C# wait command

    Answer
    Reply Quote

    Thats quite simple, you have a basic tick going on in the update call of the game and every game component so all you need to do for a basic timmer is at the start take the date time add the duration then do a check if its over that time then do the event.

    E.g

    DateTime gameendtime;//the declaration

    gameendtime = DateTime.Now + TimeSpan.FromSeconds(15);// the initialisation

     then the update:

    If (gameendtime < DateTime.Now)

    {

    ... Do stuff

    }

     

    Or you can use GameTime.ElapsedTime  rather than DateTime.Now or ElaspedRealTime

  • 4/4/2007 10:42 AM In reply to

    Re: C# wait command

    Cool! Works great! Thanks again Phantom.

    One more thing I was wondering, (while I have the thread open). If, say, one was making a console application and wished to have that program do a...

    PlayerResponse = Console.ReadLine();

    but without pausing the program to wait for the input (and thus brings us to the timer!)

    Just to clairify, I'm trying to make the program run a timer (which is working wonderfully, I might add) when it gets to a certain point of the game, and it will count to 5 seconds. If the player doesn't type a direction within the 5 seconds, he dies. Separately, it works fine. the timer works and the if statements after it work, but I can't figure out how to get the program to read what the player types without pausing for the Console.ReadLine(). Any assistance is awsome (as usual!)

  • 4/4/2007 10:55 AM In reply to

    Re: C# wait command

    Well theres a couple of ways to do this,

    a) at the 5 seconds you read the line and then do what it says,

    b) an ok/send/submit button or when the player hits enter it reads the console and dose what it says

    c) use an event on keypress of the console check if its a correct input and then fire the event.

    let me know which you want and ill fill in some more details.

     

     

  • 4/4/2007 11:04 AM In reply to

    Re: C# wait command

    Alright, well for the first one, we don't want to wait the five seconds, because then he would be dead. The example of Indiana Jones keeps coming to mind. A giant rock is rolling his way any he needs to know which way to jump. If he waited till the rock hit him before he moved, it would be counter productive.

    An ok/send/submit might work but its more of a pause thing again. The giant rock isn't going to wait for Indiana to press enter.

    The event is probably the best solution. I'm slightly anxious to find out how it would work though. =)

  • 4/4/2007 11:24 AM In reply to

    Re: C# wait command

    Ok well basicly you want to decided when do you want an event to be raised every time a letter is pressed on the console? when a space is pressed etc?

    im about to head home ill post more in a bit but think about exactly how you want it to work ingame

  • 4/5/2007 10:28 AM In reply to

    Re: C# wait command

    Alright, well basically, here is kinda how I was hopeing it would work:

    1. The console spits out some text describing how a creature is about to latch onto the player's head if he doesn't move.

    2. Console waits for user to press the enter key (thus starting the 5 second timer) <=== I guess this is the 'Event'

    3. The console spits out a line of text ("HURRY! DODGE!")

    4. While the timer is counting down, have the console check to see if 'dodge' was typed in (without pausing, like in a           response = Console.ReadLine();) .

    5. If the player types in 'dodge' before the timer gets to 5, he doesn't die. If not, the player does.

    In a nutshell, that is how I envisioned it. It seems to be a lot more complicated than I had originally thought =)

    Hope that helps a little as to what I'm thinking. (I'm sure its getting old, but thanks again =P)

  • 4/5/2007 1:07 PM In reply to

    Re: C# wait command

    Ok, i guess you already have a console done that accepts input. First thing you need to do is tell the console to check if dodge has been typed. Now as you know the word 'dodge' always ends in an 'e' then you could tell the input of the console when e is typed check to see if dodge has been input e.g

     public event EventHandler Dodge;

    public void ConsoleInput(string letter)


    {

    inputstring = inputstring + letter// this is where you normaly add the letter to the input string of the console

    if (letter == "e" && inputstring == "dodge")// you could use just one check but its slightly quicker to only check when the e is typed

    {

    if (Dodge != null)// check that somone this listening for the event

    {

    Dodge(this, Eventargs.null);// call the event

    }

    }
    }

     

    Now in whatever is listening for the event, e.g the player you need to hook up to the event

    Console.Dodge += new EventHandler(DoDodge);// console being the console that is calling the event, DoDoge is the method to doge, this gose into the constructor or initialization

    private void DoDodge(object sender, e EventArgs)//you need these paramiters always for event methods

    {

    ...// Do the doge logic here

    }

     

    hope this helps

  • 4/16/2007 10:18 AM In reply to

    Re: C# wait command

    Sorry it has taken me so long to reply! I had to go visit my grandparents for Easter.

    This looks very complex. Your explanation is exacly what I'm looking for, but I'm still not certain on how to implement the code you have given above (I get a tonne of errors if I even attempt to integrate it into my code). Any chance of rephrasing a little bit? (Pretend you are talking to a child =P works for me sometimes).

  • 4/17/2007 7:16 AM In reply to

    Re: C# wait command

    What sort of errors are you getting?

    if i could see some of the code it would help

  • 4/17/2007 11:39 AM In reply to

    Re: C# wait command

    The errors I'm getting are both:

    Expected Class, delegate, enum, interface or struct.

    Under the public event and the public void you gave me.

    As for a small bit of code, do you simply want to see the entire project? It might make more sense (if you don't mind reading 1500 lines of psuedo code =P)

  • 4/18/2007 2:27 AM In reply to

    Re: C# wait command

    That error looks like a names been used that dosnt exist or is the wrong type. i dont mind looking at 1500 lines will help me get a handle on exactly what the prob is :)
  • 4/19/2007 10:54 AM In reply to

    Re: C# wait command

    Alright =). I guess I'll post the .cs file (yes, thats right, everything is in ONE .cs file)

    Also, I have tried another method (did you know stopwatch existed??!?) and it works alright (but just alright, I'd like it to be a bit smoother in terms of after five seconds it has a message pop up as opposed to waiting for the user to type something, but that isn't paramount).

    Where do I go about doing this? (refering to the postage of the code)

  • 4/19/2007 11:15 AM In reply to

    Re: C# wait command

    Use pastebin:

    http://pastebin.com/

    just set the syntax highlighting type to C#, copy and paste your code, enter name, then hit send
  • 4/20/2007 10:20 AM In reply to

    Re: C# wait command

    Alright, I tried the pastebin, and got this error:

    Query failure: Can't open file: 'pastebin.MYI'. (errno: 145)

    and then a list of my code with slashes and brackets in wierd places. Is there anywhere I can just post a ZIP or something?

Page 1 of 1 (19 items) Previous Next