StrokesPlus.net Forum
»
General Discussion
»
Scripts
»
Why can Timers not get variables in the for loop?
Rank: Advanced Member
Groups: Approved
Joined: 9/26/2018(UTC) Posts: 69  Location: 北京 Thanks: 18 times Was thanked: 1 time(s) in 1 post(s)
|
Hi Rob: I want to use DisplayText to display the count for the for loop. However, DisplayText can only display text in the for loop and cannot obtain variables. Here is my simple code. I hope I can get your help. Thank you. Quote:sp.Sleep(600); var ress = Number(inputbox(" ")); for (ddi=1; ddi<=ress;ddi++){ sp.CreateTimer("test", 5, -1, var info = new DisplayTextInfo(); info.Title = 'Test'; info.TitleAlignment = 'Center'; info.Message = ddi;//Only text can be displayed here, and variables cannot be obtained. info.MessageAlignment = 'Left'; info.Duration = 3500; info.Opacity = 0.7; info.Location = 'top'; info.TitleFont = new Font('Segoe UI', 12, host.flags(FontStyle.Bold)); info.MessageFont = new Font('Segoe UI Semibold', 12); info.BackColor = 'black'; info.Padding = 15; info.UsePrimaryScreen = true; sp.DisplayText(info); sp.DeleteTimer("test");`); sp.Sleep(1000); sp.SendKeys("1") sp.Sleep(1000); sp.SendKeys("2") sp.Sleep(1000); sp.SendKeys("3") sp.Sleep(500); sp.SendKeys("~") }
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,294  Location: Tampa, FL Thanks: 28 times Was thanked: 404 time(s) in 349 post(s)
|
Timers execute in a thread. There are more than 1 script engine and your for loop is using the thread where the variable is defined, so it doesn't exist in the timer's thread. However, you need to cast the variable to a string since it (.Message) accepts an object and it's creating a reference issue. In this case, you don't need a timer (removed some stuff to simplify): Code:var info = new DisplayTextInfo();
info.Title = 'Test';
info.TitleAlignment = 'Center';
info.MessageAlignment = 'Left';
info.Duration = 3500;
info.Opacity = 0.7;
info.Location = 'top';
info.TitleFont = new Font('Segoe UI', 12, host.flags(FontStyle.Bold));
info.MessageFont = new Font('Segoe UI Semibold', 12);
info.BackColor = 'black';
info.Padding = 15;
info.UsePrimaryScreen = true;
sp.Sleep(600);
var ress = 4; // Number(sp.InputBox((" "));
for (ddi=1; ddi<=ress;ddi++){
info.Message = ""+ddi; // Cast variable to string
sp.DisplayText(info);
sp.Sleep(1000);
}
|
 1 user thanked Rob for this useful post.
|
|
|
Rank: Advanced Member
Groups: Approved
Joined: 9/26/2018(UTC) Posts: 69  Location: 北京 Thanks: 18 times Was thanked: 1 time(s) in 1 post(s)
|
Your plan perfectly solved my problem, thank you very much!
|
|
|
|
StrokesPlus.net Forum
»
General Discussion
»
Scripts
»
Why can Timers not get variables in the for loop?
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The StrokesPlus.net Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close