This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Weasels's profile picture

Help! Getting an error I don't how to fix.

Started by Weasels, 18 April 2015 - 12:07 AM
Weasels #1
Posted 18 April 2015 - 02:07 AM
So I'm trying to make a little Dungeon And Dragons kind of program for a few friends of mine to play on a server. After the character creation it starts the introduction to the story and is suppose to draw a map of a small room. I have a file saved on the computer named "smallRoom" with a sort of map drawn using hashtags.

########################
#			 #
#			 #
#			 #
#			 #
#			 #
#			 #
#			 #
#			 #
########################

When I try to load the map I get the error: program:201: Expected string, string
Here is the code associated with this.


function load(name)
local file = fs.open(name,"r")  --Line 201--
local data = file.readAll()
file.close()
return data
end
drawStats()
term.setCursorPos(22,10)
print("Please wait...")
sleep(4)
term.clear()
term.setCursorPos(1,1)
print(name, " awoke in a small room...")
sleep(2)
print("It was cold, all of ", name, "'s supplies have been stolen...")
sleep(3)
term.clear()
load(smallRoom)

Please help, it's keeping me from getting anything done with this program.
Edited on 18 April 2015 - 12:19 AM
valithor #2
Posted 18 April 2015 - 02:13 AM
In the last line of the code you posted you pass smallRoom to the function load, which is nil based upon the code you have posted.

If smallRoom is a variable then please upload the full code to pastebin, or mark which line is line 201 in your program.

Assuming it is not a variable change the last line in the code you posted to

load("smallRoom")
Edited on 18 April 2015 - 12:18 AM
Weasels #3
Posted 18 April 2015 - 02:18 AM
Your function load expects it is passed a string yet you pass it nil, or I am assuming nil as smallRoom is not defined in the code you posted.

If smallRoom is a variable then please upload the full code to pastebin, or mark which line is line 201 in your program.

Assuming it is not a variable change the last line in the code you posted to

load("smallRoom")

Thanks for replying. Line 201 is
local file = fs.open(name,"r")
inside the function load()
smallRoom is not defined in the code anywhere else in the program. smallRoom is the name of the map that is basically box drawn "Dwarf Fortress" style, if you know what I mean.
valithor #4
Posted 18 April 2015 - 02:24 AM
-snip

Your problem is how you have passed the argument to the function. Without quotes it looks for a variable with that name, so passing smallRoom to the function will tell the program to look for a function named smallRoom. Because you never defined one it does not exist so you pass nothing to the load function. As a result fs.open errors saying it requires two strings, because one of the things being passed to it is nil/nothing.

As stated in my previous post putting quotes "" around smallRoom will cause it to treat it as a string instead of a variable.

Solution: change last line of the code you posted to

load("smallRoom")

Sorry if you had already figured this out from my previous post; I had reread my post and it was not very descriptive/helpful.
Edited on 18 April 2015 - 12:26 AM
Weasels #5
Posted 18 April 2015 - 02:34 AM
-snip

Your problem is how you have passed the argument to the function. Without quotes it looks for a variable with that name, so passing smallRoom to the function will tell the program to look for a function named smallRoom. Because you never defined one it does not exist so you pass nothing to the load function. As a result fs.open errors saying it requires two strings, because one of the things being passed to it is nil/nothing.

As stated in my previous post putting quotes "" around smallRoom will cause it to treat it as a string instead of a variable.

Solution: change last line of the code you posted to

load("smallRoom")

Sorry if you had already figured this out from my previous post; I had reread my post and it was not very descriptive/helpful.
It makes sense and it got rid of the error; however, it doesn't print the box to the screen, it just prints an empty line to the screen like nothing was copied/read.
KingofGamesYami #6
Posted 18 April 2015 - 02:37 AM
Well, based on your code it shouldn't do anything. You never write any of the data you loaded to the screen…
valithor #7
Posted 18 April 2015 - 02:37 AM
-snip

From the current code you are not doing anything with the value returned from the load function.

example of function returning:

local function hello()
  return "hello"
end

print(hello()) --# this prints hello because hello returns "hello"
hello() --# runs the hello function, the hello function returns, but nothing is ever done with what it returns and it is discarded

edit:
ninja'd
Edited on 18 April 2015 - 12:38 AM
Weasels #8
Posted 18 April 2015 - 03:33 AM
Ah thanks! I figured it out now.
Weasels #9
Posted 18 April 2015 - 05:24 AM
Hello, hope I'm not bothering you but I just got one more question. In the map "smallRoom" I have the "#" representing the walls. I made it so you can move using the arrow keys and I was wondering if you knew how I'd make the walls solid so I don't just go through them. If you know what I mean. Here is the code:


--Default Stats--
dmg = "0"
def = "0"
int = "0"
per = "0"
acc = "0"
spe = "0"
health = "0"
mana = "0"
level = "0"
name = "nothing"
playing = false
posX = "0"
posY = "0"
--Classes Settings--

