Page 1 of 1
cant't use "else"
Posted: Wed Nov 06, 2013 3:56 am
by Baerxu
Hey there,
I want to make bot which will choose his equip from quests depending on his class.
The first pieces of equipment are obtained in from a armorbag and a weaponbag. So I let my bot open those bags. Now it shall check which class the bot's primary class is because the equipment u get from those bags are class-dependant, e.g. a mage gets a wand, a rouge gets a dagger.
Code: Select all
inventory:update();
inventory:useItem(201704); -- Waffen-Paket
inventory:useItem(201705); -- Ruestungspaket
if (player.Class1 == CLASS_SCOUT or player.Class1 == CLASS_ROGUE) then
inventory:useItem(210512);
inventory:useItem(221582);
else if (player.Class1 == CLASS_MAGE or player.Class1 == CLASS_PRIEST) then
inventory:useItem(210514);
inventory:useItem(221584);
else
inventory:useItem(210511);
inventory:useItem(221582);
end;
inventory:useItem(209577); -- Geheimnisvolle Runenschriftrolle
Alright, the problem with this is: it doesn't work. Well acutally it does work but the 'else' ruins it. Let me explain:
Whenever I try to run this mm keeps telling my I got a compilationerror and I should check the LUA code there. As I couldn't find any mistake just from looking at it, I did some tests.
Code: Select all
if (player.Class1 == CLASS_SCOUT or player.Class1 == CLASS_ROGUE) then
inventory:useItem(210512);
inventory:useItem(221582);
Using this piece of Code only works perfectly fine. He opens the bags, equips everything and uses the runic scroll.
It's the same with this piece of code:
Code: Select all
if (player.Class1 == CLASS_MAGE or player.Class1 == CLASS_PRIEST) then
inventory:useItem(210514);
inventory:useItem(221584);
And now comes the big problem: If I use something like
Code: Select all
if (player.Class1 == CLASS_MAGE or player.Class1 == CLASS_PRIEST) then
inventory:useItem(210514);
inventory:useItem(221584);
else
inventory:useItem(210511);
inventory:useItem(221582);
end;
I keep getting this compilation error.
I've tested this stuff in nearly every possible combination but whenever it comes to "else" my script goes bananas. The weird thing is that I am using "else" in many other WPs and they work fine. So what's the problem with this particular piece of code? What am I missing?
btw.
1. I know those Items arent completely right.. But as long as I can't get it to work, I don't care.
2. I am using the newest version of mm.
Thanks in advance :*
Re: cant't use "else"
Posted: Wed Nov 06, 2013 4:54 am
by Bill D Cat
Baerxu wrote:Hey there,
I want to make bot which will choose his equip from quests depending on his class.
The first pieces of equipment are obtained in from a armorbag and a weaponbag. So I let my bot open those bags. Now it shall check which class the bot's primary class is because the equipment u get from those bags are class-dependant, e.g. a mage gets a wand, a rouge gets a dagger.
Code: Select all
inventory:update();
inventory:useItem(201704); -- Waffen-Paket
inventory:useItem(201705); -- Ruestungspaket
if (player.Class1 == CLASS_SCOUT or player.Class1 == CLASS_ROGUE) then
inventory:useItem(210512);
inventory:useItem(221582);
else if (player.Class1 == CLASS_MAGE or player.Class1 == CLASS_PRIEST) then
inventory:useItem(210514);
inventory:useItem(221584);
else
inventory:useItem(210511);
inventory:useItem(221582);
end;
inventory:useItem(209577); -- Geheimnisvolle Runenschriftrolle
In this block of code, you are missing an "end" statement. Let me reformat it a little and you'll see why.
Code: Select all
if (player.Class1 == CLASS_SCOUT or player.Class1 == CLASS_ROGUE) then
inventory:useItem(210512);
inventory:useItem(221582);
else
if (player.Class1 == CLASS_MAGE or player.Class1 == CLASS_PRIEST) then
inventory:useItem(210514);
inventory:useItem(221584);
else
inventory:useItem(210511);
inventory:useItem(221582);
end;
<-- Missing "end" here.
inventory:useItem(209577); -- Geheimnisvolle Runenschriftrolle
Now if you just changed the "else if" to remove the space and make it "elseif" then everything should work fine.
Re: cant't use "else"
Posted: Wed Nov 06, 2013 5:46 am
by Baerxu
Thanks, that really was helpful but my problem still remains.
I changed my script a little to make it easier for me to use in future waypoints... So in my onload event I integrated this in my onload-event:
Code: Select all
if (player.Class1 == 2 or player.Class1 == 3) then
_armor = 0;
else
if (player.Class1 == 4 or player.Class1 == 5 or player.Class1 == 8) then
_armor = 1;
else
if player.Class1 == 1 then
_armor = 2;
else
_armor = 3;
end
end
end
Since I added those both "end"s I don't receive an onload error anymore - which I received before when I was trying to fix and change this stuff myself - so it seems to me it's working fine.
But in the WP itself it still doesn't work.
Code: Select all
if _armor == 0 then -- Leather
inventory:useItem(210512);
inventory:useItem(221582);
else if _armor == 1 then -- Cloth
inventory:useItem(210514);
inventory:useItem(221584);
else if _armor == 2 then -- Warrior
inventory:useItem(210511);
inventory:useItem(221582);
else -- Knight
inventory:useItem(210511);
inventory:useItem(221582);
end
end
end
Actually there should be enough "end"s as there are as many "end"s as "if"s and it's working this way in the onload-event (at least I don't receive a error message).
There is no difference concerning the compilation-error I receive whether I use "else if" or "elseif"... Btw does that matter?
Re: cant't use "else"
Posted: Wed Nov 06, 2013 6:05 am
by rock5
Let me see if I can explain it so you can understand.
Code: Select all
if A then
-- do A stuff
elseif B then
-- do B stuff
elseif C then
-- do C stuff
end
What this means is if A is true then it does A stuff. If it's false and B is true then it does B stuff. If A and B are false and C is true then it does C stuff.
Code: Select all
if A then
-- do A stuff
else
if B then
-- do B stuff
else
if C then
-- do C stuff
end
end
end
Actually, functionally, these would work the same. The difference being that the first example is a lot easier to understand, it's just 1 of 3 possible values, and the second could become very complicated very fast. I guess you could consider elseif a way of simplifying else ifs.
Re: cant't use "else"
Posted: Wed Nov 06, 2013 6:18 am
by Baerxu
Alright, thanks for this explanation, I guess I'll stick with elseif then (:
So I took this piece of code
Code: Select all
if A then
-- do A stuff
elseif B then
-- do B stuff
elseif C then
-- do C stuff
end
And made my piece of code look alike
Code: Select all
if _armor == 0 then -- A
inventory:useItem(210512); -- A Stuff
inventory:useItem(221582);
elseif _armor == 1 then -- B
inventory:useItem(210514); -- B Stuff
inventory:useItem(221584);
elseif _armor == 2 then -- C
inventory:useItem(210511); -- C Stuff
inventory:useItem(221582);
elseif _armor == 3 then -- D
inventory:useItem(210511); --D Stuff
inventory:useItem(221582);
end
I still receive "Failed to compile and run LUA Code [...]".
Everything else in this WP is working fine. In case I start the script without this if-check everything runs smoothly.
Re: cant't use "else"
Posted: Wed Nov 06, 2013 6:31 am
by rock5
I can't see anything wrong with that code but I copied it to my editor to see if the syntax highlighting would point anything out. In the editor I noticed that the 2 (210511)s had a question mark after them, before the brack. It's not visible in this post but I'm assuming they are some sort of hidden character. Try typing those numbers again including the brackets and see if that help.
Re: cant't use "else"
Posted: Wed Nov 06, 2013 6:43 am
by Baerxu
OMG, I am so damn angry right now. I have been trying to fix this piece of code for hours! Tryed every possibility I could think of... And it turns out that an **** invisible ? is ruining my code -.-
Anyways, thaaaaanks a lot. (:
Re: cant't use "else"
Posted: Thu Nov 07, 2013 12:02 am
by Bill D Cat
Baerxu wrote:Thanks, that really was helpful but my problem still remains.
I changed my script a little to make it easier for me to use in future waypoints... So in my onload event I integrated this in my onload-event:
Code: Select all
if (player.Class1 == 2 or player.Class1 == 3) then
_armor = 0;
else
if (player.Class1 == 4 or player.Class1 == 5 or player.Class1 == 8) then
_armor = 1;
else
if player.Class1 == 1 then
_armor = 2;
else
_armor = 3;
end
end
end
Just wanted to say "Aw, no love for the dwarves?" player.Class1 will be either 9 or 10 for Warlock or Champion.
And a suggestion for easier debugging of the code later, you could use the class names instead of the numbers like this:
Code: Select all
if (player.Class1 == CLASS_SCOUT or player.Class1 == CLASS_ROGUE) then
_armor = 0;
elseif (player.Class1 == CLASS_MAGE or player.Class1 == CLASS_PRIEST or player.Class1 == CLASS_DRUID or player.Class1 == CLASS_WARLOCK) then
_armor = 1;
elseif (player.Class1 == CLASS_WARRIOR or player.Class1 == CLASS_CHAMPION) then
_armor = 2;
else
_armor = 3;
end
Though I suppose with some elite skills, those values may change. ie: Warrior/Knight and Warden/Warrior get plate armor with their level 40 elites, and I think I read that the Druid/Warrior gets an upgrade as well to use chain armor with their level 70 elite. (But still no love from Runewaker for the Battle Monk!!)
Re: cant't use "else"
Posted: Thu Nov 07, 2013 5:23 am
by Baerxu
I've added the dwarf-classes by now, don't worry :b
Ye, those might change but as this is a script for a starting area this far, there is no chance any plate items will be distributed.
As soon as they may get plate armor as a questreward, I might check for those elite-skills but right now there is no need to do this. Anyways thanks for that hint (: