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

Why is my program not executing everything?

Started by Tjakka5, 05 May 2013 - 11:45 AM
Tjakka5 #1
Posted 05 May 2013 - 01:45 PM
Hey guys,

As you might (MIGHT) know, I'm working on a gigant RPG game.
Now, I was trying to (finally) import the saving and loading of the game, it suddenly didnt execute the last few lines.
Help?

The code:
Watch out, its over 450 lines!
Spoiler

-- Define Stuff.
local playerName = "-YourName-"
local xpGained = 0
local xpLevel = 5
local sword = "as"
local armorEq = "le"
local health = 20
local mana = 20
local armor = 0
local guySpeaking = "Tjakka5"local allVariables = {}
allVariables.playerName = playerName
allVariables.xpGained = xpGained
allVariables.xpLevel = xpLevel
allVariables.sword = sword
allVariables.armorAq = armorEq
allVariables.health = health
allVariables.mana = mana
allVariables.armor = armor
local tableOfVariables = textulis.serialize(allVariables)
local loadedMap = {}  --set loadedmap as empty table
local loadedProps = {}
local playerSpot
local loadedMapName
-- testMap1.local testMap1 =  {"[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "[]", "  ", "  ", "  ", "[]", "  ", "[]",
				   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
				   "[]", "  ", "[]", "  ", "  ", "  ", "[]", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",}
local testMap2 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",	  
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]"}	local testMap3 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",	  
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}	
local testMap4 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",	  
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}	local testMap5 =  {"[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",	  
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
				   "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}	
function saveAll()
  local file = fs.open(RPG_Game_SaveFile, "w")
  file.write(tableOfVariables)
  file.close()
endfunction gameMenu()
  term.clear()
  term.setCursorPos(1, 1)
  print("+------------------------------------------------+")
  print("| OPTIONS									    |")
  print("|											    |")
  print("|											    |")
  print("|				    Resume					  |")
  print("|				    Save					    |")
  print("|				    Load					    |")
  print("|				    Exit					    |")
  print("|											    |")
  print("+------------------------------------------------+")
  inOptionScreen = 1
  while inOptionScreen == 1 do
    event, button, x, y = os.pullEvent("mouse_click")
    if (x == 22) or (x == 23) or (x == 24) or (x == 25) or (x == 26) or (x == 27) then
	  if y == 5 then
	    inOptionScreen = 0
	  elseif y == 6 then
	    print("Save")
	  elseif y == 7 then
	    print("Load")
	  elseif y == 8 then
	    print("Exit")
	  end
    end
  end
  waitOs()
end
function printTalk()
  term.setCursorPos(20, 11)
  print("  " ..guySpeaking)
  term.setCursorPos(20, 12)
  print("+-----------------------------+")
  term.setCursorPos(20, 13)
  print("| Hey Guys!				   |")
  term.setCursorPos(20, 14)
  print("| This part is not quite done |")
  term.setCursorPos(20, 15)
  print("| but here you can see what   |")
  term.setCursorPos(20, 16)
  print("| the people have fun.	    |")
  term.setCursorPos(20, 17)
  print("| Stay Tuned!				 |")
  term.setCursorPos(20, 18)
  print("+-----------------------------+")
endfunction checkStats()
  if armorEq == "no" then
    armor = 0
  elseif armorEq == "le" then
    armor = 1
  end
end
function printInv()
  term.setCursorPos(20, 1)
  print("+-----------------------------+")
  term.setCursorPos(20, 2)
  print("|							 |")
  term.setCursorPos(20, 3)
  term.write("| Sword:  ")
 
  if sword == "bs" then
    print("Basic Sword		 |")
    term.setCursorPos(20, 4)
    term.write("| This sword does basic damage|")
 
  elseif sword == "as" then
    print("Advanced Sword	  |")
    term.setCursorPos(20, 4)
    term.write("| Just a bit sharper...	   |")
 
  elseif sword == "bk" then
    print("Basic Knife		 |")
    term.setCursorPos(20, 4)
    term.write("| Grose, it has blood on it!  |")
 
  elseif sword == "ls" then
    print("Long Sword		  |")
    term.setCursorPos(20, 4)
    term.write("| Distant killing!		    |")
  end
 
  term.setCursorPos(20, 5)
  print("|							 |")
  term.setCursorPos(20, 6)
  term.write("| Armor:  ")
 
  if armorEq == "no" then
    print("No armor equiped    |")
    term.setCursorPos(20, 7)
    term.write("| You're naked!			   |")
  elseif armorEq == "le" then
    print("Leather armor	   |")
    term.setCursorPos(20, 7)
    term.write("| Warm, cozy, weak.		   |")
  end

  term.setCursorPos(20, 8)
  print("|							 |")
  term.setCursorPos(20, 9)
  print("+-----------------------------+")
end
  
function printStats()
  term.setCursorPos(1, 11)
  print("  " ..playerName)
  print("+----------------+")
  print("|			    |")
  term.write("| Health:  " ..health)
  if health > 9 then
    print("    |")
  else
    print("	 |")
  end
  term.write("| Mana:    " ..mana)
  if mana > 9 then
    print("    |")
  else
    print("	 |")
  end
  print("| Armor:   " ..armor.. "	 |")
  term.write("| xpLvl:   " ..xpLevel)
  if xpLevel > 9 then
    print("    |")
  else
    print("	 |")
  end
  print("+----------------+")
end
function loadMap(map)
  for i = 1, 81 do
  
    if map == testMap1 then
	  loadedMapName = "testMap1"
	  loadedMap[i] = testMap1[i]
	  setProps(testMap1)
  
    elseif map == testMap2 then
	  loadedMapName = "testMap2"
	  loadedMap[i] = testMap2[i]
	  setProps(testMap2)
   
    elseif map == testMap3 then
	  loadedMapName = "testMap3"
	  loadedMap[i] = testMap3[i]
	  setProps(testMap3)
	 
    elseif map == testMap4 then
	  loadedMapName = "testMap4"
	  loadedMap[i] = testMap4[i]
	  setProps(testMap4)
   
    elseif map == testMap5 then
	  loadedMapName = "testMap5"
	  loadedMap[i] = testMap5[i]
	  setProps(testMap)
    end
  end
end

function setProps(map)
  for i = 1, 81 do
    if map == testMap1 then
	  o = testMap1[i]
    elseif map == testMap2 then
	  o = testMap2[i]
    elseif map == testMap3 then
	  o = testMap3[i]
    elseif map == testMap4 then
	  o = testMap4[i]
    elseif map == testMap5 then
	  o = testMap5[i]
    end 
	 
    if o == "[]" then
	  loadedProps[i] = "s"
    elseif o == "  " then
	  loadedProps[i] = "a"
    elseif o == "o/" then
	  loadedProps[i] = "p"
    end
  end
end
									 
function printMap()
  term.clear()
  term.setCursorPos(1, 1)
  for i = 1, 81 do
    term.write("" ..loadedMap[i])
    if (i == 9) or (i == 18) or (i == 27) or (i == 36) or (i == 45) or (i == 54) or (i == 63) or (i == 72) then
	  print("")
    end																		  
  end
endfunction checkPlayerPos()
  for i = 1, 81 do
    o = loadedProps[i]			   
    if o == "p" then
	  playerSpot = i
	  return(playerSpot)	 
    end
  end
end							   
function checkValidSpot(direction)
  playerSpot = checkPlayerPos()
  if direction == "up" then
    if loadedProps[playerSpot-9] == "a" then
	  return(true)
    else
	  return(false)
    end
  elseif direction == "down" then
    if loadedProps[playerSpot+9] == "a" then
	  return(true)
    else
	  return(false)	 
    end
  elseif direction == "left"then
    if loadedProps[playerSpot-1] == "a" then
	  return(true)
    else
	  return(false)
    end
  elseif direction == "right" then
    if loadedProps[playerSpot+1] == "a" then
	  return(true)
    else
	  return(false)
    end
  end
end
																															   
function movePlayer(direction)
  valid = checkValidSpot(direction)																																																																 
  playerPos = checkPlayerPos()
  if valid == true then
    loadedMap[playerPos] = "  "
    loadedProps[playerPos] = "a"
    if direction == "up" then
	  loadedMap[playerPos-9] = "o/"
	  loadedProps[playerPos-9] = "p"
    elseif direction == "down" then
	  loadedMap[playerPos+9] = "o/"
	  loadedProps[playerPos+9] = "p"
    elseif direction == "left" then
	  loadedMap[playerPos-1] = "o/"
	  loadedProps[playerPos-1] = "p"
    elseif direction == "right" then
	  loadedMap[playerPos+1] = "o/"
	  loadedProps[playerPos+1] = "p"
    end
  end
endfunction checkTp()
  playerPos = checkPlayerPos()
  if loadedMapName == "testMap1" then
    if playerPos == 5 then
	  loadMap(testMap2)
	  loadedMap[68] = "o/"
	  loadedProps[68] = "p"
    end
    if playerPos == 45 then
	  loadMap(testMap3)
	  loadedMap[38] = "o/"
	  loadedProps[38] = "p"
    end
    if playerPos == 37 then
	  loadMap(testMap4)
	  loadedMap[44] = "o/"
	  loadedProps[44] = "p"
    end
    if playerPos == 77 then
	  loadMap(testMap5)
	  loadedMap[14] = "o/"
	  loadedProps[14] = "p"
    end
  end
  if loadedMapName == "testMap2" then
    if playerPos == 77 then
	  loadMap(testMap1)
	  loadedMap[14] = "o/"
	  loadedProps[14] = "p"
    end
  end
  if loadedMapName == "testMap3" then
    if playerPos == 37 then
	  loadMap(testMap1)
	  loadedMap[44] = "o/"
	  loadedProps[44] = "p"
    end
  end
  if loadedMapName == "testMap4" then
    if playerPos == 45 then
	  loadMap(testMap1)
	  loadedMap[38] = "o/"
	  loadedProps[38] = "p"
    end
  end
  if loadedMapName == "testMap5" then
    if playerPos == 5 then
	  loadMap(testMap1)
	  loadedMap[68] = "o/"
	  loadedProps[68] = "p"
    end
  end
end
-- Update tick functions, Oh gawd, here is where the fun part starts.function waitOs()
  os.startTimer(0.15)
  event, t, s = os.pullEvent()
  if event == "mouse_click" then
    local mouseX = t
    local mouseY = s
    os.pullEvent("timer")
  elseif event == "key" then
    pressedKey = t
    if (pressedKey == 200) or (pressedKey == 17) then
	  movePlayer("up")
    elseif (pressedKey == 203) or (pressedKey == 30) then
	  movePlayer("left")
    elseif (pressedKey == 208) or (pressedKey == 31) then
	  movePlayer("down")
    elseif (pressedKey == 205) or (pressedKey == 32) then
	  movePlayer("right")
   
    elseif pressedKey == 28 then
	  gameMenu()
    end
   
    os.pullEvent("timer")
  end
  checkTp()
  printMap()
  checkStats()
  printStats()
  printInv()
  printTalk()
  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
print("Baaad")
loadMap(testMap1)
loadedMap[68] = "o/"
loadedProps[68] = "p"
waitOs()

Or get it in your game from here:
http://pastebin.com/Fjv8e5eA

The problem is basicly the last few lines, where I put

print("Baaad")
to test, however, it aint printing it.

Also, just so you know, the waitOs() does aaaaal the work.
Lyqyd #2
Posted 16 July 2013 - 01:36 PM
There is some weird stuff going on with line breaks in this code (like numerous endfunction() instances), so you may want to verify that the code is exactly as it appears here. This may be why you hadn't received any help previously.
immibis #3
Posted 16 July 2013 - 05:28 PM
Not the problem, but here:

if map == testMap1 then
          o = testMap1[i]
    elseif map == testMap2 then
          o = testMap2[i]
    elseif map == testMap3 then
          o = testMap3[i]
    elseif map == testMap4 then
          o = testMap4[i]
    elseif map == testMap5 then
          o = testMap5[i]
    end 
why not do this?

    o = map[i]

Bubba #4
Posted 16 July 2013 - 06:01 PM
Have you checked that a term.clear() is not called after you print "baaad"? That is the most likely issue. I have not looked at the entirety of your program yet, but you may find that more quickly than I do.
Bubba #5
Posted 16 July 2013 - 06:05 PM
Indeed, I found it. You call gameMenu through the waitOs function. gameMenu clears the screen every 0.15 seconds. So you actually are printing "baaad", but it is being quickly cleared away by gameMenu.