Jump to content
Sign in to follow this  
rcmaehl

Making Function

Recommended Posts

rcmaehl

I need help converting this:

Case $Msg = $Item_Block_All
        $Block_All = True
        ContinueCase

;   Update Shutdown Blocking Status
    Case $Msg = $Item_Block_Shutdown
        If Not $Block_Shutdown Then
            GUICtrlSetState($Item_Block_Shutdown, $GUI_CHECKED)
            FileSetAttrib($Settings, "-RSH")
            INIWrite($Settings, "Blocking", "Shutdown", "True")
            FileSetAttrib($Settings, "+RSH")
            $Block_Shutdown = True
        Else
            GUICtrlSetState($Item_Block_Shutdown, $GUI_UNCHECKED)
            FileSetAttrib($Settings, "-RSH")
            INIWrite($Settings, "Blocking", "Shutdown", "False")
            FileSetAttrib($Settings, "+RSH")
            $Block_Shutdown = False
        EndIf
        If $Block_All Then ContinueCase

;   Update Taskmgr Blocking Status
    Case $Msg = $Item_Block_Taskmgr
        If Not $Block_Taskmgr Then
            GUICtrlSetState($Item_Block_Taskmgr, $GUI_CHECKED)
            FileSetAttrib($Settings, "-RSH")
            INIWrite($Settings, "Blocking", "Taskmgr", "True")
            FileSetAttrib($Settings, "+RSH")
            $Block_Taskmgr = True
        Else
            GUICtrlSetState($Item_Block_Taskmgr, $GUI_CHECKED)
            FileSetAttrib($Settings, "-RSH")
            INIWrite($Settings, "Blocking", "Taskmgr", "True")
            FileSetAttrib($Settings, "+RSH")
        EndIf
        If $Block_All Then $Block_All = False

Into a independent function using an array to get rid of most of the above variables (EG. State to change to, Current State), kinda like:

Global $Blocking[2] = [0,0]

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
smartee

Um, your question is a bit too vague, where is your attempt? What exactly is hampering your ability to make it into a function yourself? :mellow:

Share this post


Link to post
Share on other sites
rcmaehl

Um, your question is a bit too vague, where is your attempt? What exactly is hampering your ability to make it into a function yourself? :mellow:

So far all my functions are called by a Single Case statement. I'm not sure how to call the function and tell it what to do from multiple Case statements.

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
MvGulik

Read Func...Return...EndFunc

;; function definition.
Func SimpleFunction()
    ;; Internal function code.
EndFunc
;; function call.
SimpleFunction()

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
rcmaehl

Read Func...Return...EndFunc

;; function definition.
Func SimpleFunction()
    ;; Internal function code.
EndFunc
;; function call.
SimpleFunction()

I understand that however I'm trying to do somethig like:

Global $Array[2] = [0,0]

Case $MenuOption1
$Array[0] = $Array[0] + 1
Function()

Case $MenuOption2
$Array[0] = $Array[0] + 2
Function()

Case $MenuOption3
$Array[0] = $Array[0] + 3
Function()

Function()
If $Array[0] = 3 or $Array[0] = 2 Then
something
$Array[0] = $Array[0] - 2
$Array[1] = $Array[1] + 2
EndIf
If $Array[0] = 1 Then
Something
$Array[0] = $Array[0] - 1
$Array[1] = $Array[1] + 1
EndIf
EndFunc

And make the function do something based on that however both case statements have 2 states (checked/unchecked) and I need to figure out how to adjust for that in the function without adding variables (unless it can be added to the current $array[] somehow)

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
MvGulik

I see your using CASE in a way that's not boosting my confidence in your AutoIt knowledge.

Ergo: Read ...

Select...Case...EndSelect

Switch...Case...EndSwitch

---

And function definitions need to be put at root code level.

So not inside any loop,select,switch, or other function code block.

---

To understand good function definition practice see next example:

;; some code.
;; function call.
SimpleFunction()
;; some more code.
Exit ;; no more direct root code.
;; -- My Functions --
;; function definition.
Func SimpleFunction()
    ;; Internal function code.
EndFunc
Function calls can go just about anywhere you like. Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
Blue_Drache

That's not a function.

Functions return something useful.

Also, modifying globals inside a function is bad coding practice. Pass the array in as a function parameter and then return it out.

