🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

is this the right code?

Started by
4 comments, last by indie_evolution 3 years, 6 months ago

hi everyone i am new to gamedev but not new to coding

i had a question

is this the right code for a text rpg game in lua?

i got to this code so far

local Fish = Instance.new("IntValue")
local wood = Instance.new("IntValue")
wood.Value = 0
fish.Value = 0

io.write("you are trappedin the woods by yourself, alone")
io.write(", what should i do")

response = io.read()
  if response == "Get wood" then
     wood.Value = wood.Value + 1
     print("the mission was a success")
  else
     print("you get wounded and need some bandages")
     wood.Value = wood.Value - 1
  end


  if response == "Hunt for Fish" then
     fish.Value = fish.Value + 1
     print("the mission was a success")
  else
     print("The fish got away, will get them some other day")
  end
end

io.write("now what should i do")

local Hut = Instance.new("IntValue")
Hut.Value = 0

  if response == "Get wood" then
     wood.Value = wood.Value + 1
     print("the mission was a success")
  else
     print("you get wounded and need some bandages")
     wood.Value = wood.Value - 1
  end

  if response == "Hunt for Fish" then
     fish.Value = fish.Value + 1
     print("the mission was a success")
  else
     print("The fish got away, will get them some other day")
  end

  if response == "Build a Hut " then 
     Hut.Value = Hut.Value + 1
     print("The build was a success")
  else
     print("the build collapsed")
     Hut.Value = Hut.Value - 1
  end
end

io.write("Now that we have a shelter we can start exploring")
io.write("now what should i do?")

local trap = Instance.new("IntValue")
local toolset = Instance.new("IntValue")
trap.Value = 0
toolset.Value = 0

  if response == "Get wood" then
     wood.Value = wood.Value + 1
     print("the mission was a success")
  else
     print("you get wounded and need some bandages")
     wood.Value = wood.Value - 1
  end

  if response == "Hunt for Fish" then
     fish.Value = fish.Value + 1
     print("the mission was a success")
  else
     print("The fish got away, will get them some other day")
  end

  if response == "Build a Hut " then 
     Hut.Value = Hut.Value + 1
     print("The build was a success")
  else
     print("the build collapsed")
     Hut.Value = Hut.Value - 1
  end

  if response == "Build a Trap" then 
     Trap.Value = Trap.Value + 1
     print("The build was a success")
  else
     print("the build collapsed")
     Trap.Value = Trap.Value - 1
  end

  if response == "Forge a Tool set" then 
     toolset.Value = toolset.Value + 1
     print("The Forging was a success")
  else
     print("the Forging didn't work correctly")
  end
end

io.write("now what should i do?")

  if response == "Get wood" then
     wood.Value = wood.Value + 1
     print("the mission was a success")
  else
     print("you get wounded and need some bandages")
     wood.Value = wood.Value - 1
  end

  if response == "Hunt for Fish" then
     fish.Value = fish.Value + 1
     print("the mission was a success")
  else
     print("The fish got away, will get them some other day")
  end

  if response == "Build a Hut " then 
     Hut.Value = Hut.Value + 1
     print("The build was a success")
  else
     print("the build collapsed")
     Hut.Value = Hut.Value - 1
  end

  if response == "Build a Trap" then 
     Trap.Value = Trap.Value + 1
     print("The build was a success")
  else
     print("the build collapsed")
     Trap.Value = Trap.Value - 1
  end

  if response == "Forge a Tool set" then 
     toolset.Value = toolset.Value + 1
     print("The Forging was a success")
  else
     print("the Forging didn't work correctly")
  end

  if response == "Check Traps" then
     Fish.Value = Fish.Value + 5
     print("the where caught")
  else 
     print("The trap collapsed")
     Fish.Value = Fish.Value - 3
  end
end
Advertisement

You can do it that way, that's how I started writing text adventures in BASIC back in the day. However, as your game grows larger, it will be difficult to manage. You may want to try to package each room as an object or class (I guess this would be a dictionary in Lua but I don't have much experience there). Then in the dictionary you can store the initial message for the room, a associative array / hash table of choices, and then result of each choice (which could be as simple as showing a message, updating a variable, or loading a new room). But as I said, I've only worked in Lua a tiny bit so I can't give you any code.

There are a large number of serious issues with that code:

  • You only read user input once, then use that input for multiple iterations. This tells me that you never even tried to run the code that you posted.
  • You only check for very specific command strings. For example, you check for “Build a Hut ”, but not for “Build a Hut” (no extra space at end of string) or “Build a hut “ (”hut" not capitalized) or “build a hut” or “build hut” or “build shelter” or “construct hut” or any number of other variations.
  • The feedback you give is totally useless. If the player tries to build a hut, the game says “The build was a success” and then says “the build collapsed” (because the player didn't build a trap). If the player tries to build a trap, the game gives the same feedback, with only the order switched. It's up to the player to guess what was a success and what collapsed.
  • You don't give any indication when the game fails to understand the player's input.
  • You have a lot of code repetition. You should use a loop instead of repeating the same code over and over again.
  • None of the player's actions have any real effect on the game.
  • The narrative switches between second person and first person, and between first person singular and first person plural.
  • The game has spelling, punctuation, and grammar errors.

Your question isn't very clear. In a very, very broad sense, yes, that is how you write a game. You write a game by writing code, which is what you have done. In all of the particulars mentioned in my list, no, that is not how you write a game. At least, not a good one.

That was a charming read. Warmed my cold, weary developer heart.

You're on the right track.
When you get around to structures and array, perhaps you can have a command that require some basic tokens to be present in a particular order, like ["build", “hut”] and you could make it so that the code that processes the command checks against these command criteria without looking at whitespace or casing.

And it's great to have a dedicated command processor.

And a state machine.

And a simple inventory management system.

But you'll get there!

@logan38 please take my advice with a grain of salt. I new to the forums. I have never made a game. However, I have been a programmer for most of my professional life. First I want to say that your attempt is admirable. The biggest obstacle I have faced in game development despite my programming experience is finding the time and energy to start a project. Congratulations you are well on your way to becoming a game developer. Looking at your code I can almost see what you were thinking when you wrote it : ). That is a good thing it means you are expressing yourself properly with the tools at your disposal. I have just started reading up on design patterns in game development. I would recommend you do the same. There is a really good free resource online that can help you. Please, check out the free e-book version https://gameprogrammingpatterns.com/contents.html.​ The content will introduce you to the state pattern and the command pattern as well as others in a very game-centric way.

Indie

None

This topic is closed to new replies.

Advertisement