Page 1 of 1

Probably really easy IF THEN question

Posted: Sat Aug 26, 2023 3:54 pm
by codrus
Hi. Sorry if this is a really easy question. It seems so simple, but I just can't seem to see why it isn't working.

I have an alias called spiderdown which I want to run me down to the next level from my current one. It looks like the following (and, yes, I make sure floor is equal to 1 beforehand):
if floor == 1 then
send("go 9e, 9n, 18w, 2s, 16e, 5s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, d")
elseif floor == 2 then
send("go 11s, 18e, 8n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, d")
elseif floor == 3 then
send("go n, w, 2n, e, 2n, 2w, 5n, 2e, 3s, 2e, 3n, 14e, 6s, 10w, 2n, 8e, 2n, 10w, 3s, 2w, 2s, 2e, 2s, 2e, n, 10e, s, d")
elseif floor == 4 then
send("go 9s, 18w, 2n, 16e, 2n, 16w, 2n, 16e, 2n, 13w, d")
elseif floor == 5 then
send("go 2n, 15e, 8n, 18w, 4s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 4s, 16w, 4s, d")
elseif floor == 6 then
send("go 8s, 6e, 2n, 4w, 2n, 11e, 2s, 5w, 2s, 10e, 2n, 3w, 2n, 3e, 2n, 16w, 2n, d")
elseif floor == 7 then
send("go 16e, 2n, 16w, 2n, 16e, 2n, 16w, 2n, 16e, 2n, 18w, 9s, d")
elseif floor == 8 then
send("go 9s, 18e, 18n, 16w, 16s, 14e, 14n, 12w, 12s, 10e, 10n, 8w, 8s, 6e, 6n, 4w, 4s, 2e, 2n, d")
elseif floor == 9 then
echo("\nAlready on 9th floor.\n")
floor = floor - 1
end
floor = floor + 1
When I activate the alias, no command is sent to mud, but floor is then equal to 2. So the first IF statement isn't being activated. Why?

Thanks for any help!

Re: Probably really easy IF THEN question

Posted: Sat Aug 26, 2023 7:13 pm
by dt192
It works fine for me if floor is the number 1. If however floor is the string "1" send doesn't fire, but ends up being 2.

You probably need to add a

Code: Select all

floor = tonumber(floor)
line at the start.

Re: Probably really easy IF THEN question

Posted: Sat Aug 26, 2023 10:25 pm
by codrus
Great! That appears to have been the issue.

Thanks so much!

Re: Probably really easy IF THEN question

Posted: Sun Aug 27, 2023 12:07 pm
by Jor'Mox
Interestingly, you can actually use tables to bypass most of the if/then stuff here, like this.
Code: [show] | [select all] lua
local dirs = {"go 9e, 9n, 18w, 2s, 16e, 5s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, d",
"go 11s, 18e, 8n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, d",
"go n, w, 2n, e, 2n, 2w, 5n, 2e, 3s, 2e, 3n, 14e, 6s, 10w, 2n, 8e, 2n, 10w, 3s, 2w, 2s, 2e, 2s, 2e, n, 10e, s, d",
"go 9s, 18w, 2n, 16e, 2n, 16w, 2n, 16e, 2n, 13w, d",
"go 2n, 15e, 8n, 18w, 4s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 4s, 16w, 4s, d",
"go 8s, 6e, 2n, 4w, 2n, 11e, 2s, 5w, 2s, 10e, 2n, 3w, 2n, 3e, 2n, 16w, 2n, d",
"go 16e, 2n, 16w, 2n, 16e, 2n, 16w, 2n, 16e, 2n, 18w, 9s, d",
"go 9s, 18e, 18n, 16w, 16s, 14e, 14n, 12w, 12s, 10e, 10n, 8w, 8s, 6e, 6n, 4w, 4s, 2e, 2n, d"}

floor = tonumber(floor)
local cmd = dirs[floor]
if cmd then
    send(cmd)
    floor = floor + 1
else
    echo("\nAlready on 9th floor.\n")
end

Re: Probably really easy IF THEN question

Posted: Sun Aug 27, 2023 12:58 pm
by dt192
Jor'Mox wrote:
Sun Aug 27, 2023 12:07 pm
Interestingly, you can actually use tables to bypass most of the if/then stuff here, like this.
Code: [show] | [select all] lua
local dirs = ["go 9e, 9n, 18w, 2s, 16e, 5s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, 2w, 3n, 2w, 3s, d",
"go 11s, 18e, 8n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, 2w, 6s, 2w, 6n, d",
"go n, w, 2n, e, 2n, 2w, 5n, 2e, 3s, 2e, 3n, 14e, 6s, 10w, 2n, 8e, 2n, 10w, 3s, 2w, 2s, 2e, 2s, 2e, n, 10e, s, d",
"go 9s, 18w, 2n, 16e, 2n, 16w, 2n, 16e, 2n, 13w, d",
"go 2n, 15e, 8n, 18w, 4s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 2s, 2e, 2n, 2e, 4s, 16w, 4s, d",
"go 8s, 6e, 2n, 4w, 2n, 11e, 2s, 5w, 2s, 10e, 2n, 3w, 2n, 3e, 2n, 16w, 2n, d",
"go 16e, 2n, 16w, 2n, 16e, 2n, 16w, 2n, 16e, 2n, 18w, 9s, d",
"go 9s, 18e, 18n, 16w, 16s, 14e, 14n, 12w, 12s, 10e, 10n, 8w, 8s, 6e, 6n, 4w, 4s, 2e, 2n, d"]

floor = tonumber(floor)
local cmd = dirs[floor]
if cmd then
    send(cmd)
    floor = floor + 1
else
    echo("\nAlready on 9th floor.\n")
    floor = floor - 1
end
Your table is using square brackets instead of swirlies and `floor = floor - 1` is undeed.

Re: Probably really easy IF THEN question

Posted: Mon Aug 28, 2023 11:44 am
by Jor'Mox
You are right that. I'm totally sleep deprived. My bad.Thanks.

Edit: Fixed the original post to use the right brackets, and pull out the other bit.