Deceiving_Solicitite is technically correct... but I don't feel he explained his code very well and frankly the Hisham's code is just written better even if it's wrong (only by 2 small details). Now that that was said lets get to coding.
The fist issue that pops out to me is your Send command. Lets try changing Send, Array[rand]
to Send % Array[rand]
and now the code works most of the time...
But what does this mean?
Notice the % sign? What this is doing is Forcing an Expression on a command that doesn't normally evaluate expressions. Arrays/Objects are treated as such in AHK and Command's won't recognize them.
This still doesn't explain why the code only "works most of the
time..."
Well as it turns out there's also a slight issue with the Random numbers he's generating. When Inserting Values into an Array they are Indexed incrementally starting at 1 and well his Random number is sometimes producing a zero. So we'll change that line now from to Random, rand, 1, 3
at this point the code is fixed and works %100 of the time.
But what if he wanted to have more insults than 3? Will he have to go through
and count every line and amend his Random command?
That would be silly since we can have the the computer do it for us as it is much faster at counting lines of text than we are. So we'll simply have our Random produce results between 1 and the Max Index of our array like so: Random, rand, 1, % Array.MaxIndex()
. Notice the %? We are again Forcing a Command to evaluate and expression. Neat huh?
Completed code:
Array:= Object()
Loop, Read, C:\Users\dell\Desktop\insults.txt
{
Array.Push(A_LoopReadLine)
}
::diss::
Random, rand, 1, % Array.MaxIndex()
Send % Array[rand]
Return
I hope you learned from this.
Edit: Changed Array.Insert(), deprecated, to Array.Push().