Titan
Titan

Reputation: 13

Lua "or" statement issue

I'm very new to Lua, and I'm doing a very simple text based adventure thing, but it wont work. My code is as follows:

while input ~= ("leave cave" or "leave") do
print("What do you want to do?")
input = io.read()

if input == "inspect" then 
        print("You are in a cave") 
    elseif input == "leave cave" or "leave" then
        print("You leave the cave")
    elseif input == "inv" then
        for i,v in pairs(inv) do
        print(i, v)
    end
  else
    print("You didn't write a valid command...")
  end
end

-- leave cave

input = ""
print("What do you want to do?")
input = io.read()
while input ~= "follow path" do 
if input == "inspect" then 
        print("You are at the base of a hill. There is a path.") 
    elseif input ==  "follow path" then
        print("You follow the path. There is a gate.") 
     elseif input == "inv" then
        for i,v in pairs(inv) do
        print(v)
        end
    else 
        print("That's not a valid command...")
    end 
end

What I'm trying to do is have it so whenever the user types leave, or leave cave, it proceeds to the next segment (the path one), however, when I type "leave" and then type "inspect" again it says "I am in a cave" rather than what it should be saying which is saying that you left, and you see a path. And when I type leave cave, and then inspect, it spams "You are at the base of a hill. THERE IS A PATH" over and over, indefinitely.

And when I type "inv" it doesn't print my inventory, and instead prints "You left the cave," but doesn't actually leave.

Upvotes: 1

Views: 1828

Answers (2)

warspyking
warspyking

Reputation: 3113

While or cannot accomplish such a complex operation, it is possible to recreate the effect yourself with some hacky metatable code.

Please note I do not reccomend using this code in any serious professional or commercial programs, or really at all for that matter, this code is inefficient and unecessary, however it is a fun piece of code to do exactly what you're looking for. It's just a fun way to experiment with the power of Lua.

local iseither
iseither = setmetatable({},{
   __sub = function(arg1,arg2)
      if arg2 == iseither then
         arg2.Value = arg1
         return arg2
      else
         if type(arg2) ~= "table" then
            error("Second operator is -iseither- was not a table",2)
         else
            for i,v in ipairs(arg2) do
               if arg1.Value == v then
                  arg1.Value = nil
                  return true
               end
            end
            arg1.Value = nil
            return false
         end
      end
   end
})

print(1 -iseither- {1,2,3,4,5})

Upvotes: 0

Curtis Fenner
Curtis Fenner

Reputation: 1403

a or b can't make a value that means "either a or b" -- that would be too complicated.

In fact, if you ask it to choose between two strings, it will just pick the first:

print("leave cave" or "leave") --> leave cave

or is only meant to be used on booleans -- you have to combine it on multiple full conditions:

while (input ~= "leave cave") and (input ~= "leave") do

In this case, a repeat ....... until <condition> loop might serve you better:

repeat
    print("What do you want to do?")
    input = io.read()

    -- <do stuff>
until input == "leave" or input == "leave cave"

Upvotes: 3

Related Questions