function mage()
dmg = "5"
def = "3"
int = "10"
per = "8"
acc = "4"
spe = "9"
health = "22"
mana = "45"
level = "1"
name = name
end

function paladin()
dmg = "9"
def = "7"
int = "4"
per = "7"
acc = "6"
spe = "5"
health = "35"
mana = "12"
level = "1"
name = name
end

function knight()
dmg = "12"
def = "7"
int = "0"
per = "0"
acc = "6"
spe = "6"
health = "40"
mana = "0"
level = "1"
name = name
end

function thief()
dmg = "7"
def = "5"
int = "5"
per = "2"
acc = "8"
spe = "12"
health = "25"
mana = "10"
level = "1"
name = name
end

function cleric()
dmg = "5"
def = "4"
int = "5"
per = "10"
acc = "3"
spe = "8"
health = "23"
mana = "40"
level = "1"
name = name
end

function archer()
dmg = "8"
def = "6"
int = "4"
per = "4"
acc = "11"
spe = "7"
health = "29"
mana = "10"
level = "1"
name = name
end

--End of Classes--
function freeMove()				   --This is my movement function--
while playing == true do
  local event, key = os.pullEvent("key")
  if key == keys.up then
   term.setCursorPos(posX,posY)
   print(" ")
   posY = posY - 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print("^")
  elseif key == keys.down then
   term.setCursorPos(posX,posY)
   print(" ")
   posY = posY + 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print("V")
  elseif key == keys.right then
   term.setCursorPos(posX,posY)
   print(" ")
   posX = posX + 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print(">")
  elseif key == keys.left then
   term.setCursorPos(posX,posY)
   print(" ")
   posX = posX - 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print("<")
  end
  if playing == false then
   break
  end
end
end
term.clear()
term.setCursorPos(17,1)
print("Character Design")
sleep(1)
term.setCursorPos(2,3)
write("Character Name: ")
name = read()
term.clear()
--Race Selection--
write("Select a race: ")
race = read()
if race == "human" then
  term.clear()
elseif race == "hafling" then
   term.clear()
  elseif race == "elf" then
	term.clear()
   elseif race == "orc" then
	 term.clear()
	elseif race == "drow" then
	  term.clear()
else
  print("Invalid Race!")
  sleep(1)
  print("Please run program: races for the list of playable races.")
  sleep(2.5)
  term.clear()
  shell.run("mud")
end
--Class Selection--
write("Select a class: ")
class = read()
if class == "mage" then
  mage()
  term.clear()
elseif class == "paladin" then
   paladin()
   term.clear()
  elseif class == "knight" then
	knight()
	term.clear()
   elseif class == "thief" then
	 thief()
	 term.clear()
	elseif class == "cleric" then
	  cleric()
	  term.clear()
	 elseif class == "archer" then
	   archer()
	   term.clear()
else
  print("Invalid Class!")
  sleep(1)
  print("Please run program: classes for the list of playable classes.")
  sleep(2.5)
  term.clear()
  shell.run("mud")
