Defined[] with arrays Behavior change, intentional?

A forum for reporting bugs NOT related to custom plugins.

Moderator: MacroQuest Developers

devestator
a ghoul
a ghoul
Posts: 121
Joined: Thu Feb 27, 2003 4:25 pm

Defined[] with arrays Behavior change, intentional?

Post by devestator » Sat Sep 16, 2017 9:55 pm

I didn't see this mentioned in any of the patch notes so I'm not sure if this was an intentional change or a side effect of the undeclared variable changes.

It deals with Defined[] and arrays.

Before the undeclared variables patch you could do something like this:

Code: Select all

sub main
  /declare myArray[20] string local NULL

  /echo ${Defined[myArray[1]]}
/return
And it would echo TRUE. Now it echoes FALSE and you have to do this instead:

Code: Select all

sub main
  /declare myArray[20] string local NULL

  /echo ${Defined[myArray]}
/return
While the change is simple enough in an example like this, where it becomes a problem is if you are storing the variable name in a variable.

For a more specific example, I have a function that retrieves settings from an INI, automatically declares variables if the arguments passed tell it to, and things like that. So, as part of that, it passes in the variable name to store the INI setting into. When I'm loading arrays it would pass in the variable name as myArray[1] so that it does the /varset, it sets the value to myArray[1].

Example:

Code: Select all

sub main
  /declare iniName string outer myini.ini
  /declare myArray[20] string outer NULL

  /for i 1 to 20
    /call iniLoad General Setting${i} myArray[${i}]
  /next i
/return

sub iniLoad(iniSection, iniKey, variableName)
  /declare iniValue string local

  /if (${Defined[${variableName}]}) {
    /varset ${variableName} ${Ini[${iniName},${iniSection},${iniKey},NULL]}
  }
/return
This of course isn't my exact code but it shows a use case. My ini load function is a bit more complicated because it can automatically create the setting in the INI if it doesn't exist, create it with a specified default, and things like that. And like I said, it also has the ability to declare variables that aren't defined, which is why with this patch I didn't have a problem in my macros with a bunch of undeclared variables (I did have problems with using ${Defined[]} on a lot of fucntion arguments that I had to make a ton of changes for but that is a different story). Something like this is really useful when loading multiple similar settings like a list of buffs and such.

Anyway, I got around it for now by defining another string and parsing out the array name using ${variableName.Left[${Math.Calc[${variableName.Find[[]} - 1]}]}. I'm just not sure if it's an unintended bug that someone might want to be aware of.



Also, this probably isn't the right place to ask this, but while I'm posting I'm also wondering if it would be possible since all these changes were made, to have an optional default value for function arguments. So that instead of everything defaulting to NULL if it's not defined, you can specify it's default value if it's not passed in.

Example:

Code: Select all

sub myFunction( bool myBoolean TRUE, string myString SOME VALUE )
/return
It would be really nice, as that is the main problem I ran into with the recent change. I used to use Defined[] checks to declare my arguments locally if they weren't defined and set my default values. Now I have to check if they are == NULL if they are a number or boolean, or .Equal[NULL] if they are a string. I mean, it's fine I've made the changes at this point, but I still think it would be a good change to be able to define default values in the function definition.

EqMule
Developer
Developer
Posts: 2697
Joined: Fri Jan 03, 2003 9:57 pm
Contact:

Re: Defined[] with arrays Behavior change, intentional?

Post by EqMule » Sat Sep 16, 2017 11:26 pm

I don't know if it's intentional.

I like the suggestion with default values.

I'll look into this a bit more before I have a better answer for the first question.
My status o/
If you like MQ2 and would like to contribute, please do. My goal is 25 donations per month.
So far I've received Image donations for this month's patches.

Bitcoin: 1Aq8ackjQ4f7AUvbUL7BE6oPfT8PmNP4Zq
Krono: PM me.
I can always use characters for testing, PM me if you can donate one.

JudgeD
a snow griffon
a snow griffon
Posts: 354
Joined: Sat Aug 18, 2012 8:07 pm

Re: Defined[] with arrays Behavior change, intentional?

Post by JudgeD » Sun Sep 17, 2017 6:51 pm

Thanks for posting this Devestator, this is something I do a lot as well.

Vayeco Dynn
a ghoul
a ghoul
Posts: 113
Joined: Wed Aug 25, 2004 3:00 am

Re: Defined[] with arrays Behavior change, intentional?

Post by Vayeco Dynn » Thu Sep 28, 2017 1:03 am

Defined is also incorrectly returning TRUE for Sub parameters which haven't been set.

Tribunalite
decaying skeleton
decaying skeleton
Posts: 3
Joined: Mon Nov 20, 2006 5:22 pm

Re: Defined[] with arrays Behavior change, intentional?

Post by Tribunalite » Mon Jul 08, 2019 2:24 am

I'm having this issue with an old include (AAPurchase.inc), spams that the name has been declared.

Code: Select all

| If AACount is not 0
   /if (${AACount}) {
| Create the AA array
      /declare AA[${AACount}] string outer
| Start for/next loop.
      /for a 1 to ${AACount}
| Load the AAs into the array.
         /call LoadINIVar ${IniSection} AA${a} " " AA[${a}] ${AAIniFile}
| End for/next loop.
      /next a
   }