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

Need Help With Objects In A Game

Started by CaosTECH, 24 October 2015 - 06:58 PM
CaosTECH #1
Posted 24 October 2015 - 08:58 PM
I've started from scratch to make a new game, its gonna be like a RPG Survival style game, but I need to make objects. I will put my code here:
local char = ">"
local playerX = 1
local playerY = 1

function drawCharacter()
term.clear()
term.setCursorPos(playerX, playerY)
write(char)
drawMap()
end

function newSolidObject(obx, oby, obj)
term.setCursorPos(obx, oby)
write(obj)
if playerX == obx and char == ">" then
playerX = playerX - 1
elseif playerX == obx and char == "<" then
playerX = playerX + 1
elseif playerY == oby and char == "^" then
playerY = playerY + 1
elseif playerY == oby and char == "v" then
playerY = playerY -1
drawCharacter()
end
end

function drawMap()
term.clear()
newSolidObject(4, 4, "x")

end
while true do
drawCharacter()
drawMap()
local e,key = os.pullEvent( "key" )
if key == 17 or key == 200 then
playerY = playerY -1
char = "^"
elseif key == 31 or key == 208 then
playerY = playerY +1
char = "v"
elseif key == 203 or key == 30 then
playerX = playerX -1
char = "<"
elseif key == 205 or key == 32 then
playerX = playerX +1
char = ">"
elseif key == 15 then
break
end
end


whenever I run it the character shows up, I tried many different combinations but
here is the situations that happen:
The character shows up but the object doesnt
The object shows up but the character doesnt
The character shows up but its trapped in a three by three box, and the object doesnt show up