$aVariable = SomeFunc($aArray)
Func SomeFunc($aParam)
If Not IsArray($aParam) then
   Consolewrite("Non Array Variable.  Exiting Function" & @CRLF)
   Return -1
   SetError(0)
EndIf
; Other conditions
EndFunc
Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
rcmaehl

:mellow: It was an example of what I was trying to do, it wasn't a script I coded. I KNOW functions don't go inside loops. Well I guess I could manually type out an entire example script. DISCLAIMER: THE SCRIPT BELOW IS NOT AND NEVER WILL BE EXECUTED IT IS AN EXAMPLE AND PROBABLY CONTAINS ERRORS.

Global $Array[2] = [0,0]

Func Example()
While 1
$Msg = GUIGetMsg()
Select
Case $Msg = $MenuOption1
$Array[0] = $Array[0] + 1
Function()

Case $Msg = $MenuOption2
$Array[0] = $Array[0] + 2
Function()

Case $Msg = $MenuOption3
$Array[0] = $Array[0] + 3
Function()

EndSelect
WEnd
EndFunc

Function()
If $Array[0] = 3 or $Array[0] = 2 Then
something
$Array[0] = $Array[0] - 2
$Array[1] = $Array[1] + 2
EndIf
If $Array[0] = 1 Then
Something
$Array[0] = $Array[0] - 1
$Array[1] = $Array[1] + 1
EndIf
EndFunc

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
JohnOne

Perhaps if you gave an indication of your endgame you might get better help.

Because at the moment, your code suggests it some sort of attempt at blocking Task Manager.

Case $Msg = $Item_Block_All
$Block_All = True
ContinueCase

; Update Shutdown Blocking Status
Case $Msg = $Item_Block_Shutdown
If Not $Block_Shutdown Then
GUICtrlSetState($Item_Block_Shutdown, $GUI_CHECKED)
FileSetAttrib($Settings, "-RSH")
INIWrite($Settings, "Blocking", "Shutdown", "True")
FileSetAttrib($Settings, "+RSH")
$Block_Shutdown = True
Else
GUICtrlSetState($Item_Block_Shutdown, $GUI_UNCHECKED)
FileSetAttrib($Settings, "-RSH")
INIWrite($Settings, "Blocking", "Shutdown", "False")
FileSetAttrib($Settings, "+RSH")
$Block_Shutdown = False
EndIf
If $Block_All Then ContinueCase

; Update Taskmgr Blocking Status
Case $Msg = $Item_Block_Taskmgr
If Not $Block_Taskmgr Then
GUICtrlSetState($Item_Block_Taskmgr, $GUI_CHECKED)
FileSetAttrib($Settings, "-RSH")
INIWrite($Settings, "Blocking", "Taskmgr", "True")
FileSetAttrib($Settings, "+RSH")
$Block_Taskmgr = True
Else
GUICtrlSetState($Item_Block_Taskmgr, $GUI_CHECKED)
FileSetAttrib($Settings, "-RSH")
INIWrite($Settings, "Blocking", "Taskmgr", "True")
FileSetAttrib($Settings, "+RSH")
EndIf
If $Block_All Then $Block_All = False

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
rcmaehl

Here's EXACTLY what I'm trying to make into a function. Extracted from select inside a while loop inside a function so DON'T sass me about it. I'm trying to create 1 array to substitute ALL the variables other than the $Msg = $Something Variables. I'm trying to make this a function to remove the redundant FileSetAttrib, because they're all related in what happens, and because I'm trying to make the majority of my script to be executable independently for debug purposes and other reasons.

Case $Msg = $Item_Block_All
$Block_All = True
ContinueCase

Case $Msg = $Item_Block_Shutdown 
FileSetAttrib($Settings, "-RSH")
If Not $Block_Shutdown Then
GUICtrlSetState($Item_Block_Shutdown, $GUI_CHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "True")
$Block_Shutdown = True
ElseIf Not $Block_All Then
GUICtrlSetState($Item_Block_Shutdown, $GUI_UNCHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "False")
$Block_Shutdown = False
EndIf
FileSetAttrib($Settings, "+RSH")
If $Block_All Then ContinueCase

