Jump to content

How to make a UDF?


Recommended Posts

I thought it was just simple, you create a script with a function and etc. and then you create another script that calls on that function and you get the information back.

For example:

TestUDF.au3

Func _Test1()
  $Var = "String"
  $VarStripped = StringTrimLeft($Var, 1)
EndFunc

TestingUDF.au3

#include <TestUDF.au3>

HotKeySet("{1}", "_TestRun")

Func _TestRun()
  _Test1()
  MsgBox(0, "Test", $VarStripped)
EndFunc

But obviously not. I was just wondering on the proper way of making a UDF, but I could not find any documentation on it.

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

That's the right principal but in all liklihood the call is incorrect.

First a UDF should have the Line

#Include-Once

at the top of it so you don't inadvertantly include it in another UDF and then call both.

Secondly, read the help file about #include. Calling it with <filename.au3> makes AutoIt look in AutoIt's include folder. You probably wanted to call it with a specific path so it should have been

#include "path\filename.au3"

That is a generalization because there is a registry hack that will tell AutoIt to also look in other specific folders when using the <> method.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

Damein,

You need to declare $VarStripped as a Global variable so that all the functions in your script can see it. At the moment the 2 functions each see only their Local versions of the variable which is destroyed when the function ends. ;)

Your current script showing why it does not work:

; #include <TestUDF.au3> ; This is expanded so AutoIt sees the following lines
Func _Test1()
  $Var = "String"                        ; This variable only exists within this function
  $VarStripped = StringTrimLeft($Var, 1) ; As does this one - they are both destroyed when the function exits
EndFunc

HotKeySet("{1}", "_TestRun")

Func _TestRun()
  _Test1()
  MsgBox(0, "Test", $VarStripped) ; This variable again only exists within this function
EndFunc

I even get an error if I try to run this code telling me that the $VarStripped variable in the MsgBox line is undeclared. :D

You just need to add Global $VarStripped at the top of your script so that all your functions can see the variable. :unsure:

Take a look at the Variables - using Global, Local and ByRef tutorial in the Wiki for a more detailed explanation of Global and Local. ;)

M23

P.S. You do not need the {} in your HotKey declaration either! :>

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

For your example a better way would be.

TestUdf.au3

#include-once
Func _Test1($var)
  $VarStripped = StringTrimLeft($Var, 1)
    Return $VarStripped
EndFunc

Used like this

#include <TestUDF.au3>

HotKeySet("{1}", "_TestRun")
While 1

WEnd
Func _TestRun()
   $var = "String"
   $VarStripped =_Test1($var)
    MsgBox(0, "Test", $VarStripped)
 EndFunc

or alternatively like this

#include <TestUDF.au3>

HotKeySet("{1}", "_TestRun")
While 1

WEnd
Func _TestRun()
  MsgBox(0, "Test", _Test1("String"))
EndFunc

Edit: Fixed AutoIt tags

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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...