ShiroTora Ryu
ShiroTora Ryu

Reputation: 1

Lua - have a variable that seems to be not referenced anywhere later, so why is that code working?

this is my first post here, please excuse my english, it is not perfect.

So i want to learn LUA, I am a Beginner in Lua and Programming in general, and i followed a YouTube Tutorial, so i am not really the Author of the code that i will post later.

EDIT: (more clear) They are working well together, because there is only the variable with the boolean value „false“ and then the code directly goes to „invalid_command()“. But the code just runs perfectly. I actually would get this if the code would look like this: elseif valid = false then invalid_command(valid). Then there would be a visible connection between them. But instead we have elseif valid = false then invalid_command(). So valid seems to be not referenced. all which seems to be defined is that the variable (valid) is true in some cases, and false in other cases. But why can the Interpreter understand what „valid = false“ actually means? If i would be the Interpreter, i would understand that valid = false means that we now have a variable called „valid“, and then that the value of the variable is „false“. But how would i also understand that (valid = false) means in this code that the command is invalid? For me, it looks like There is skipped something, i am trying to see what is the „bridge“ to connect them. EDIT END

I do not get, why the variable called "valid" is working fine actually. It has the effect that is wanted. All i see in that code is either "valid = true" or "valid = false" but "valid" then seems not referenced later in that code, but still if i execute the code and type an invalid command via keyboard in ZeroBrane Studio, then the Interpreter is able to detect that command as invalid. But why can this be? I always thought that you need to reference a variable later in the same code, otherwise the variable will have either no effect or will cause an error.

if i execute the code and type in first "inspect", then i get a "You are in a cave, there is an exit." which is obvious why this is working, even for me.

if i now type in "asdf" or something else which is not defined, then i get a "You didn't write a valid command." But why is that working so well without the variable "valid" referenced anywhere later?

i actually also do not get why the Interpreter is able to get also what the variable "room_exit" means.

I cannot ask the author of the Tutorial and that code, he seems to be not active anymore.

Hope someone can explain me why this code is actually working well.

Thanks in Advance

this is the full code:

print("Welcome to the game. ")

input = ""

inv = {"coin", "sword", "armor"}

function get_inv()
  for i,v in pairs(inv) do
  print(i .. " " .. v) 
  end
end

function push_inv(item)
  table.insert(inv, item)
end

function pop_inv(item)
  for i, v in pairs(inv) do
    if v == item then
      table.remove(inv, i)
    end
  end
end

function invalid_command()
  print("You didn't write a valid command.")
end

function get_input()
  print("What do you want to do?")
  i = io.read()  -- Get what the User types
  return i
end

-- {'command' = 'response', 'command2' = 'response2'}
function room(t)
  room_exit = false
  input = ""
  while not room_exit do
    input = get_input()
    valid = false
    for k, v in pairs(t) do
        if input == k then
          if type(v) == "string" then
            print(v)
          else 
            v()
          end
          valid = true
        end
    end
    
    if input == "inv" then
      get_inv()
    elseif valid == false then
      invalid_command()
    end
  end
end

room_com = {
  inspect = "You are in a cave, there is an exit.", 
  exit = function()
    print("you leave the cave.")
    room_exit = true
  end
}

room(room_com)

Upvotes: 0

Views: 456

Answers (2)

luther
luther

Reputation: 5564

You may be confusing = with ==. = means that we're assigning a value to a variable.

== is an operator that returns true if both values are equal, and false otherwise. This boolean value is then used to decide if the elseif block runs.

This code is a bit confusing, because comparing valid to false is redundant. They could have just said not valid instead of valid == false.

Upvotes: 1

Blank Dank
Blank Dank

Reputation: 9

as you can see in your code there is a function called "invalid_command". Because there is also a variable (valid), when you type something invalid, it runs that function. The room exit variable is to stop the room function from running and make it say that you have exited the cave. I hope this helps. If you need more help, feel free to comment to my answer.

Upvotes: 0

Related Questions