Jump to content




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



12 replies to this topic

#1 Weasels

  • Members
  • 11 posts

Posted 18 April 2015 - 01: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 by Weasels, 18 April 2015 - 01:19 AM.


#2 valithor

  • Members
  • 1,053 posts

Posted 18 April 2015 - 01: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 by valithor, 18 April 2015 - 01:18 AM.


#3 Weasels

  • Members
  • 11 posts

Posted 18 April 2015 - 01:18 AM

View Postvalithor, on 18 April 2015 - 01:13 AM, said:

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.

#4 valithor

  • Members
  • 1,053 posts

Posted 18 April 2015 - 01:24 AM

View PostWeasels, on 18 April 2015 - 01:18 AM, said:

-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 by valithor, 18 April 2015 - 01:26 AM.


#5 Weasels

  • Members
  • 11 posts

Posted 18 April 2015 - 01:34 AM

View Postvalithor, on 18 April 2015 - 01:24 AM, said:

View PostWeasels, on 18 April 2015 - 01:18 AM, said:

-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.

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 18 April 2015 - 01:37 AM

Well, based on your code it shouldn't do anything. You never write any of the data you loaded to the screen...

#7 valithor

  • Members
  • 1,053 posts

Posted 18 April 2015 - 01:37 AM

View PostWeasels, on 18 April 2015 - 01:34 AM, said:

-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 by valithor, 18 April 2015 - 01:38 AM.


#8 Weasels

  • Members
  • 11 posts

Posted 18 April 2015 - 02:33 AM

Ah thanks! I figured it out now.

#9 Weasels

  • Members
  • 11 posts

Posted 18 April 2015 - 04: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 by Weasels, 18 April 2015 - 04:38 AM.


#10 Weasels

  • Members
  • 11 posts

Posted 18 April 2015 - 04: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()


#11 grand_mind1

  • Members
  • 207 posts

Posted 18 April 2015 - 05: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.

#12 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 18 April 2015 - 05: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.../TablesTutorial

You should probably use tables to store the info for your classes too

#13 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 18 April 2015 - 06:39 AM

Threads merged.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users