Bookworm Posted September 19, 2008 Share Posted September 19, 2008 (edited) First of all thanks to everybody who has already contributed to form. This is my first post, but I have used this forum extensively to self teach myself AutoIt which is saying a lot considering I have no former programming experience. Okay so here’s my problem I’m writing an educational program that will require the student to do lessons that are generated randomly by the program. I’ve already wrote some script which works, but it seems excessively long for the small operation that he needs to perform. Any suggestions on how to shorten the code would be greatly appreciated. I’ve already spent the better part of two days working on this small piece of the script. Also if you could include a working example it would help me out a lot. Thanks CODE$lesson1 = 0 $lesson2 = 0 $lesson3 = 0 $lesson4 = 0 $lesson5 = 0 $lesson6 = 0 $lesson7 = 0 $lesson8 = 0 $lesson9 = 0 $lesson10 = 0 $While_out1 = 1 $While_out2 = 1 $nMsg = 10 While $While_out1 = 1 $RandomLesson = Random(1,10, 1) $TempVariable = $RandomLesson $While_out2 = 1 While $While_out2 = 1 Switch $nMsg Case $lesson1 = 0 $lesson1 = $RandomLesson Select Case $lesson1 = 1 #include "L1.AU3" EndSelect Select Case $lesson1 = 2 #include "L3.AU3" EndSelect Select Case $lesson1 = 3 #include "L3.AU3" EndSelect Select Case $lesson1 = 4 #include "L4.AU3" EndSelect Select Case $lesson1 = 5 #include "L5.AU3" EndSelect Select Case $lesson1 = 6 #include "L6.AU3" EndSelect Select Case $lesson1 = 7 #include "L7.AU3" EndSelect Select Case $lesson1 = 8 #include "L8.AU3" EndSelect Select Case $lesson1 = 9 #include "L9.AU3" EndSelect Select Case $lesson1 = 10 #include "L10.AU3" EndSelect Case $lesson1 = $TempVariable $While_out2 = 0 Case $lesson2 = 0 $lesson2 = $RandomLesson Select Case $lesson2 = 1 #include "L1.AU3" EndSelect Select Case $lesson2 = 2 #include "L3.AU3" EndSelect Select Case $lesson2 = 3 #include "L3.AU3" EndSelect Select Case $lesson2 = 4 #include "L4.AU3" EndSelect Select Case $lesson2 = 5 #include "L5.AU3" EndSelect Select Case $lesson2 = 6 #include "L6.AU3" EndSelect Select Case $lesson2 = 7 #include "L7.AU3" EndSelect Select Case $lesson2 = 8 #include "L8.AU3" EndSelect Select Case $lesson2 = 9 #include "L9.AU3" EndSelect Select Case $lesson2 = 10 #include "L10.AU3" EndSelect Case $lesson2 = $TempVariable $While_out2 = 0 Case $lesson3 = 0 $lesson3 = $RandomLesson Select Case $lesson3 = 1 #include "L1.AU3" EndSelect Select Case $lesson3 = 2 #include "L3.AU3" EndSelect Select Case $lesson3 = 3 #include "L3.AU3" EndSelect Select Case $lesson3 = 4 #include "L4.AU3" EndSelect Select Case $lesson3 = 5 #include "L5.AU3" EndSelect Select Case $lesson3 = 6 #include "L6.AU3" EndSelect Select Case $lesson3 = 7 #include "L7.AU3" EndSelect Select Case $lesson3 = 8 #include "L8.AU3" EndSelect Select Case $lesson3 = 9 #include "L9.AU3" EndSelect Select Case $lesson3 = 10 EndSelect Case $lesson3 = $TempVariable $While_out2 = 0 Case $lesson4 = 0 $lesson4 = $RandomLesson Select Case $lesson4 = 1 #include "L1.AU3" EndSelect Select Case $lesson4 = 2 #include "L3.AU3" EndSelect Select Case $lesson4 = 3 #include "L3.AU3" EndSelect Select Case $lesson4 = 4 #include "L4.AU3" EndSelect Select Case $lesson4 = 5 #include "L5.AU3" EndSelect Select Case $lesson4 = 6 #include "L6.AU3" EndSelect Select Case $lesson4 = 7 #include "L7.AU3" EndSelect Select Case $lesson4 = 8 #include "L8.AU3" EndSelect Select Case $lesson4 = 9 #include "L9.AU3" EndSelect Select Case $lesson4 = 10 #include "L10.AU3" EndSelect Case $lesson4 = $TempVariable $While_out2 = 0 Case $lesson5 = 0 $lesson5 = $RandomLesson Select Case $lesson5 = 1 #include "L1.AU3" EndSelect Select Case $lesson5 = 2 #include "L3.AU3" EndSelect Select Case $lesson5 = 3 #include "L3.AU3" EndSelect Select Case $lesson5 = 4 #include "L4.AU3" EndSelect Select Case $lesson5 = 5 #include "L5.AU3" EndSelect Select Case $lesson5 = 6 #include "L6.AU3" EndSelect Select Case $lesson5 = 7 #include "L7.AU3" EndSelect Select Case $lesson5 = 8 #include "L8.AU3" EndSelect Select Case $lesson5 = 9 #include "L9.AU3" EndSelect Select Case $lesson5 = 10 #include "L10.AU3" EndSelect Case $lesson5 = $TempVariable $While_out2 = 0 Case $lesson6 = 0 $lesson6 = $RandomLesson Select Case $lesson6 = 1 #include "L1.AU3" EndSelect Select Case $lesson6 = 2 #include "L3.AU3" EndSelect Select Case $lesson6 = 3 #include "L3.AU3" EndSelect Select Case $lesson6 = 4 #include "L4.AU3" EndSelect Select Case $lesson6 = 5 #include "L5.AU3" EndSelect Select Case $lesson6 = 6 #include "L6.AU3" EndSelect Select Case $lesson6 = 7 #include "L7.AU3" EndSelect Select Case $lesson6 = 8 #include "L8.AU3" EndSelect Select Case $lesson6 = 9 #include "L9.AU3" EndSelect Select Case $lesson6 = 10 #include "L10.AU3" EndSelect Case $lesson6 = $TempVariable $While_out2 = 0 Case $lesson7 = 0 $lesson7 = $RandomLesson Select Case $lesson7 = 1 #include "L1.AU3" EndSelect Select Case $lesson7 = 2 #include "L3.AU3" EndSelect Select Case $lesson7 = 3 #include "L3.AU3" EndSelect Select Case $lesson7 = 4 #include "L4.AU3" EndSelect Select Case $lesson7 = 5 #include "L5.AU3" EndSelect Select Case $lesson7 = 6 #include "L6.AU3" EndSelect Select Case $lesson7 = 7 #include "L7.AU3" EndSelect Select Case $lesson7 = 8 #include "L8.AU3" EndSelect Select Case $lesson7 = 9 #include "L9.AU3" EndSelect Select Case $lesson7 = 10 #include "L10.AU3" EndSelect Case $lesson7 = $TempVariable $While_out2 = 0 Case $lesson8 = 0 $lesson8 = $RandomLesson Select Case $lesson8 = 1 #include "L1.AU3" EndSelect Select Case $lesson8 = 2 #include "L3.AU3" EndSelect Select Case $lesson8 = 3 #include "L3.AU3" EndSelect Select Case $lesson8 = 4 #include "L4.AU3" EndSelect Select Case $lesson8 = 5 #include "L5.AU3" EndSelect Select Case $lesson8 = 6 #include "L6.AU3" EndSelect Select Case $lesson8 = 7 #include "L7.AU3" EndSelect Select Case $lesson8 = 8 #include "L8.AU3" EndSelect Select Case $lesson8 = 9 #include "L9.AU3" EndSelect Select Case $lesson8 = 10 #include "L10.AU3" EndSelect Case $lesson8 = $TempVariable $While_out2 = 0 Case $lesson9 = 0 $lesson9 = $RandomLesson Select Case $lesson9 = 1 #include "L1.AU3" EndSelect Select Case $lesson9 = 2 #include "L3.AU3" EndSelect Select Case $lesson9 = 3 #include "L3.AU3" EndSelect Select Case $lesson9 = 4 #include "L4.AU3" EndSelect Select Case $lesson9 = 5 #include "L5.AU3" EndSelect Select Case $lesson9 = 6 #include "L6.AU3" EndSelect Select Case $lesson9 = 7 #include "L7.AU3" EndSelect Select Case $lesson9 = 8 #include "L8.AU3" EndSelect Select Case $lesson9 = 9 #include "L9.AU3" EndSelect Select Case $lesson9 = 10 #include "L10.AU3" EndSelect Case $lesson9 = $TempVariable $While_out2 = 0 Case $lesson10 = 0 $lesson10 = $RandomLesson Select Case $lesson10 = 1 #include "L1.AU3" EndSelect Select Case $lesson10 = 2 #include "L3.AU3" EndSelect Select Case $lesson10 = 3 #include "L3.AU3" EndSelect Select Case $lesson10 = 4 #include "L4.AU3" EndSelect Select Case $lesson10 = 5 #include "L5.AU3" EndSelect Select Case $lesson10 = 6 #include "L6.AU3" EndSelect Select Case $lesson10 = 7 #include "L7.AU3" EndSelect Select Case $lesson10 = 8 #include "L8.AU3" EndSelect Select Case $lesson10 = 9 #include "L9.AU3" EndSelect Select Case $lesson10 = 10 #include "L10.AU3" EndSelect Case $lesson10 = $TempVariable $While_out2 = 0 $While_out1 = 0 EndSwitch WEnd WEndL2.au3L1.AU3L3.au3L4.au3L5.au3L6.au3L7.au3L8.au3L9.au3L10.au3 Edited September 19, 2008 by SmOke_N Added codebox tags for scrolling Link to comment Share on other sites More sharing options...
Community On Patrol Posted September 19, 2008 Share Posted September 19, 2008 Hi Bookworm,1st Welcome to the AutoIt Forums! Some of the following tips may not apply to you, but it may make your life a bit easier here on the forum in the future.CODEDid you know that we have an awesome search feature? You can find many answers to your current questions, just by typing in the right search patterns.A suggestion is to use the Advanced Search mode:Type your specific search term in quotes.Click the forum you want to search in (the one most likely to have your information would generally be the Example Script forum and or the General Help and Support Forum).Click on "Search titles only" radio button.Click perform search.The above will help you narrow down your searches and prevent you from unneccesarily posting a new thread.[*]Also, you should try to read the Sticky posts that are at the top of each of the AutoIt Forums you enter such as:FAQs (Frequently Asked Questions)Are my AutoIt EXEs really infected? (About false positives and what to do if you may encounter one)[*]Keep in mind, the help file will be your best friend, however you may find some of the tutorials written by some of our elite forum members helpful.Valuaters Interactive AutoIt 1-2-3 tutorialLxPs Learning to script with AutoIt3[*]Forum Etiquette:Making a new thread:Use the Search feature first to see if your question has already been answered.Look in the help file as well before even thinking of posting (When what you want could be obtained by simply reading the help file, you don't generally get a good response from your AutoIt community).Titles are very important here. 1 word titles or titles like "help me", "write something for me", "I'm a noob" etc... aren't tolerated.Make sure you are posting in the correct forum:General Help and Support:This forum is for AutoIt related support questions only. If you have a question related to another language, or nothing at all to do with AutoIt then you need to post in the chat forum, or in that languages perspective forum.Example Script:This forum is for AutoIt scripts/executables only.Source code is preferred but not necessary, you do have the right to just post the binary of your project if you wish.Please don't post questions in this forum unless it's directly related to a thread already existing.Use common sense when creating a new thread. Ask yourself if the title is descriptive enough to even interest someone (preferably those that know what they are talking about) to even look at your thread, let alone reply in it.Think about how it would show in the search feature if someone were to look for something just like you are looking for (think of the keywords you used yourself and obviously didn't find anything (because we know you used the search feature ) and use those types of keywords in your title as well).Thread content:Be descriptive with your query. (Make sure we actually know what you want to do).Show you've made an effort in coding what you want (provide the reproducer code (generally no more than 50 lines as people lose interest in debugging someones script for free)).Don't talk in ebonics. A lot of the forum members are adults, and a lot of them know how to help you, but talk like a child, you'll be treated as such.Don't ask for help making keyloggers, spam (even if it's to do as a prank), or anything that can be thought of as malicious. You'll more than likely have the thread locked by a moderator, and take a bashing from your fellow AutoIt community.When posting code, use code boxes. This can be accomplished by using [code ]<content here>[/code ] (No spaces between the brackets []).Using code boxes will keep the indentation and make it easier to read for others to help you.Bumping your threads:Use common courtesy here.Keep in mind every time you bump your thread to the top of the forum, you knock the other threads down a notch.Everyone posting for help has just as much right for their threads to get read as you do.Because of that, do not bump your post more than once in a 24 hour period.A Bump is simply posting in your thread with nothing that pertains to your query with the sole purpose of moving it up.Deleting previous bumps, and posting new ones is not tolerated, and the moderators can find those deletions, so do yourself a favor and don't cross that line >_< .Rude or obnoxious content:This falls pretty much under the common sense thing. If you use it (common sense) before posting, you won't have issues.Don't use foul language, remember, a lot of the community is at work when they read these threads.Don't provoke or instigate an argument with someone.Double Posting:It's understood that sometimes there's a lag in the system, and sometimes people don't see their post go up right aways so they post again.If this happens to you, simply notify a moderator with the report feature in the post, and politely ask them to delete it.If you're just creating another topic because your original topic is not being answered the way you want or at all, this is not tolerated. You could lose your posting privileges all together over it.Non-English languagesIf English is not your primary language, please make an attempt to interpret (yourself or online) and post that interpretation.We have wonderful users from around the world, so after you've done your post in English, back it up with your question also in your native tongue (You may find your answer much quicker using both).That's it for now, I hope you have a wonderful learning experience, and hope to see you contribute to the community as your knowledge grows. Link to comment Share on other sites More sharing options...
TehWhale Posted September 19, 2008 Share Posted September 19, 2008 For all the Case/Select/Endselects you can use Case $Lesson10 = 0 for example, and then like: Case $Lesson10 = 0 Switch $Lesson10 Case 1 #Include "blah..au3" Case 2 #include "blah..au3" EndSwitch That's just some short code as an example, to shorten it. And theres no need to #Include those, just use this: Case $Lesson10 = 0 Switch $Lesson10 Case 1 MsgBox(0, "", "testy, 1") Case 2 MsgBox(0, "", "Testy, 2") EndSwitch etc. Good luck my friend. If I have some more spare time, I can rewrite this code for you, if this doesn't help! Link to comment Share on other sites More sharing options...
Bookworm Posted September 19, 2008 Author Share Posted September 19, 2008 Thanks Alienware I removed all the Select/Endselects like you said and it helped shorten the script a bit. I left the #includes in though because Im going to use them in my program. I only had message boxes in them now to help me troubleshoot without having to go through the rest of the program. Do you know if theres any other way to shorten this particular section? The program will eventually have over a hundred lessons that will all have to be randomly selected in groups of tens. I can write it in the way I have it now, but it seems like an awfully big waste of time/space. expandcollapse popup$lesson1 = 0 $lesson2 = 0 $lesson3 = 0 $lesson4 = 0 $lesson5 = 0 $lesson6 = 0 $lesson7 = 0 $lesson8 = 0 $lesson9 = 0 $lesson10 = 0 $While_out1 = 1 $While_out2 = 1 $nMsg = 10 While $While_out1 = 1 $RandomLesson = Random(1,10, 1) $TempVariable = $RandomLesson $While_out2 = 1 While $While_out2 = 1 Switch $nMsg Case $lesson1 = 0 $lesson1 = $RandomLesson Switch $lesson1 Case $lesson1 = 1 #include "L1.AU3" Case $lesson1 = 2 #include "L3.AU3" Case $lesson1 = 3 #include "L3.AU3" Case $lesson1 = 4 #include "L4.AU3" Case $lesson1 = 5 #include "L5.AU3" Case $lesson1 = 6 #include "L6.AU3" Case $lesson1 = 7 #include "L7.AU3" Case $lesson1 = 8 #include "L8.AU3" Case $lesson1 = 9 #include "L9.AU3" Case $lesson1 = 10 #include "L10.AU3" EndSwitch Case $lesson1 = $TempVariable $While_out2 = 0 Case $lesson2 = 0 $lesson2 = $RandomLesson Switch $lesson2 Case $lesson2 = 1 #include "L1.AU3" Case $lesson2 = 2 #include "L3.AU3" Case $lesson2 = 3 #include "L3.AU3" Case $lesson2 = 4 #include "L4.AU3" Case $lesson2 = 5 #include "L5.AU3" Case $lesson2 = 6 #include "L6.AU3" Case $lesson2 = 7 #include "L7.AU3" Case $lesson2 = 8 #include "L8.AU3" Case $lesson2 = 9 #include "L9.AU3" Case $lesson2 = 10 #include "L10.AU3" EndSwitch Case $lesson2 = $TempVariable $While_out2 = 0 Case $lesson3 = 0 $lesson3 = $RandomLesson Switch $lesson3 Case $lesson3 = 1 #include "L1.AU3" Case $lesson3 = 2 #include "L3.AU3" Case $lesson3 = 3 #include "L3.AU3" Case $lesson3 = 4 #include "L4.AU3" Case $lesson3 = 5 #include "L5.AU3" Case $lesson3 = 6 #include "L6.AU3" Case $lesson3 = 7 #include "L7.AU3" Case $lesson3 = 8 #include "L8.AU3" Case $lesson3 = 9 #include "L9.AU3" Case $lesson3 = 10 EndSwitch Case $lesson3 = $TempVariable $While_out2 = 0 Case $lesson4 = 0 $lesson4 = $RandomLesson Switch $lesson4 Case $lesson4 = 1 #include "L1.AU3" Case $lesson4 = 2 #include "L3.AU3" Case $lesson4 = 3 #include "L3.AU3" Case $lesson4 = 4 #include "L4.AU3" Case $lesson4 = 5 #include "L5.AU3" Case $lesson4 = 6 #include "L6.AU3" Case $lesson4 = 7 #include "L7.AU3" Case $lesson4 = 8 #include "L8.AU3" Case $lesson4 = 9 #include "L9.AU3" Case $lesson4 = 10 #include "L10.AU3" EndSwitch Case $lesson4 = $TempVariable $While_out2 = 0 Case $lesson5 = 0 $lesson5 = $RandomLesson Switch $lesson5 Case $lesson5 = 1 #include "L1.AU3" Case $lesson5 = 2 #include "L3.AU3" Case $lesson5 = 3 #include "L3.AU3" Case $lesson5 = 4 #include "L4.AU3" Case $lesson5 = 5 #include "L5.AU3" Case $lesson5 = 6 #include "L6.AU3" Case $lesson5 = 7 #include "L7.AU3" Case $lesson5 = 8 #include "L8.AU3" Case $lesson5 = 9 #include "L9.AU3" Case $lesson5 = 10 #include "L10.AU3" EndSwitch Case $lesson5 = $TempVariable $While_out2 = 0 Case $lesson6 = 0 $lesson6 = $RandomLesson Switch $lesson6 Case $lesson6 = 1 #include "L1.AU3" Case $lesson6 = 2 #include "L3.AU3" Case $lesson6 = 3 #include "L3.AU3" Case $lesson6 = 4 #include "L4.AU3" Case $lesson6 = 5 #include "L5.AU3" Case $lesson6 = 6 #include "L6.AU3" Case $lesson6 = 7 #include "L7.AU3" Case $lesson6 = 8 #include "L8.AU3" Case $lesson6 = 9 #include "L9.AU3" Case $lesson6 = 10 #include "L10.AU3" EndSwitch Case $lesson6 = $TempVariable $While_out2 = 0 Case $lesson7 = 0 $lesson7 = $RandomLesson Switch $lesson6 Case $lesson7 = 1 #include "L1.AU3" Case $lesson7 = 2 #include "L3.AU3" Case $lesson7 = 3 #include "L3.AU3" Case $lesson7 = 4 #include "L4.AU3" Case $lesson7 = 5 #include "L5.AU3" Case $lesson7 = 6 #include "L6.AU3" Case $lesson7 = 7 #include "L7.AU3" Case $lesson7 = 8 #include "L8.AU3" Case $lesson7 = 9 #include "L9.AU3" Case $lesson7 = 10 #include "L10.AU3" EndSwitch Case $lesson7 = $TempVariable $While_out2 = 0 Case $lesson8 = 0 $lesson8 = $RandomLesson Switch $lesson8 Case $lesson8 = 1 #include "L1.AU3" Case $lesson8 = 2 #include "L3.AU3" Case $lesson8 = 3 #include "L3.AU3" Case $lesson8 = 4 #include "L4.AU3" Case $lesson8 = 5 #include "L5.AU3" Case $lesson8 = 6 #include "L6.AU3" Case $lesson8 = 7 #include "L7.AU3" Case $lesson8 = 8 #include "L8.AU3" Case $lesson8 = 9 #include "L9.AU3" Case $lesson8 = 10 #include "L10.AU3" EndSwitch Case $lesson8 = $TempVariable $While_out2 = 0 Case $lesson9 = 0 $lesson9 = $RandomLesson Switch $lesson9 Case $lesson9 = 1 #include "L1.AU3" Case $lesson9 = 2 #include "L3.AU3" Case $lesson9 = 3 #include "L3.AU3" Case $lesson9 = 4 #include "L4.AU3" Case $lesson9 = 5 #include "L5.AU3" Case $lesson9 = 6 #include "L6.AU3" Case $lesson9 = 7 #include "L7.AU3" Case $lesson9 = 8 #include "L8.AU3" Case $lesson9 = 9 #include "L9.AU3" Case $lesson9 = 10 #include "L10.AU3" EndSwitch Case $lesson9 = $TempVariable $While_out2 = 0 Case $lesson10 = 0 $lesson10 = $RandomLesson Switch $lesson10 Case $lesson10 = 1 #include "L1.AU3" Case $lesson10 = 2 #include "L3.AU3" Case $lesson10 = 3 #include "L3.AU3" Case $lesson10 = 4 #include "L4.AU3" Case $lesson10 = 5 #include "L5.AU3" Case $lesson10 = 6 #include "L6.AU3" Case $lesson10 = 7 #include "L7.AU3" Case $lesson10 = 8 #include "L8.AU3" Case $lesson10 = 9 #include "L9.AU3" Case $lesson10 = 10 #include "L10.AU3" EndSwitch Case $lesson10 = $TempVariable $While_out2 = 0 $While_out1 = 0 EndSwitch WEnd WEnd For all the Case/Select/Endselects you can use Case $Lesson10 = 0 for example, and then like: Case $Lesson10 = 0 Switch $Lesson10 Case 1 #Include "blah..au3" Case 2 #include "blah..au3" EndSwitch That's just some short code as an example, to shorten it. And theres no need to #Include those, just use this: Case $Lesson10 = 0 Switch $Lesson10 Case 1 MsgBox(0, "", "testy, 1") Case 2 MsgBox(0, "", "Testy, 2") EndSwitch etc. Good luck my friend. If I have some more spare time, I can rewrite this code for you, if this doesn't help! Link to comment Share on other sites More sharing options...
TehWhale Posted September 19, 2008 Share Posted September 19, 2008 You would probably shorten your code a lot, if you used OnEvent mode. It would call a function, and should shorten it a little. Link to comment Share on other sites More sharing options...
Valuater Posted September 20, 2008 Share Posted September 20, 2008 Think about this.... it's not going to work like you want! That is not how "Includes" are handled 8) Link to comment Share on other sites More sharing options...
ankitstr Posted September 20, 2008 Share Posted September 20, 2008 (edited) Well, you can use the following piece of code if you intend on having 10 .AU3 files for your script. expandcollapse popupGlobal $nMsg = 10 Global $lessonArray[$nMsg] For $n=0 To $nMsg-1 $lessonArray[$n]=0 Next Func lesson($RandomNo) $flag=1 For $n = 0 To $nMsg-1 If $lessonArray[$n]=$RandomNo And $lessonArray[$nMsg-1]=0 Then $flag=2 EndIf If $lessonArray[$n]=0 And $flag=1 Then $lessonArray[$n]=$RandomNo $flag=0 Switch $RandomNo Case 1 #include "L1.AU3" Case 2 #include "L2.AU3" Case 3 #include "L3.AU3" Case 4 #include "L4.AU3" Case 5 #include "L5.AU3" Case 6 #include "L6.AU3" Case 7 #include "L7.AU3" Case 8 #include "L8.AU3" Case 9 #include "L9.AU3" Case 10 #include "L10.AU3" Case Else Exit 0 EndSwitch EndIf Next If $flag=1 Then Return 0 Else Return 1 EndIf EndFunc While lesson(Random(1,$nMsg,1)) Sleep(100) WEnd But a better way would be to #include "Lessons.AU3" at the top and write all the lessons in 10 different functions say lesson1()... lesson10(), and change the lesson() function as follows: Func lesson($RandomNo) $flag=1 For $n = 0 To $nMsg-1 If $lessonArray[$n]=$RandomNo And $lessonArray[$nMsg-1]=0 Then $flag=2 EndIf If $lessonArray[$n]=0 And $flag=1 Then $lessonArray[$n]=$RandomNo $flag=0 Call("lesson"&$RandomNo) EndIf Next If $flag=1 Then Return 0 Else Return 1 EndIf EndFunc Edited September 20, 2008 by ankitstr [topic="80940"] AutoScript Creator, Script to automate anything![/topic] Link to comment Share on other sites More sharing options...
PsaltyDS Posted September 20, 2008 Share Posted September 20, 2008 This topic has gone off the rails here: #include statements are NOT CONDITIONAL because they are parsed by the compiler at compile-time. Every bit of code in the #include file will be there at run-time.So when you do this: Switch $RandomNo Case 1 #include "L1.AU3" Case 2 #include "L2.AU3" EndSwitchEvery bit of the contents in L1.au3 and L2.au3 is present at those locations in the compiled script code. Execution of that code will be conditional, but inclusion of the code is not. This presents more problems because the normal use of an #include file is to provide Global variables and function declarations, neither of which should be conditional.This is a bad idea, like painting with oil-based over latex, and you will save yourself trouble later by not starting it out this way.If you want to make each lesson an #include file that automatically becomes part of a random selection then try this:1. Make your script standard format with all includes at the top: ; Main script #include <Lessons_A1.au3> #include <Lessons_B2.au3> #include <Array.au3> ; Debug: Display list of lessons _ArrayDisplay($avLessons, "Debug: $avLessons") ; Call a random lesson $iLesson = Random(1, $avLessons[0], 1) Call($avLessons[$iLesson])Each of these includes is a self-contained lesson, and each of the lesson include files is in the same format (two provided as examples): Lessons_A1.au3: #include-once #include <Lessons_Global.au3> ; ===> Change this value for each new lesson <=== ; Define the lesson name in this include file $sLessonName = "Lesson_A1" ; Add this lesson to the global array of lesson names ReDim $avLessons[UBound($avLessons) + 1] $avLessons[0] = UBound($avLessons) - 1 $avLessons[$avLessons[0]] = $sLessonName ; ===> Change this function name to match $sLessonName <=== ; Declare the function for the lesson Func Lesson_A1() ; Everything inside this function runs as this lesson ; This is where the unique content for each lesson goes MsgBox(64, $sLessonName, "Here be monsters...") EndFuncLessons_B2.au3: #include-once #include <Lessons_Global.au3> ; ===> Change this value for each new lesson <=== ; Define the lesson name in this include file $sLessonName = "Lesson_B2" ; Add this lesson to the global array of lesson names ReDim $avLessons[UBound($avLessons) + 1] $avLessons[0] = UBound($avLessons) - 1 $avLessons[$avLessons[0]] = $sLessonName ; ===> Change this function name to match $sLessonName <=== ; Declare the function for the lesson Func Lesson_B2() ; Everything inside this function runs as this lesson ; This is where the unique content for each lesson goes MsgBox(64, $sLessonName, "Here be monsters...") EndFuncThe lesson include files also make sure Lessons_Global.au3 is included first, because it contains the global variables and common functions used by every lesson:Lessons_Global.au3: #include-once ; Global array of available lesson names Global $avLessons[1] = [0] ; Additional functions used by all lessons: Func _Lesson_Start() ; ... EndFunc Func _Lesson_End() ; ... EndFunc Func _Lesson_Score() ; ... EndFunc Func _Lesson_RecordResults() ; ... EndFuncThe #include-once tag is critical to this working. Even though every lesson's include file also includes the global file, it will still only be included once because of the #include-once tag. In addition, if you accidentally listed the same lesson's include file twice in the primary script it will still only occur once because of the #include-once tag.When the primary script is compiled, the first code compiled in is (in this example) Lessons_A1.au3, but the first lines of that include file also include Lessons_Global.au3, so the global stuff gets done first, and only gets done once (#include-once tag again).The Lessons_Global code creates a global array that contains a list of all lesson names, which are also the names of the functions to call in order to run the lesson. The limitation of that is the lesson name must fit the rules for a valid function name also (i.e. no spaces).Now if you run the primary script with as many lesson module #include files as you care to create, it will randomly select a lesson and run the function for that lesson. This can be easily modified to run them all in a random order, or a randomly selected subset of lessons, etc., etc.Note that in coding the function for each lesson that anything you find yourself coding over and over again should be turned into a function itself and included in the Lessons_Global include file. The general purpose functions in Lessons_Global will then be available to every new lesson include file you create.When you feel a little more advanced, you won't want any AutoIt code at all in the individual lessons anymore. Then each lesson will become just an XML or database file of some sort that will be opened and presented by the script.Hope that helps and gives a better idea of how to use #include files. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now