Problem with "Return"

Discussion of Inner Space

Moderators: Lavish Software Team, Moderators

Post Reply
Trickster
Non-Subscriber
Posts: 9
Joined: Tue Feb 08, 2005 6:28 pm

Problem with "Return"

Post by Trickster » Tue Mar 01, 2005 2:59 am

Anyone having problems with the Return command being cranky?

Code: Select all

...
echo "Current Distance 1: " ${Math.Distance[${Me.X},${Me.Y},${Target.X},${Target.Y}]}
call GetCurrentDistanceToTarget
echo "Return: " ${Float[${Return}]}
varset CurrentDistance ${Float[${Return}]}
echo "Current Distance 2: " ${CurrentDistance}
...
function GetCurrentDistanceToTarget()
{
	declare distanceCalc float local
	varset distanceCalc ${Math.Distance[${Me.X},${Me.Y},${Target.X},${Target.Y}]}
	echo "Current Distance: " ${distanceCalc}
	return ${distanceCalc}
}
Output is along the lines of:

Code: Select all

Current Distance 1:  45.63
Variable 'distanceCalc' creation successful
Current Distance:  45.63
Return:  0.00
Current Distance 2:  0.00

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 3:05 am

That could also be the Float TLO being cranky. When using echo and varset, you should not use ${Float[]} around the return value, because it's just being converted to a float and then right back to a string, which both of those use.

This would do exactly the same thing, but with less twists and turns

Code: Select all

echo Return: ${Return}
varset CurrentDistance ${Return}
Anyhow, I'll check both of those.

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 3:38 am

Okay, ${Return} wasnt working at all.

0.76 is up now, and fixes this issue. Restart IS and let it patch, and you should be set

Trickster
Non-Subscriber
Posts: 9
Joined: Tue Feb 08, 2005 6:28 pm

Post by Trickster » Tue Mar 01, 2005 5:35 am

Now that I've patched, NULL seems to equal TRUE.

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 12:40 pm

In what context? Dont tell me you're using ${Bool[x]} too ;)

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 1:35 pm

Okay, I fixed the Bool top-level object.

However, I want you to be aware that you almost never want to use the Bool, Float, Int, or String Top-Level Objects. Usually you only want to if you need different text than you started with, or need to access a member or method of the bool, float, int, or string types without already having one.

For example, if you have the text "NULL" and you use Bool, you're going to get "TRUE" or "FALSE" FALSE and NULL are both effectively 0.00. Used anywhere that uses a formula, they are one and the same (e.g. ${Math.Calc[FALSE+NULL+1]} will be 1 and is perfectly valid). Likewise, ${Float[0.00]} simply gets you the text "0.00". The only time you want to use that is if you need to use one of its members, for example converting 1.234 to 1.2 can be done using ${Float[1.234].Deci}, but if I just wanted 1.234, going through Float is just adding to the confusion.

Similarly, many people use the String TLO where it's not appropriate. For example, around something that's already a string, such as Return. ${String[${Return}]} is the same thing as ${Return}.

Note: I'm not trying to tell you that you should have known this, I'm just explaining it to you so that you will know it :)

Trickster
Non-Subscriber
Posts: 9
Joined: Tue Feb 08, 2005 6:28 pm

Post by Trickster » Tue Mar 01, 2005 2:17 pm

Code: Select all

echo "Target.Name: " ${Target.Name}
echo "Bool: " ${Bool[${Target.Name}]}
echo "Equal: " ${Target.Name.Equal[NULL]}

Without a target I'd get:
Target.Name: NULL
Bool: FALSE
Equal: NULL

With a target:
Target.Name:  Prairie Stalker
Bool: FALSE
Equal: FALSE
Regardless of whether I had a target or not, I'd always go down the FALSE path.

I eventually found that the following worked fine, but I didn't want to go to the WoWScript TLO to do it.

Code: Select all

if ${WoWScript[UnitExists("target")]}

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 2:23 pm

The quickest way might actually be to use NotEqual (theres a lot of examples of this in MQ2 macros, which although are not LavishScript, are the precursor). The problem is that if you have no target, Target itself is NULL, and processing ends there. So you want to know if the processing continues. There's two ways of doing this:

1. ${Target.Name.NotEqual[NULL]} -- will be NULL if you have no target, or TRUE if you have a target
2. ${Target(bool)} - should be NULL if you have no target, or TRUE if you do

Either are perfectly acceptable. The second method is "best", though most people do not understand "type casting" and you generally don't want to use it other than the above method.

Trickster
Non-Subscriber
Posts: 9
Joined: Tue Feb 08, 2005 6:28 pm

Post by Trickster » Tue Mar 01, 2005 3:02 pm

Code: Select all

echo ${Target(bool)}

Without Target:
NULL

With Target:
Unknown  type 'bool)t'
NULL
${Target.Name.NotEqual[NULL]} got predictable results

Here's another fun one:

Code: Select all

if 1 > 2
echo "1 greater 2"
if 2 > 1
echo "2 greater 1"
I get both echoes.

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 3:10 pm

< and > have special meaning in LavishScript commands. If you want to use them in a condition that's not inside an index (which are only inside ${} and the command name in certain cases), you need to use quotes.

if "1>2"
if "2<1"

I'll check the bug with type casting

Trickster
Non-Subscriber
Posts: 9
Joined: Tue Feb 08, 2005 6:28 pm

Post by Trickster » Tue Mar 01, 2005 3:11 pm

Yeah, just saw that on another thread. That deserves a specific mention in the Wiki if it's not already there, I'll go put something in there in a bit.

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 3:20 pm

Fixed type casting. ${Target(bool)} should now work as described. Restart IS and let it patch ;)

Thanks for the reports by the way, 3 bugs fixed as a result of this thread so far

Lax
Owner
Posts: 6634
Joined: Fri Jun 18, 2004 6:08 pm

Post by Lax » Tue Mar 01, 2005 4:29 pm

Also thanks for adding to the wiki, I expanded on the If article a bit as well :)

Post Reply