Plazmic: Problem with $defined(@varname)?

A forum for feature requests/discussions and user submitted patches that improve MQ2

Moderator: MacroQuest Developers

wassup
Official Guardian and Writer of TFM
Official Guardian and Writer of TFM
Posts: 1487
Joined: Sat Oct 26, 2002 5:15 pm

Plazmic: Problem with $defined(@varname)?

Post by wassup » Wed Oct 08, 2003 5:31 pm

I was testing $defined but when I tested this code:

Code: Select all

Sub Main
   /zapvars
   /declare TestVar global
   /echo TestVar defined $defined(@TestVar)
   
   /varset TestVar 15
   /echo TestVar defined $defined(@TestVar)
/return
With this output:

Code: Select all

[color=cyan]
[Wed Oct 08 17:20:38 2003] Cleared the following: Timers Vars Arrays
[Wed Oct 08 17:20:38 2003] [MacroQuest] TestVar defined FALSE
[Wed Oct 08 17:20:38 2003] [MacroQuest] TestVar defined FALSE
[Wed Oct 08 17:20:38 2003] Cleared the following: Timers Vars Arrays
[Wed Oct 08 17:20:38 2003] The current macro has ended.
[/color]

I then changed the macro to:

Code: Select all

Sub Main
   /zapvars
   /declare TestVar global
   /echo TestVar defined $defined(TestVar)
   
   /varset TestVar 15
   /echo TestVar defined $defined(TestVar)
/return

with this as the output:

Code: Select all

[color=cyan]
[Wed Oct 08 17:27:16 2003] Cleared the following: Timers Vars Arrays
[Wed Oct 08 17:27:16 2003] [MacroQuest] TestVar defined TRUE
[Wed Oct 08 17:27:16 2003] [MacroQuest] TestVar defined TRUE
[Wed Oct 08 17:27:16 2003] Cleared the following: Timers Vars Arrays
[Wed Oct 08 17:27:16 2003] The current macro has ended.
[/color]
Any idea what I might be doing wrong? Or is $defined(@varname) not working? I tried it with array, array2, and timer. None work.

Jaerin
Developer
Developer
Posts: 133
Joined: Mon Mar 10, 2003 7:37 pm
Contact:

Post by Jaerin » Wed Oct 08, 2003 5:42 pm

As far as I know it should be $defined(VarName).

This would essentially see if the variable was defined. $defined(@VarName) would just tell you if there is a value in the variable not necessarily if it's defined.

Jaerin

wassup
Official Guardian and Writer of TFM
Official Guardian and Writer of TFM
Posts: 1487
Joined: Sat Oct 26, 2002 5:15 pm

Post by wassup » Wed Oct 08, 2003 6:06 pm

If you look at the code I do a test on the variable before and after the variable is set to a value.

Two different macro's were run, one with $defined(@VarName) and one with $defined(VarName)

First macro gave FALSE for before and after.

Second macro gave TRUE before and after.

Unless you mean $defined(VarName) is just used to test if the Variable was declared, not testing to see if it holds a value.

When I used a macro with /varset VarName 15, and then used $defined(@VarName), FALSE was returned.

User avatar
dont_know_at_all
Developer
Developer
Posts: 5450
Joined: Sun Dec 01, 2002 4:15 am
Location: Florida, USA
Contact:

Post by dont_know_at_all » Wed Oct 08, 2003 6:30 pm

Wassup wrote:When I used a macro with /varset VarName 15, and then used $defined(@VarName), FALSE was returned.

Code: Select all

/varset VarName 15
/echo $defined(@VarName)
How is this handled by the parser, you ask? The @VarName is changed to its value by the parser *before* the $defined operator is run. So, effectively you have done this

Code: Select all

/echo $defined(15)
which should return false.

How would you evaluate this?

Code: Select all

/varset VarName Param0
/echo $defined(@VarName)
Now, wrap your brain around Plaz's test macro:

Code: Select all

#event Test "custom test"
#event Test2 "custom test"

