Jump to content

Using a variable result to call up a function


emog
 Share

Recommended Posts

Say you had 6 possible outcomes for a variable (for example a dice), and you had created 6 functions so that for any particular variable result it also carries out the function relating to that variable result. (Ie. a different function for each of the 1-6 outcomes of the die)...

... how would you call up a function directly, using the variable result, rather than having to go through the much more long-winded (theoretical) method of:

if $var = 1

function 1()

else

if $var = 2

function 2()

else

if $var = 3

function 3

else .....

...etc. So if the variable was 6, it would take 6 cycles to get to actually executing the relevant function.

My problem is that you can't use a number as the name of a function - a function name needs to start with a letter to distinguish it from merely being a number value. Additionally, in my program I don't have just 6 outcomes (I merely used a dice as an example), I actually have 200 possible outcomes for my variable and hence have an equal amount of functions, so looping through a huge if/then loop would be highly inefficient for each cycle.

I've tried to do the following without success:

$FunctionName = ("F" & $var)
; ^ Adding an alphabetic letter to the front of the variable
FunctionName()
; ^ Calling up the function

or a more simpler idea:

"F" & $var()
; ^ Calling up the function named F$var .... so if the previous variable result was the number 5, it would call up the function called "F5"

...but it says "Badly formatted Func statement" when executed.

I'm a bit of a noob, so would love to hear anyone's advice. Probably a really easy query.

Thanks :whistle:

Edited by emog
Link to comment
Share on other sites

That's gonna be a LOT of functions... You will have to create them all manually (or have you written another script to write the function code for you?).

I really wonder what you need 200 different functions for... That number makes me think it's possible that they all are the same function but just depending on some differing value... If you dont want to tell what all those functions are for then ok, but if you do, there might be hints we can give about generalizing the functionality to one or a couple of different functions (but using function parameters). This might a. help you learn a bit more about efficient coding, b. cut down drastically on your coding (copy-pasting? :whistle:) time, and c. cut down drastically on the amount of code :P

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

That's gonna be a LOT of functions... You will have to create them all manually (or have you written another script to write the function code for you?).

I really wonder what you need 200 different functions for... That number makes me think it's possible that they all are the same function but just depending on some differing value... If you dont want to tell what all those functions are for then ok, but if you do, there might be hints we can give about generalizing the functionality to one or a couple of different functions (but using function parameters). This might a. help you learn a bit more about efficient coding, b. cut down drastically on your coding (copy-pasting? :whistle:) time, and c. cut down drastically on the amount of code :P

Hi Bunny, you are of course completely right, on several accounts.

I had already written out the functions by using a copy/paste jobby and using the "find + replace" tool in WordPad to change various numbers en masse. So all 200 functions have already been created.

An example of a few of them would be:-

