2

First off, I know this is a messy, unorganized pile of Lua, but I'm still learning, so please forgive me.

My problem is: I run my code on TouchLua+ on my iPad, it runs nice and smoothly until I get down to the tinput = io.read(), whether I type 'attack' or 'run away' I end up with an "that is not a valid command" printed on the screen, infinitely.

I believe it is a conflict between the get_input() in the while statement, and the fact that I have defined io.read() as tinput instead of the standard input through the whole script.

I could accept this except from the fact that the script I am learning from (youtube video at the moment) doesn't do this, I have compared the two and the function, while, and the 'if tinput' and they are written exactly the same.

I have tried to find answers on google, the comments on the video and the github the original script is stored on, but I don't understand github yet, and google hasn't helped.

What have I done wrong, or missed, or generally screwed up...

Also, please excuse my heavy use comments, sorry.

    print("Welcome to the game")

    -- expilictly set the variable to an empty string
    -- because it is used in a 'while' loop
    input = ""
    inv = {"sword", "coin", "armour"}

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

    -- invalid command function
    function inv_com()
        print("You did not type a valid command...")
    end

    -- function to simplify 'while' statement throughout
    -- entire programme
    function get_input()
        print("What do you want to do?")
        i = io.read() -- get what the user types
        return i
    end

    -- fuction adding item to inventory
    function push_inv(item)
        table.insert(inv, item)
    end

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

    -- '~=' means not equal to
    while input ~= "leave cave" do
        input = get_input() -- get what the user types

        -- '==' means equal to
        if input == "inspect" then
            print("You are in a cave")
        elseif input == "leave cave" then
            print("You leave cave")
        elseif input == "inv" then
            get_inv()
        else
            inv_com()
        end
    end

    input = ""
    while input ~= "follow path" do
        input = get_input()

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")
    print("A troll appears wielding an axe")
    print("What do you want to do")
    tinput = io.read()
    if tinput == "attack" then
        print("You smack it, and it falls dead")
    elseif tinput == "run away" then
        print("You cowardly run away and it smack you, and steals your coin")
        pop_inv("coin")
    else
        print("You stand there, it stabs you, you die")
        os.exit()
    end
elseif input == "inv" then
    get_inv()
else
    inv_com()
end
    end


    input = ""

    -- a boolean value can only have 1 of 2 values
    -- 'true' or 'false', this works great for the have_key
    -- value because there are only 2 possibilities:
    -- you either have the key or you don't.
    have_key = false

    -- this stement mean: while this statement:
    -- 'input == "open gate"
    -- and have_key' is true
    while not (have_key == true and input == "open gate") do
input = get_input


if input == "inspect" then
    --- if we dont have the key, tell us
    if have_key == false then
        print("there is a path behind you, a gate infront of you and key is hidden in the grass")
        -- if we have the key, dont tell us
    elseif have_key == true then
        print("There is a path behind you, and a gate infront of you")
    else
        print("you did not put a valid command")
    end

    -- we now have the key, set variable to true
elseif input == "grab key" then
    have_key = true
    print("You grabbed the key")
    table.insert(inv, "gate key")
elseif input == "inv" then
    get_inv()
elseif input == "open gate" then
    if have_key == true then
        print("You've escaped")
    elseif have_key == false then
        print("The gate is locked")
    end
elseif input == "pick up magic" then
    push_inv("MAGIC")
elseif input == "inv" then
    get_inv()
else
    inv_com()
end
    end


    print("You won the game. Please leave!")

1 Answer 1

2

One of your while loops has the code

input = get_input

This should be

input = get_input()

What's happening is that you've set the variable input equal to the function get_input, not the result of calling it, so input will never be a valid string, it will always call inv_com.


EDIT: I know that this is outside of the scope of the question, but I have a couple of tips for your program outside of that specific problem.

  1. Be careful when naming your functions that you don't create ambiguities. You have functions like pop_inv and push_inv for inventory management, but at the same time have inv_com for invalid commands.
  2. Read the Lua style guide. Your code has some really crazy indentation going on, which makes it really hard for other people to understand what's going on.
  3. Try and think about how you could turn all of this code into potentially one main loop that can handle all possibilities, instead of having a bunch of loops and if statements one after another. Think about how complicated your program's structure is going to become if you want to add more functionality later.

What you're doing reminds me a lot of how I started programming, making text adventures. If you're interested in text adventures, take a look at Zork to see how their game loop works. Additionally do some research on state machines, as it seems like that's going to be useful for what you're building right now. Happy coding :)

Sign up to request clarification or add additional context in comments.

1 Comment

If my answer helped, could you please mark it as accepted? If it's still in an infinite loop I'd be glad to take another look and edit my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.