viewpoint-particle

Author Topic: Pass variable from UDF to main script  (Read 125 times)

jmburton2001

  • Jr. Member
  • **
  • Posts: 59
Pass variable from UDF to main script
« on: October 24, 2019, 08:00:12 am »
I've been searching for an answer to this but the items I've read seem to say it's not possible, but my gut tells me there has to be a way.

An example that I'm working with was posted by Deana here https://forum.winbatch.com/index.php?topic=871.msg3511#msg3511

The section that I'm looking at is this:

Code: Winbatch
     case MSG_RADIOPUSHED  ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        radiovalue = Int(Example_Name)
        Pause('radiovalue',radiovalue)
        return(RET_DO_DEFAULT)
 

My question is, can the variable "radiovalue" (or its result) be passed out of the function into a variable outside the function to be used in the main script?

JTaylor

  • Pundit
  • *****
  • Posts: 1033
    • Data & Stuff Inc.
Re: Pass variable from UDF to main script
« Reply #1 on: October 24, 2019, 08:30:15 am »
One way would be to make the Callback a SubRoutine rather than a function (make sure you are not stepping on other variables with the change).   Another would be to use a global variable.

Jim

jmburton2001

  • Jr. Member
  • **
  • Posts: 59
Re: Pass variable from UDF to main script
« Reply #2 on: October 24, 2019, 08:32:36 am »
Thank you JTaylor! I needed a nudge in the right direction!

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 275
Re: Pass variable from UDF to main script
« Reply #3 on: October 25, 2019, 01:07:13 am »
I'm not sure what Jim is referring to with the phrase "global variable", since there really aren't any such things in WinBatch.

I'm also surprised that no one has mentioned pointers so far in this thread.  Pointers are, I believe, the currently accepted method of accomplishing the concept of "global variables" in WinBatch.

Personally, I don't "get" pointers.  I've looked at them several times; never quite got the point.  (Not saying I don't understand how they work; just don't get the reason to bother).  Instead, I use the "xglob" extender, which gives (again) the concept of "global variables" in WinBatch.

JTaylor

  • Pundit
  • *****
  • Posts: 1033
    • Data & Stuff Inc.
Re: Pass variable from UDF to main script
« Reply #4 on: October 25, 2019, 06:56:14 am »
Sorry.   Just took the phrase from the Help file.   Happy for you to correct things so the person needing help won't be misled.

Jim

jmburton2001

  • Jr. Member
  • **
  • Posts: 59
Re: Pass variable from UDF to main script
« Reply #5 on: October 25, 2019, 09:21:22 am »
I've downloaded the "xglob" extender and will take a look at it when I get time. I've been analyzing the following snippet so I can better understand the global "pointer" function.

Code: Winbatch
#DefineFunction MyTest()
MyTest2 = PtrGlobal(MyTest2)
*MyTest2 = "Apples"
#EndFunction

PtrGlobalDefine(MyTest2)
MyTest1 = "Cheese Fries"
retvalue = MyTest()
Message ("MyTest1",MyTest1)
Message ("MyTest2",MyTest2)
Exit

At this point I've entered a rabbit hole labyrinth that has taken me far, far away from my original dilemma...

I have a dialog with four radio buttons. I want to return the value of a selected radio button to the dialog in real time.

Here's a mockup (because a picture is worth a thousand words). https://i.imgur.com/0NbIdeg.png

Thank you both for all your suggestions!

JTaylor

  • Pundit
  • *****
  • Posts: 1033
    • Data & Stuff Inc.
Re: Pass variable from UDF to main script
« Reply #6 on: October 25, 2019, 06:17:01 pm »
Hmmmmmm....not overly familiar with the xglob extender.   Unless I am missing something, which is entirely possible, I think you can do this without an extender. 

I normally just make my Callbacks SubRoutines and then I don't have to worry about this issue.


Jim

JTaylor

  • Pundit
  • *****
  • Posts: 1033
    • Data & Stuff Inc.
Re: Pass variable from UDF to main script
« Reply #7 on: October 25, 2019, 06:30:46 pm »
Please clarify...originally it sounded like you wanted to pass a variable outside the dialog function.   The last post makes it sound like you want to use that value within the dialog.    For the first scenario what I suggested with using a SubRoutine rather than a Function would work or the Pointer Functions.   

Jim

jmburton2001

  • Jr. Member
  • **
  • Posts: 59
Re: Pass variable from UDF to main script
« Reply #8 on: October 26, 2019, 07:21:04 am »
Please bear in mind that I'm a hobbyist. Therefore my scripts tend to take me in directions that I never intended and usually way outside my initial requirements. Hence the use of the phrase rabbit hole labyrinth.

To put it simply, I want to be able to have a user click on a radio button and return that radio button's description back to the dialog in real time. I modified Deana's script to give me that description and the "Pause" message returns the description for "MyType" within the function without fail. I was trying to figure out a way to take "MyType" out of the function and return it to the dialog as a variable.

Code: Winbatch
     case MSG_RADIOPUSHED  ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        radiovalue = Int(Example_Name)
        If radiovalue == "1" Then *MyType = "Blues"
        If radiovalue == "2" Then *MyType = "Jazz"
        If radiovalue == "3" Then *MyType = "Rock"
        Pause('radiovalue',MyType)
        return(RET_DO_DEFAULT)

In my mockup the yellow background with red lettering would be either a static or variable text field. When a user selects a particular radio button, it would return the description to the main script (as a variable) to update the text field.

So if a user selected radiovalue 1 (Blues) it would populate the text field within the dialog with the word (%MyType%) "Blues". I just need to figure out how to pass "MyType" out of the function and back into the dialog.

EDIT: At this point it's purely academic since I've abandoned that in my current project although it's something I've wanted to do in other projects.

JTaylor

  • Pundit
  • *****
  • Posts: 1033
    • Data & Stuff Inc.
Re: Pass variable from UDF to main script
« Reply #9 on: October 26, 2019, 08:47:37 am »
Okay.   Your wording is hindering an answer as what you really want is different than it first appeared.  The part that was throwing me off was talking about passing the value out of the function.   Assuming I finally understand, you do not need to pass this out of any function but simply need to update the dialog when a radiobutton is pushed.  You do not need to use global variables or change to a SubRoutine.  Simply add DialogControlSet() to the script.  Change parameters as needed.

        radiovalue = Int(Example_Name)
        If radiovalue == "1" Then MyType = "Blues"
        If radiovalue == "2" Then MyType = "Jazz"
        If radiovalue == "3" Then MyType = "Rock"

        DialogControlSet( dialog-handle, control-name, @dcTitle, MyType )

        Pause('radiovalue',MyType)



Jim

jmburton2001

  • Jr. Member
  • **
  • Posts: 59
Re: Pass variable from UDF to main script
« Reply #10 on: October 27, 2019, 09:28:46 am »
Assuming I finally understand, you do not need to pass this out of any function but simply need to update the dialog when a radiobutton is pushed.

I think the underlined part sums it up quite eloquently. My lack of understanding of various WinBatch functions usually leads me down a path in the wrong direction. By the time I ask for assistance, I've already gone into territory far from my original dilemma. I guess I should have asked that question first!

I apologize for not being clearer in my initial request and I sincerely appreciate you taking the time to lend a helping hand! I'll probably revisit this in the future when I get the time.

Thanks again and please enjoy the rest of your weekend!