Func Pal16053205()
    MouseClick("left", $IBZ[0] + 202, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal15394989()
    MouseClick("left", $IBZ[0] + 210, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal14733186()
    MouseClick("left", $IBZ[0] + 218, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal14078299()
    MouseClick("left", $IBZ[0] + 226, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal13354290()
    MouseClick("left", $IBZ[0] + 234, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal16045525()
    MouseClick("left", $IBZ[0] + 242, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal15387309()
    MouseClick("left", $IBZ[0] + 250, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal14728835()
    MouseClick("left", $IBZ[0] + 258, $IBZ[1] + 81, 1, 0)
EndFunc

The only difference between them is that, depending on the result of a variable *, it moves the mouse and clicks a slightly different area of the screen.

* my variable in this case is a colour. The XXXXXXXX in the functions "PalXXXXXXXX" refer to a decimal representation of a RRGGBB code detected via the PixelGetColor command.

I'm a bit nooby (only started learning to program several days ago, lol), and I didn't want to flood the forum with beginner questions as I'm sure you all have your fair share of noobs on here, so the 200 functions seemed the most efficient thing (in terms of execution times) to do at the time (as opposed to running one humungous and hugely inefficient if/then/else loop), but I'm sure there's probably an easier way? :lmao:

Also wanted to say thank you for your other (last) post on this thread: http://www.autoitscript.com/forum/index.ph...mp;#entry314519

The "maze" analogy hammered the theory home, was a good way to visualise it. :D

Link to comment
Share on other sites

Func Pal16053205()
    MouseClick("left", $IBZ[0] + 202, $IBZ[1] + 81, 1, 0)
EndFunc

Func Pal15394989()
    MouseClick("left", $IBZ[0] + 210, $IBZ[1] + 81, 1, 0)
EndFunc

The only difference between them is that, depending on the result of a variable *, it moves the mouse and clicks a slightly different area of the screen. <...> but I'm sure there's probably an easier way? :P

What I would do, since you seem to have a list of those Pal's connected to the colors, put in in a file, like :

16053205@202

15394989@210

.....

and at startup, read that to an array with _fileReadToArray[$file,$array1] (see example in help), sort it with _ArraySort (use starting index 1 for the sort since element 0 contains special info from _fileReadToArray!) to sort it on the big numbers if the list isn't already sorted that way, then go through the array once more (using a for-next loop in this case, from 1 to $array1[0]) and use StringSplit to divide all those lines into two strings. These you can then put in two different arrays. One for the big numbers, one for the small numbers. Now you have a sorted bignumber-array and a smallnumber-array that contains all the small numbers on the same array index number as the big number they are connected to.

Now you can use the fast _arrayBinarySearch (can only be done on 1-dim. sorted array, that's why we needed two different arrays) to search for the big number, and just read out the small number and pass it to the function

Then you only need one function, like so:

while <some condition>
$bigNumber = <read the big number from wherever you get that>
$key = _arrayBinarySearch($bigNumbersArray,$bigNumber,1); again, use starting index 1
Pal($smallNumbersArray[$key])
sleep(<some time>) ; maybe, to not overload your CPU, if needed
wend

Func Pal($moveTo)
MouseClick("left", $IBZ[0] + $moveTo, $IBZ[1] + 81, 1, 0)
EndFunc
I have not tested this but it was pseudocode anyway, to explain the concept that I mean. There might be better ways to do it but I would do it like this, saves a whole lot on code.

Also wanted to say thank you for your other (last) post on this thread: http://www.autoitscript.com/forum/index.ph...mp;#entry314519

The "maze" analogy hammered the theory home, was a good way to visualise it. :whistle:

Good! Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

What I would do, since you seem to have a list of those Pal's connected to the colors, put in in a file, like :

16053205@202

15394989@210

.....

and at startup, read that to an array with _fileReadToArray[$file,$array1] (see example in help), sort it with _ArraySort (use starting index 1 for the sort since element 0 contains special info from _fileReadToArray!) to sort it on the big numbers if the list isn't already sorted that way, then go through the array once more (using a for-next loop in this case, from 1 to $array1[0]) and use StringSplit to divide all those lines into two strings. These you can then put in two different arrays. One for the big numbers, one for the small numbers. Now you have a sorted bignumber-array and a smallnumber-array that contains all the small numbers on the same array index number as the big number they are connected to.

Now you can use the fast _arrayBinarySearch (can only be done on 1-dim. sorted array, that's why we needed two different arrays) to search for the big number, and just read out the small number and pass it to the function

Then you only need one function, like so:

while <some condition>
$bigNumber = <read the big number from wherever you get that>
$key = _arrayBinarySearch($bigNumbersArray,$bigNumber,1); again, use starting index 1
Pal($smallNumbersArray[$key])
sleep(<some time>) ; maybe, to not overload your CPU, if needed
wend

Func Pal($moveTo)
MouseClick("left", $IBZ[0] + $moveTo, $IBZ[1] + 81, 1, 0)
EndFunc
I have not tested this but it was pseudocode anyway, to explain the concept that I mean. There might be better ways to do it but I would do it like this, saves a whole lot on code.

Good!

Hi Bunny, don't worry about the silence, I'm merely digesting and letting it sink in! Arrays are new for me, so am getting my head around it and the correct syntax.

Reading your code and explanation, and referring to the help file, I think I've got the gist. Weeee!

So the array database in this case would be stored in a separate (physical) text file to the script? What would the file type (extension) be when you create that file (in notepad)? Does it even need an extension, or could it just be <filename>?

Also, the array I require needs to store two values per $bigNumber, not just the one "@", as the 202,81 etc. are in fact (X,Y) co-ordinates and the 81 value does change, too (even though it didn't in the example functions I gave).

Would you need two separate array files for that, or can you somehow add a second value to each of the $bigNumber entries? If the latter, how would you recall the two values (co-ordinates) from each $bigNumber

Thanks again :whistle:

Link to comment
Share on other sites

Hi Bunny, don't worry about the silence, I'm merely digesting and letting it sink in! Arrays are new for me, so am getting my head around it and the correct syntax.

Some people say silence is gold. I disagree with that, but I can make more than enough noise to entertain myself! Don't worry. You're not obliged, required or forced to answer... :lmao:

So the array database in this case would be stored in a separate (physical) text file to the script? What would the file type (extension) be when you create that file (in notepad)? Does it even need an extension, or could it just be <filename>?

I meant I would store it in a file, and then read it into an array like I said. You can also hardcode it into your script (but that makes your script ugly-ly (is that English?) long), or put it in another script and use #include <putNumbersInArrays_OrSomething.au3> (which does exactly the same but makes your code more readable)... Question of taste.

/edit: About the extensions: I believe Notepad creates files with .txt by default. But you can rename that to anything you want. As long as the file is plaintext and contains the information you want to read in the format you expect, the fileReadToArray will just open whatever file you specify and start working.

Also, the array I require needs to store two values per $bigNumber, not just the one "@", as the 202,81 etc. are in fact (X,Y) co-ordinates and the 81 value does change, too (even though it didn't in the example functions I gave).

Would you need two separate array files for that, or can you somehow add a second value to each of the $bigNumber entries? If the latter, how would you recall the two values (co-ordinates) from each $bigNumber

I would just make the file contain something like 16053205@202@81 <newline> 16053205@210@105 <newline> etcetera, and creating a third separate array like you said. Stringsplit will return those three values just as easily. Then change the 'Func Pal($moveTo)' to 'Func Pal($moveToX,$moveToY)' to be able to pass two parameters to the function instead of one, then just use the $moveToY instead of the 81. It doesn't change anything in the general idea. You could even add a Z-coordinate too if you wanted, just extend it one step further :whistle:

Thanks again :P

No problem. :D Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...