end
--End Of Character Creation / Stats--
function drawStats()
term.setCursorPos(17,1)
print(name, " the ", race, " ", class, "!")
term.setCursorPos(1,3)
print("Damage: ", dmg)
print("Defence: ", def)
print("Intelligence: ", int)
print("Personality: ", per)
print("Accuracy: ", acc)
print("Speed: ", spe)
print("Health: ", health)
print("Mana: ", mana)
end
function load(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
print(data)
end
drawStats()
term.setCursorPos(22,10)
print("Please wait...")
sleep(4)
term.clear()
term.setCursorPos(1,1)
print(name, " awoke in a small room...")
sleep(2)
print("It was cold, all of ", name, "'s supplies have been stolen...")
sleep(3)
term.clear()
--Area 1: Small Room--
load("smallRoom")
playing = true
term.setCursorPos(25,9)
term.setTextColor(colors.orange)
print("^")
posX = "25"
posY = "9"
freeMove()

This is a screenshot I took of the program running, the arrow being the character. When you move right it changes to > etc… hope this helps.
Edited on 18 April 2015 - 03:38 AM
Weasels #10
Posted 18 April 2015 - 05:50 AM
Hello! I recently posted a topic for an error I had regarding the same program, now I just have a question. I'm trying to create a Dungeon And Dragons type of game for Computercraft. Everything was going well until now. When you start the game you'll be a room called "smallRoom" and your character is represented by an arrow which faces direction when you move that direction. Here's a screenshot of what the room looks like with the character in it.http://imgur.com/4IK1Big

The "#" represents the walls of the room and you control your movement with arrow keys. How would I make the "#" solid so I can't move through them?


--Default Stats--
dmg = "0"
def = "0"
int = "0"
per = "0"
acc = "0"
spe = "0"
health = "0"
mana = "0"
level = "0"
name = "nothing"
playing = false
posX = "0"
posY = "0"
--Classes Settings--

function mage()
dmg = "5"
def = "3"
int = "10"
per = "8"
acc = "4"
spe = "9"
health = "22"
mana = "45"
level = "1"
name = name
end

function paladin()
dmg = "9"
def = "7"
int = "4"
per = "7"
acc = "6"
spe = "5"
health = "35"
mana = "12"
level = "1"
name = name
end

function knight()
dmg = "12"
def = "7"
int = "0"
per = "0"
acc = "6"
spe = "6"
health = "40"
mana = "0"
level = "1"
name = name
end

function thief()
dmg = "7"
def = "5"
int = "5"
per = "2"
acc = "8"
spe = "12"
health = "25"
mana = "10"
level = "1"
name = name
end

function cleric()
dmg = "5"
def = "4"
int = "5"
per = "10"
acc = "3"
spe = "8"
health = "23"
mana = "40"
level = "1"
name = name
end

function archer()
dmg = "8"
def = "6"
int = "4"
per = "4"
acc = "11"
spe = "7"
health = "29"
mana = "10"
level = "1"
name = name
end

--End of Classes--
function freeMove()				   --This is my movement function--
while playing == true do
  local event, key = os.pullEvent("key")
  if key == keys.up then
   term.setCursorPos(posX,posY)
   print(" ")
   posY = posY - 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print("^")
  elseif key == keys.down then
   term.setCursorPos(posX,posY)
   print(" ")
   posY = posY + 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print("V")
  elseif key == keys.right then
   term.setCursorPos(posX,posY)
   print(" ")
   posX = posX + 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print(">")
  elseif key == keys.left then
   term.setCursorPos(posX,posY)
   print(" ")
   posX = posX - 1
   term.setCursorPos(posX,posY)
   term.setTextColor(colors.orange)
   print("<")
  end
  if playing == false then
   break
  end
end
end
term.clear()
term.setCursorPos(17,1)
print("Character Design")
sleep(1)
term.setCursorPos(2,3)
write("Character Name: ")
name = read()
term.clear()
--Race Selection--
write("Select a race: ")
race = read()
if race == "human" then
  term.clear()
elseif race == "hafling" then
   term.clear()
  elseif race == "elf" then
	term.clear()
   elseif race == "orc" then
	 term.clear()
	elseif race == "drow" then
	  term.clear()
else
  print("Invalid Race!")
  sleep(1)
  print("Please run program: races for the list of playable races.")
  sleep(2.5)
  term.clear()
  shell.run("mud")
end
--Class Selection--
write("Select a class: ")
class = read()
if class == "mage" then
  mage()
  term.clear()
elseif class == "paladin" then
   paladin()
   term.clear()
  elseif class == "knight" then
	knight()
	term.clear()
   elseif class == "thief" then
	 thief()
	 term.clear()
	elseif class == "cleric" then
	  cleric()
	  term.clear()
	 elseif class == "archer" then
	   archer()
	   term.clear()
else
  print("Invalid Class!")
  sleep(1)
  print("Please run program: classes for the list of playable classes.")
  sleep(2.5)
  term.clear()
  shell.run("mud")
end
--End Of Character Creation / Stats--
function drawStats()
term.setCursorPos(17,1)
print(name, " the ", race, " ", class, "!")
term.setCursorPos(1,3)
print("Damage: ", dmg)
print("Defence: ", def)
print("Intelligence: ", int)
print("Personality: ", per)
print("Accuracy: ", acc)
print("Speed: ", spe)
print("Health: ", health)
print("Mana: ", mana)
end
function load(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
print(data)
end
drawStats()
term.setCursorPos(22,10)
print("Please wait...")
sleep(4)
term.clear()
term.setCursorPos(1,1)
print(name, " awoke in a small room...")
sleep(2)
print("It was cold, all of ", name, "'s supplies have been stolen...")
sleep(3)
term.clear()
--Area 1: Small Room--
load("smallRoom")
playing = true
term.setCursorPos(25,9)
term.setTextColor(colors.orange)
print("^")
posX = "25"
posY = "9"
freeMove()
grand_mind1 #11
Posted 18 April 2015 - 06:00 AM
In your movement function, you can check if the position the user has moved to is a valid spot. Perhaps add the positions of walls to a table when they are created and loop through that to check in your movement function.
HPWebcamAble #12
Posted 18 April 2015 - 06:36 AM
You'll want to use tables to define where your walls are.

You can use the table to draw them the first time, then check to make sure the player isn't moving into a wall


Here's a quick intro to tables:
http://lua-users.org/wiki/TablesTutorial

You should probably use tables to store the info for your classes too
Lyqyd #13
Posted 18 April 2015 - 07:39 AM
Threads merged.