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

rpg:62: attempt to index? (a nil value) WHY?

Started by Tjakka5, 12 April 2013 - 02:33 AM
Tjakka5 #1
Posted 12 April 2013 - 04:33 AM
Okay, so I'm trying to make a game, link: http://www.computercraft.info/forums2/index.php?/topic/11829-rpg-game-wip-v-04/

However, I created a bug, and now I get a error at line 62…
If someone can figure out why, it will be greatly appriciates.



--Note to self.
--TJ, YOU FOOL, you aren't making this game very modular with other maps!
--Fix it, and dont make any more map-related functions till you fix it!



-- Define Stuff.
local playerName = "Steve"
local xpGained = 0
local xpLevel = 0
local inventory = {"nil", "nil", "nil", "nil", "nil"}
local weapon = "nil"
local health = 20
local hunger = 20
local armor = 0
local damage = 1,5


-- testMap1.

local testMap1 =  {"[]", "[]", "[]", "  ", "  ",
                   "  ", "  ", "##", "  ", "[]"}

-- testMap1Props

local testMap1Props = {"s", "s", "s", "a", "a",
                       "a", "a", "a", "a", "s"}

-- loads a map

function loadMap(mapProps)
  local loadedMap = mapProps
end

function SetPlayerPos(spot)
  loadedMap[spot] = "##"
end

-- Check location function, checks for players current postition -- in the map.
-- Will probaly change when the way maps work change.

function checkPlayerCoords()
  for i = 1, 10 do
    f = loadedMap[i]
    if f == "##" then
      return(f)
    end
  end
end

-- Movement Function.

function movePlayer(direction) 
  spot = checkPlayerCoords()
  setPlayerPos(spot)
end

-- Prints a Map

function printMap()
  for i = 1, 10 do
    term.write("" ..loadedMap[i])
    if i == 5 then
      print("")
    end
  end
end

-- Update tick functions, Oh gawd, here is where the fun part starts.

function waitOs()
  os.startTimer(0.2)
  event, t, s = os.pullEvent()
  if event == "mouse_click" then
    local mouseX = t
    local mouseY = s
    os.pullEvent("timer")
  elseif event == "key" then
    local pressedKey = t
    print("" ..pressedKey)
    if pressedKey == 200 then
      movePlayer(up)
    elseif pressedKey == 203 then
      movePlayer(left)
    elseif pressedKey == 208 then
      movePlayer(down)
    elseif pressedKey == 205 then
      movePlayer(right)
    end
    os.pullEvent("timer")
  elseif event == "timer" then
    print("TIME!")
  end
  waitOs()
end   


-- Function for middlePrinting, as many suggested...
w, h = term.getSize()

function centerPrint(y, text)
  term.setCursorPos(w/2 - #text/2, y)
  write(text)
end


-- Startup Stuff
local gotPressKeyStartScreen
function startScreenVisual()
  term.clear()
  centerPrint(h/2-2, "R P G   G A M E")
  while true do 
    centerPrint(h/2, "P R E S S   A N Y   K E Y")
    centerPrint(h/2+1, "T O   C O N T I N U E")
    os.startTimer(1)
    event, l = os.pullEvent()
    if event == "key" then
          temporary()
          break
    end
    term.clear()
    centerPrint(h/2-2, "R P G   G A M E")
    os.startTimer(1)
    event, l = os.pullEvent()
    if event == "key" then
      temporary()
      break
    end
  end
end
function temporary()
  term.clear()
  centerPrint(h/2, "Do Stuff!")
end


loadMap(testMap1Props)
printMap()
checkPlayerCoords()
setPlayerPos(9)
loadMap()
printMap()
--waitOs()
--startScreenVisual()
LordIkol #2
Posted 12 April 2013 - 04:51 AM
loadedmap is set local inside the loadmap function so he can not use it in the printmap function

You should define it outside the function already and then use loadmap to set it to sth

Greets loki
Tjakka5 #3
Posted 12 April 2013 - 04:53 AM
loadedmap is set local inside the loadmap function so he can not use it in the printmap function

You should define it outside the function already and then use loadmap to set it to sth

Greets loki

Hm, I thought 'Local' was able to make it useable for ALL the functions…
Anyways, thanks, and I'll see if it works :)/>

EDIT: Okay… so that didnt work :/
Anymore ideas?
LBPHacker #4
Posted 12 April 2013 - 05:31 AM
that didnt work :/
What did you exactly do to make it work?
LordIkol #5
Posted 12 April 2013 - 05:37 AM
ok now im home, will have a look at it and see if i can help. :)/>
LordIkol #6
Posted 12 April 2013 - 08:35 AM
Fixed the code :)/>
you had defined setPlayerpos with capital S but used it with small s
and as mentioned before declared the loaded map outside the function
and added the loadMap argument in the end of file
i set comments where i changed sth

http://pastebin.com/Azn94hq2

greets Loki

edit: updated cause i forgot to remove debug stuff, should work now :D/>
Edited on 12 April 2013 - 06:44 AM
TheOddByte #7
Posted 12 April 2013 - 09:13 AM
May I suggest also that you add a screen clearer?

function cleanScreen()
	 term.clear()
		  term.setCursorPos(1,1)
	 end
Spongy141 #8
Posted 12 April 2013 - 03:30 PM
May I suggest also that you add a screen clearer?

function cleanScreen()
	 term.clear()
		  term.setCursorPos(1,1)
	 end
Yeah it would be nice to add that, but not the best idea to set the Pos back to (1,1) since it might mess up were the player is at, so you might want to have it log where the player is at then clear the screen, so you don't have something mess up.
Tjakka5 #9
Posted 13 April 2013 - 12:07 AM
Wow, I realy tought the problem was at the loadMap function, never thought of the setPlayerPos xD
Anyways, thanks for your help, this game will get a lot of attention this weekend :)/>