The if command

Posted by Val on Sat 19 Mar 2011 03:53 PM — 8 posts, 28,563 views.

#0
Hi

I hope this is a quick one

in an alias I would like options on casting a spell with eiter of these two variables @target (preset with "me" or "not me" depending on the spell) and @target2 (a name I entered when I use "t *")

so if @target = me then cast @spellname at @target1

but if @target = not me then cast @spellname at @target2

Immediate execution
[string "Alias: "]:6: 'then' expected near '='


local target = GetVariable("target")
local target2 = GetVariable("target2")

local x = (".. target")

if x = ("not me") then

Send "cast @spellname at @target2"

if x = ("me") then

Send "cast @spellname at @target"

Thanks
Val
USA Global Moderator #1
Quote:
if x = ("not me") then

= is for assignment
for comparison you want ==
#2
Thanks for that, I managed to take it a stage further but now I'm stuck again on something I know you will see as basic

currently I have the following -

local spellname = GetVariable("spellname1")
local targetme = GetVariable("targetme")
local target2 = GetVariable ("target2")

if x == "me" then Send ("cast " .. spellname, " on " .. targetme)

end


if x == "notme" then Send ("cast " .. spellname, " on " .. target2)
end

if x not == "notme" or "me" then Send ("cast " .. class, " " .. spellname, " on " .. target2)

end

The top part works, last line keeps throwing an error saying its expecting "then" near "not", so I gather it does not like the "or", help please?

Thank you
Val
#3
Either this:


if x == "notme" then Send ("cast " .. spellname, " on " .. target2)
end

if x != "notme" or "me" then Send ("cast " .. class, " " .. spellname, " on " .. target2)
end


This:


if x == "notme" then
Send ("cast " .. spellname, " on " .. target2)
else
Send ("cast " .. class, " " .. spellname, " on " .. target2)
end


Or this, if you need more flexibility later:


Commands = {}
Commands["notme"] = "cast " .. spellname .. " on " .. target2
Commands.Default = "cast " .. class .. " " .. spellname .. " on " .. target2

tosend = Commands[x]
--this line sets your command to a default if it's something not already in Commands
if not tosend then tosend = Commands.Default end

Send(tosend)
#4
Thanks

I went for
if x == "me" then
Send ("cast " .. spellname, " on " .. target2)
else
Send ("cast " .. spellname, " on " .. target2)
end

I tried the != "notme" or "me" then Send ///

But I get n error, its looking for

17: 'then' expected near '!

If you can let me know what that might be it would help in the future.

the third one with the commands I did not really get yet, I'm still learning so I left it

Thanks for your help
Val
Australia Forum Administrator #5
I don't think Manacle tested this bit (hey, I don't test all my posts either!) ...


if x != "notme" or "me" then Send ("cast " .. class, " " .. spellname, " on " .. target2)
end


For a start, break it into lines to look neater. Second "not equal" is ~= not !=. (The != form is from C which trips up C programmers).

Then you really want to re-test x, just saying 'or "me"' will have a different effect.

And you seem to be mixing up commas and concatenation. For neatness, use one or the other.

So in total:


if x ~= "notme" and x ~= "me" then 
   Send ("cast ", class, " ", spellname, " on ", target2)
end


So this spell is cast if x is neither "me" nor "notme" if that is what you intended. But if not, I prefer the else. If you are doing this *or* that, then use if/else.

#6
Nick Gammon said:

I don't think Manacle tested this bit (hey, I don't test all my posts either!) ...


if x != "notme" or "me" then Send ("cast " .. class, " " .. spellname, " on " .. target2)
end


For a start, break it into lines to look neater. Second "not equal" is ~= not !=. (The != form is from C which trips up C programmers).



I think half of the effort I spend on Lua plugins is translation. I might add that another ten percent is lamenting the loss of my ternary operator. Tasty, tasty ternary...
#7
Hi

Thanks for the explination, and thanks to both of you for your help, the method i'm using seems to be doing the trick

Val