Sub Main(TestParam)
   /echo UserVarsTest - Start
   /echo


   /echo TEST: Parameters - Start
   /if $defined(TestParam)==FALSE {
      /echo Please pass in 2 parameters
      /return
   }
   /if $defined(Param1)==FALSE {
      /echo Please pass in 2 parameters
      /return
   }
   /echo Two parameters passed in: '@TestParam' and '@Param1'
   /echo TEST: Parameters - End

   /echo

   /echo TEST: Case Sensitivity - Start
   /declare MyGlobal global
   /varset MyGlobal gtest
   /echo $defined(MyGlobal) should be TRUE
   /echo $defined(Myglobal) should be FALSE
   /echo '@MyGlobal' should be 'gtest'
   /echo '@Myglobal' should be '²Myglobal'
   /echo TEST: Case Sensitivity - End

   /echo

   /echo TEST: Dereference - Start
   /declare Loc1 local
   /declare Loc2 local
   /declare Loc3 local
   /declare Loc4 local
   /varset Loc1 Loc2
   /varset Loc2 Loc3
   /varset Loc3 Loc4
   /varset Loc4 MyGlobal
   /echo '@Loc1' should be 'Loc2'
   /echo '@@Loc1' should be 'Loc3'
   /echo '@@@Loc1' should be 'Loc4'
   /echo '@@@@Loc1' should be 'MyGlobal'
   /echo '@@@@@Loc1' should be '@MyGlobal'
   /echo TEST: Dereference - End

   /echo

   /echo TEST: For 1 to 5 - Start
   /declare MyLoc local
   /varset Loc1 ""
   /for MyLoc 1 to 5
      /varset Loc1 "@Loc1 @MyLoc"
   /next MyLoc
   /echo '@Loc1' should be ' 1 2 3 4 5'
   /echo TEST: For 1 to 5 - End

   /echo

   /echo TEST: /var... functions - Start
   /echo MyLoc: '@MyLoc'

   /varsub MyLoc 2
   /echo MyLoc-2: '@MyLoc'

   /varadd MyLoc 3
   /echo MyLoc+3: '@MyLoc'

   /varcalc MyLoc @MyLoc*10
   /echo MyLoc*10: '@MyLoc'
   /echo TEST: /var... functions - End

   /echo

   /echo TEST: 1D Array - Start
   /declare MyArray array
   /varset MyArray(2) test1d
   /echo '@MyArray(2)' should be 'test1d'
   /echo TEST: 1D Array - End

   /echo

   /echo TEST: 2D Array - Start
   /declare TwoArray array2
   /varset TwoArray(5,2) test2d
   /echo '@TwoArray(5,2)' should be 'test2d'
   /echo TEST: 2D Array - END


   /echo TEST: Sub Named Parameters - Start
   /if $defined(blahblah)==TRUE /echo blahblah should not be defined!
   /call paramtest @blahblah lala @MyGlobal
   /echo TEST: Sub Named Parameters - End

   /echo

   /echo TEST: Timer - Start
   /declare MyTimer timer
   /varset MyTimer 1s
   /varset MyGlobal 0
   :Loop
      /doevents
   /if n @MyGlobal==0 /goto :Loop
   /echo TEST: Timer - End

   /echo

   /echo TEST: Custom events - Start
   /echo Trigger: custom test
   /doevents
   /echo 2 events should have fired
   /echo TEST: Custom events - End

   /echo
   /echo UserVarsTest - End

/return

Sub paramtest(blahblah,MyTimer,LastVar)
   /echo Sanity: '@LastVar' should be '@MyGlobal'
   /echo Resursive: '@blahblah' should be '²blahblah'
   /echo Scope masking: '@MyTimer' should be 'lala'
/return

Sub Event_Timer(Timer,Original)
   /echo @Timer fired
   /varset MyGlobal 1
/return

Sub Event_Test
   /echo this event test has no params defined
/return

Sub Event_Test2(Message)
   /echo this event test did have params defined.
/return


MacroFiend
a grimling bloodguard
a grimling bloodguard
Posts: 662
Joined: Mon Jul 28, 2003 2:47 am

Post by MacroFiend » Wed Oct 08, 2003 7:13 pm

/boggle

I really liked the dereference test. Dereferencing your way through a half dozen variables looks kinda useful.

wassup
Official Guardian and Writer of TFM
Official Guardian and Writer of TFM
Posts: 1487
Joined: Sat Oct 26, 2002 5:15 pm

Post by wassup » Wed Oct 08, 2003 7:39 pm

So $defined is most useful for determining if parameters were passed and for determining if a specific variable was declared?

That dereference test was quite interesting...

User avatar
dont_know_at_all
Developer
Developer
Posts: 5450
Joined: Sun Dec 01, 2002 4:15 am
Location: Florida, USA
Contact:

Post by dont_know_at_all » Wed Oct 08, 2003 8:52 pm

Wassup wrote:So $defined is most useful for determining if parameters were passed and for determining if a specific variable was declared?
Correct.

Plazmic
The One
The One
Posts: 800
Joined: Fri Jun 14, 2002 12:31 am
Contact:

Post by Plazmic » Wed Oct 08, 2003 8:58 pm

If you know a variable is declared and want to see if a value was set yet, compare it against "UNDEFINED-GLOBAL", "UNDEFINED-LOCAL" or "UNDEFINED-ARRAY-ELEMENT" (depending on the type)
- Plazmic