Advertisement
1RedOne

Writing out characters from a variable with a small delay

May 7th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Writing out characters from a variable with a small delay
  2.  
  3. $blah  = "From the beginning of time man has sought its own destruction, in this time of despair only one hero may rise amongst the ranks to deter evil from taking its place in the world. Will you be this hero? Can you stand up for what is right? Can you protect the helpless from there own demise? You hero, our are only hope."
  4.  
  5. Function Write-Char{   #this function grabs each character in a variable and writes it out to screen with a small delay, causing a very familiar Final Fantasy or Zelda affect.
  6. Param
  7.     (
  8.         #Specify the characters to write to screen
  9.         [Parameter(Mandatory=$true)]
  10.         [Alias("characters")]
  11.         [string]$text,
  12.  
  13.         #Specify the delay between characters
  14.         [Parameter(Mandatory=$false)]
  15.         [Alias("pause","wait")]
  16.         [string]$delay='15'
  17.     )
  18.         $i=0
  19.     Do {
  20.             If ($i -eq $host.ui.RawUI.BufferSize.Width){  #this logic detects if the current character would force the window to scroll, and if so will write to a new line
  21.                 Write-Host ($text.Substring($i,1))
  22.                 }
  23.             else{                                         #if the current character won't cause the window to scroll horizontally, then add the current character to the line
  24.                 Write-Host ($text.Substring($i,1)) -NoNewline
  25.                 }
  26.  
  27.            
  28.             Start-Sleep -Milliseconds $delay #amount of time to wait between each character
  29.             $i++                             #increment $i to move to the next character
  30.         }
  31.         Until ($i -eq ($text.Length - 1))
  32.        
  33. }
  34.  
  35. Write-Char -characters $blah
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement