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

Need Npc Help For My Rpg

Started by Ziriee, 25 January 2013 - 03:50 AM
Ziriee #1
Posted 25 January 2013 - 04:50 AM
I need help for my new RPG game. I tried implementing NPCs, but they seem buggy. Can anyone
please take a look at it? :)/>


Link:
http://pastebin.com/x3WDk4Va

NPC functions:

function drawNPC(nx,ny,npos,nchar,nmode)
  npcon = true
  npcx = nx
  npcy = ny
  npcchar = nchar
  npcmode = nmode
  npcpos = npos
  drawMap()
end
function updateNPC()
  if npcmode == "dumb" then
	turn = math.random(1,4)
if (turn == 1) and (map[npcpos-9] == "-") then
   npcpos = npcpos-9
   npcy = npcy-1
elseif (turn == 2) and (map[npcpos+9] == "-") then
   npcpos = npcpos+9
   npcy = npcy+1
elseif (turn == 3) and (map[npcpos-1] == "-") then
   npcpos = npcpos-1
   npcy = npcx-1
elseif (turn == 4) and (map[npcpos+1] == "-") then
   npcpos = npcpos+1
   npcy = npcx+1
end
  end
end
ikke009 #2
Posted 25 January 2013 - 05:00 AM
what exactly is "buggy" about them?
Ziriee #3
Posted 25 January 2013 - 05:13 AM
what exactly is "buggy" about them?

I put the updateNPC() in the move section, so when you move, the NPC updates, but when I run it and walk, the NPC is really buggy and just jumps up and down 1 - 3 pixels away. Any help possible?
ikke009 #4
Posted 25 January 2013 - 05:48 AM
Is your npc perhaps in a spot where it can only move up and down? i believe it should do what you want if you put it in the move function..
But I have to say that code could be a lot less complicated, if you for instance have the (drawable) map in a table with metatables for every line, you could easily check what is above or under the npc..ill pm you a piece of code that will probably help you understand what i mean.
Ziriee #5
Posted 25 January 2013 - 06:24 AM
Is your npc perhaps in a spot where it can only move up and down? i believe it should do what you want if you put it in the move function..
But I have to say that code could be a lot less complicated, if you for instance have the (drawable) map in a table with metatables for every line, you could easily check what is above or under the npc..ill pm you a piece of code that will probably help you understand what i mean.
It for example simply checks if the block type above it is "-" ((map[npcpos-9])), and then changes the npcpos to npcpos-9 and npcy to npcy-1. I have no idea why does'n it work..
Ziriee #6
Posted 25 January 2013 - 06:28 AM
Is your npc perhaps in a spot where it can only move up and down? i believe it should do what you want if you put it in the move function..
But I have to say that code could be a lot less complicated, if you for instance have the (drawable) map in a table with metatables for every line, you could easily check what is above or under the npc..ill pm you a piece of code that will probably help you understand what i mean.
Found a bug: I wrote "npcy = npcx-1" and "npcy = npcx+1" :)/> Now the NPC works!! :o/>
ikke009 #7
Posted 25 January 2013 - 07:13 AM
Ah, should've seen that xD But still, you're making this way too hard for yourself