I would like to know whats wrong, I would also know how to
change the code to where the camera follows the character
HPWebcamAble #2
Posted 24 October 2015 - 09:40 PM
Remember to define functions BEFORE you call them
(eg your drawMap() function is called on line 8, but isn't defined until further down)

Also, if you want to make anything substantial, you'll need to format it correctly (namely indenting).
Although, the strange spacing here may be the forum software's fault. Maybe put it on pastebin instead, to make it easier for us to read?
CaosTECH #3
Posted 24 October 2015 - 10:54 PM
K, fixed the using function before its defined thing, heres the pastebin: http://pastebin.com/nuYSrr9i
HPWebcamAble #4
Posted 24 October 2015 - 11:50 PM
In your 'drawCharacter' function, you clear the screen.

You call that function in 'newSolidObject', after having drawn the object. So it clears the object you just drew.

Since you clear the screen in 'drawMap' anyway, maybe you should just remove the 'term.clear()' in 'drawCharacter'.
CaosTECH #5
Posted 25 October 2015 - 12:40 AM
Ok, that fixes the texture issue, just not the fact that im still stuck in a 3x3 box


Also dont forget, still need help figuring out
how to make the screen follow the character, so
the character doesn't go out the screen.
Edited on 24 October 2015 - 10:52 PM
CaosTECH #6
Posted 25 October 2015 - 01:59 AM
Well, I have figured out the character being stuck in the 3x3 box, but can't fix it still
I tried to figure out how to fix it but it felt as if blowing out my brains with a shotgun :(/>
Still need help with it
HPWebcamAble #7
Posted 25 October 2015 - 03:03 AM
You'll notice you AREN'T stuck in a 3x3 box. You can move up and left as far as you'd like (offscreen)

This code fixes the movement issue (and a few other things, compare it to your current code)
http://pastebin.com/FmcRAN7M


It's really messy (will make it harder to add more things to the game) and really inefficient (not as important)
For example: Instead of having the newSolidObject function make sure the character isn't inside of that object, you should probably have a table of the objects,
and before moving the character, make sure that you won't be moving into an object. That would go in the if-block that waits for key presses.

Not that I'm the perfect programmer, but you could take a look at some of my programs.
(Maybe my File Manager?)
CaosTECH #8
Posted 25 October 2015 - 12:38 PM
(Angels Singing) Thx for the help dude! It draws the character and the object that is solid. Im able to go anywhere on the map now and Im gonna beable to make my game, just need one more thing that I need help with

Having the screen follow the character, Kinda like in Miniaturecraft how it follows the character.


EDIT:
Checked out your FileManager, i now use that as my main OS, you know, the startup program and stuff. Its pretty awesome, not to complicated and not to laggy/buggy
Edited on 25 October 2015 - 11:43 AM
CaosTECH #9
Posted 25 October 2015 - 05:24 PM
I was making my game and making new objects, I tried to make a small hut but when I ran the code (I always test code) I got an error on line 18 "index expected, got nil" But I just don't understand why.
I will put my code on here and on pastebin

Code:
local ScreenWidth, ScreenHeight = term.getSize()
local char = ">"
local playerX = math.floor(ScreenWidth / 2)
local playerY = math.floor(ScreenHeight / 2)
local offsetX, offsetY = 0, 0
local map = {}
function drawCharacter()
  term.setCursorPos(playerX - offsetX, playerY - offsetY)
  write(char)
end

function newObject(x, y, obj)
  map[y] = map[y] or {}; map[y][x] = {obj}
end

function newSolidObject(x, y, obj)
  map[x] = map[y] or {}; map[y][x] = {obj, true}
end
function checkForCollision(x, y)
  return map[y] and map[y][x] and map[y][x][2]
end

function drawMap()
  term.clear()
  for y = 1, ScreenHeight do
    if map[y + offsetY] then
	  for x = 1, ScreenWidth do
	    if map[y + offsetY][x + offsetX] then
		  term.setCursorPos(x, y)
		  term.write(map[y + offsetY][x + offsetX][1])
	    end
	  end
    end
  end
  drawCharacter()
end
newSolidObject(4, 5, "x")
newSolidObject(6, 6, "x")
newSolidObject(5, 5, "x")

newObject(7, 7, "w")
newObject(8, 8, "w")
newObject(8, 7, "w")

while true do
  drawMap()
  local e, key = os.pullEvent("key")
  if (key == 17 or key == 200) then
    if not checkForCollision(playerX, playerY - 1) then
	  offsetY = offsetY - 1; playerY = playerY - 1
    end; char = "^"
  elseif (key == 31 or key == 208) then
    if not checkForCollision(playerX, playerY + 1) then
	  offsetY = offsetY + 1; playerY = playerY + 1
    end; char = "v"
  elseif (key == 203 or key == 30) then
    if not checkForCollision(playerX - 1, playerY) then
	  offsetX = offsetX - 1; playerX = playerX - 1
    end; char = "<"
  elseif (key == 205 or key == 32) then
    if not checkForCollision(playerX + 1, playerY) then
	  offsetX = offsetX + 1; playerX = playerX + 1
    end; char = ">"
  elseif key == 15 then
    break
  end
end

Heres the pastebin also: http://pastebin.com/Xyg8Yqcz
Lyqyd #10
Posted 25 October 2015 - 05:51 PM
Threads merged. Please stick to one topic for all questions about a given program.
KingofGamesYami #11
Posted 25 October 2015 - 05:53 PM
I did some testing, and the part where you do map[ map[ 5 ] ][ 4 ] = map[ 4 ] or {} is confusing the heck out of me. I have no idea why you'd ant to do such a thing.

Anyway, the problem is map[ 5 ] and map[ 4 ] are nil, so the expression becomes map[ nil ][ 4 ] = {}. Lua doesn't like it when you index a table with nil.
CaosTECH #12
Posted 25 October 2015 - 06:13 PM
I did some testing, and the part where you do map[ map[ 5 ] ][ 4 ] = map[ 4 ] or {} is confusing the heck out of me. I have no idea why you'd ant to do such a thing.

Anyway, the problem is map[ 5 ] and map[ 4 ] are nil, so the expression becomes map[ nil ][ 4 ] = {}. Lua doesn't like it when you index a table with nil.

Ok, so how do I fix it?
KingofGamesYami #13
Posted 25 October 2015 - 06:32 PM
I dunno. The function is so broken I can't tell what it's supposed to do.

If it's supposed to add something to the map, why is it using another part of the map as an index?
CaosTECH #14
Posted 25 October 2015 - 07:00 PM
Ok, well that doesn't help much. I've tried other ways but this is the only thing that doesn't break the game, but now instead it still breaks whenever I try making parallel solid objects, Can someone else please help?
Edited on 25 October 2015 - 06:00 PM
Lyqyd #15
Posted 25 October 2015 - 07:24 PM
Try explaining in detail what the function is supposed to do. Go through each line of the function and explain what you were trying to do. Once we understand what the function is supposed to be doing, it will be a lot easier for people to help you fix it. Basically, make sure you're asking good questions.
CaosTECH #16
Posted 25 October 2015 - 09:38 PM
I will try to explain the problem as best as possible::::::::::: Also, im a complete noob at this type of programming, but I could still figure out what the functions do when I first looked at them :P/>

local ScreenWidth, ScreenHeight = term.getSize()
local char = ">"
local playerX = math.floor(ScreenWidth / 2)
local playerY = math.floor(ScreenHeight / 2)
local offsetX, offsetY = 0, 0
local map = {}
function drawCharacter()
  term.setCursorPos(playerX - offsetX, playerY - offsetY)
  write(char)
end
function newObject(x, y, obj)
  map[y] = map[y] or {}; map[y][x] = {obj}
end
---------------------------------------------   The
---------------------------------------------   Problem
---------------------------------------------   Area
function newSolidObject(x, y, obj)
  map[x] = map[y] or {}; map[y][x] = {obj, true}  ---------------- Refer to the below comment
--[[
Its letting the user give coordinates on the x and y plane nomatter what the coordinate is (e.g. -57, -73 or 74, 1)
and placing the character there, which is the obj (object). The true value is saying if the players character can
go into the objects (where the exact X and Y coordinate is) x and y area. I tried to explain as best as possible, im
not good at explaing things

--]]
end
---------------------------------------------
---------------------------------------------
---------------------------------------------
function checkForCollision(x, y)
  return map[y] and map[y][x] and map[y][x][2]
end
function drawMap()
  term.clear()
  for y = 1, ScreenHeight do
	if map[y + offsetY] then
		  for x = 1, ScreenWidth do
			if map[y + offsetY][x + offsetX] then
				  term.setCursorPos(x, y)
				  term.write(map[y + offsetY][x + offsetX][1])
			end
		  end
	end
  end
  drawCharacter()
end
newSolidObject(4, 5, "x")
newSolidObject(6, 6, "x")
newSolidObject(5, 5, "x")
newObject(7, 7, "w")
newObject(8, 8, "w")
newObject(8, 7, "w")
while true do
  drawMap()
  local e, key = os.pullEvent("key")
  if (key == 17 or key == 200) then
	if not checkForCollision(playerX, playerY - 1) then
		  offsetY = offsetY - 1; playerY = playerY - 1
	end; char = "^"
  elseif (key == 31 or key == 208) then
	if not checkForCollision(playerX, playerY + 1) then
		  offsetY = offsetY + 1; playerY = playerY + 1
	end; char = "v"
  elseif (key == 203 or key == 30) then
	if not checkForCollision(playerX - 1, playerY) then
		  offsetX = offsetX - 1; playerX = playerX - 1
	end; char = "<"
  elseif (key == 205 or key == 32) then
	if not checkForCollision(playerX + 1, playerY) then
		  offsetX = offsetX + 1; playerX = playerX + 1
	end; char = ">"
  elseif key == 15 then
	break
  end
end
Edited on 25 October 2015 - 08:39 PM
KingofGamesYami #17
Posted 25 October 2015 - 10:52 PM
Ok, I think I understand the problem now… line 18 is flawed:

Your line 18:

map[ x ] = map[ y ] or {}; map[ y ][ x ] = {obj, true}

Two fixed line 18s:

map[ x ] = map[ x ] or {}; map[ x ][ y ] = {obj, true}

map[ y ] = map[ y ] or {}; map[ y ][ x ] = {obj, true}

Based on the newObject function, I would say you want the second version.
CaosTECH #18
Posted 26 October 2015 - 12:01 AM
Ok, the issue is that im just very stupid, it was a very simple mistake yet I look for the most complex answers. Sorry for any frustration I caused.
Edited on 26 October 2015 - 02:20 AM