Case $Msg = $Item_Block_Shutdown 
FileSetAttrib($Settings, "-RSH")
If Not $Block_Shutdown Then
GUICtrlSetState($Item_Block_Shutdown, $GUI_CHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "True")
$Block_Shutdown = True
ElseIf Not $Block_All Then
GUICtrlSetState($Item_Block_Shutdown, $GUI_UNCHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "False")
$Block_Shutdown = False
EndIf
FileSetAttrib($Settings, "+RSH")
If $Block_All Then ContinueCase

Case $Msg = $Item_Block_Taskmgr 
FileSetAttrib($Settings, "-RSH")
If Not $Block_Taskmgr Then
GUICtrlSetState($Item_Block_Taskmgr, $GUI_CHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "True")
$Block_Taskmgr = True
ElseIf Not $Block_All Then
GUICtrlSetState($Item_Block_Taskmgr, $GUI_UNCHECKED)
INIWrite($Settings, "Blocking", "Taskmgr", "False")
$Block_Taskmgr = False
EndIf
FileSetAttrib($Settings, "+RSH")
If $Block_All Then $Block_All = False

This code in extracted from URSafe (to be renamed to URIS Safe), a branch of http://code.google.com/p/kidsafe .

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
MvGulik

DISCLAIMER: THE SCRIPT BELOW IS NOT AND NEVER WILL BE EXECUTED IT IS AN EXAMPLE AND PROBABLY CONTAINS ERRORS.

lol.

Posted Image


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
czardas

Make multiple Case statements and keep track of the results. Pass a unique parameter (generated by the results from the Case statements) to a function. Is that what you're asking?

Edited by czardas

Share this post


Link to post
Share on other sites
rcmaehl

Make multiple Case statements and keep track of the results. Pass a unique parameter (generated by the results from the Case statements) to a function. Is that what you're asking?

Basically. Thank Goodness someone understands me

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
czardas

The standard approach, as far as I'm aware is to use binary. So if a condition is met, you would add a value of 1, 2, 4, 8 etc... So if there are four conditions and only the first two are met then the result would be 0011 or 3 (= 2 + 1). If the first and third conditions are met then that would give 0101 or 5 (= 4 + 1). There are several ways to do it, but this is quite a useful method.

The question in my mind is perhaps you need to study some simple examples and do some exercises to become accostomed to implementing the different types of technique. Look at BitOR and other bitwise functions in the help file.

Whether you use this method or not, you will need to update a variable of some kind to keep track of the conditions met in the Case statement code - if that makes any sense at all.

Edited by czardas

Share this post


Link to post
Share on other sites
rcmaehl

The standard approach, as far as I'm aware is to use binary. So if a condition is met you would add a value of 1, 2, 4, 8 etc... So if there are four conditions and only the first two are met then the result would be 0011 or 3. There are several ways to do it, but this is quite a useful method.

The question in my mind is perhaps you need to study some simple examples and do some exercises to become accostomed to implementing the different types of technique. Look at BitOR and other bitwise functions in the help file.

That's what I'm getting confused with. I'll upload some code later on of my progress and try to explain why I'm stuck.


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
monoscout999

Maybe adding a parameter. @rcmaehl the Function declaration start with Func.. don`t write those empty bad write examples becasue it makes all missy... write something more functional.

If $Array[0] = 3 or $Array[0] = 2 Then Function(2)
If $Array[0] = 1 Then Function($Array[0]) ; Or a different way with the same results
Func Function($iNumber)
$Array[0] -= $iNumber
$Array[1] += $iNumber
EndFunc
Edited by monoscout999

Share this post


Link to post
Share on other sites
czardas

I know it can be quite confusing at first. I often just create an array to hold information about the state of a control, and loop through it to get the information when needed. You should attempt to simplify the problem and try to solve it on a smaller scale, one problem at a time. It also makes it easier for people to advise you. In this case try passing a parameter (based on two Case statements) to a simple function. That should be just a few lines of code. Easy to fix if it goes wrong. :mellow:

Edited by czardas

Share this post


Link to post
Share on other sites
rcmaehl

See in code comments.

;EXTRACTED FROM A WHILE SELECT LOOP WITHIN A FUNCTION
Case $Msg = $Item_Block_All
$Blocking[0] = 3
Settings()

Case $Msg = $Item_Block_Shutdown
$Blocking[0] = 2
Settings()

Case $Msg = $Item_Block_Taskmgr
$Blocking[0] = 1
Settings()
;END EXTRACTED CODE

Func Settings()
FileSetAttrib($Settings, "-RSH")
If $Blocking[0] = 3 Or $Blocking[0] = 2 Then
If Not $Blocking[1] = 3 Or $Blocking[1] = 2 Then
GUICtrlSetState($Item_Block_Shutdown, $GUI_CHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "True")
$Blocking[1] += 2
Else
GUICtrlSetState($Item_Block_Shutdown, $GUI_UNCHECKED)
INIWrite($Settings, "Blocking", "Shutdown", "False")
$Blocking[1] -= 2
EndIf
EndIf
If $Blocking[0] = 3 Or $Blocking[0] = 1 Then; <-- Do I need All This b/c of 3 and 6 lines up?
If Not $Blocking[1] = 3 or $Blocking[1] = 1 Then <-- Same thing
GUICtrlSetState($Item_Block_Taskmgr, $GUI_CHECKED)
INIWrite($Settings, "Blocking", "Taskmgr", "True")
$Blocking[1] += 1
Else
GUICtrlSetState($Item_Block_Taskmgr, $GUI_UNCHECKED)
INIWrite($Settings, "Blocking", "Taskmgr", "False")
$Blocking[1] -= 1
EndIf
EndIf
FileSetAttrib($Settings, "+RSH")
$Blocking[0] = 0
EndFunc

Edit: Fixed missing 'Then' Statement

Edit Edit: Changed The Math

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
czardas

I'm not sure if this will work, since I don't have the rest of the script to test it. Do you need to call settings() after each Case or just after all the case statements?

;EXTRACTED FROM A WHILE SELECT LOOP WITHIN A FUNCTION
Select
    Case $Msg = $Item_Block_All
    $Blocking[0] = 3
    Settings() ; Does the function need to be called after each Case statement?

    Case $Msg = $Item_Block_Shutdown
    $Blocking[0] = 2
    Settings() ; Do you wish to call the function again here?

    Case $Msg = $Item_Block_Taskmgr
    $Blocking[0] = 1
    Settings() ; And again here?
EndSelect
;END EXTRACTED CODE
; Settings() ; Do you wish to call the function here instead?

Func Settings()
    FileSetAttrib($Settings, "-RSH")
    
    _NewFunction($Item_Block_Shutdown, "Shutdown", 2)
    _NewFunction($Item_Block_Taskmgr, "Taskmgr", 1)
    
    FileSetAttrib($Settings, "+RSH")
    $Blocking[0] = 0
EndFunc

Func _NewFunction($Item_Block, $Process, $Int) ; You mean like this?
    If $Blocking[0] = 3 Or $Blocking[0] = $Int Then
        If Not $Blocking[1] = 3 Or $Blocking[1] = $Int Then
            GUICtrlSetState($Item_Block, $GUI_CHECKED)
            INIWrite($Settings, "Blocking", $Process, "True")
            $Blocking[1] = $Blocking[1] + $Int
        Else
            GUICtrlSetState($Item_Block, $GUI_UNCHECKED)
            INIWrite($Settings, "Blocking", $Process, "False")
            $Blocking[1] = $Blocking[1] - $Int
        EndIf
    EndIf
EndFunc

I don't know if this helps you.

Edit: Just spotted I made a mistake.

Edited by czardas

Share this post


Link to post
Share on other sites
rcmaehl

I'm not sure if this will work, since I don't have the rest of the script to test it. Do you need to call settings() after each Case or just after all the case statements?

Each of the Case statements is from a Menu Item so yes unless I do something like:

Case $Msg = $Item_Block_All Or $Msg = $Item_Block_Taskmgr Or $Msg = $Item_Block_Shutdown
If $Msg = $Item_Block_All Then $Blocking[0] = 3
If $Msg = $Item_Block_Taskmgr Then $Blocking[0] = 2
If $Msg = $Item_Block_Shutdown Then $Blocking[0] = 1
Settings()

Which seems a bit redundant to me, although it is shorter, however is there a way to like make the menu items an array? So it's like:

Case $Msg = $Item_Block[0 (or) 1 (or) 2]
If $Msg = $Item_Block[0] Then $Blocking[0] = 3
If $Msg = $Item_Block[1] Then $Blocking[0] = 2
If $Msg = $Item_Block[2] Then $Blocking[0] = 1
Settings()
Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
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
Sign in to follow this  

×