attempt to index global 'InputTimer2' (a nil value)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

attempt to index global 'InputTimer2' (a nil value)

Post by onedaysnotice »

Once again, inputTimer has given me grief. I've tried to look for the problem for two hours now, and I still can't find what it is :S. (I literally just c/p'ed the same code for player2 from player1, only change the variables). The problem only occurs when I press a or d, so its isolated in the left and right movement of player 2 :(

Sorry for the extremely messy code.

Code: Select all

debug = true
paused = false

function love.load()

   ground = {}
   ground.y = love.graphics.getHeight() - love.graphics.getHeight()/4
   ground.w = love.graphics.getWidth()
   ground.h = love.graphics.getHeight() - ground.y

   player1 = {}
   player1.name = "P1"
   player1.w = 50
   player1.h = 50
   player1.y = ground.y - player1.h
   player1.x = 100
   player1.y_vel_base = -600
   player1.y_vel = player1.y_vel_base
   player1.x_vel = 200
   player1.x_vel2 = player1.x_vel*2
   player1.health = 9001
   player1.state = {
      jumping = "false",
      facing = "right",
      running = "false",
   }

   player2 = {}
   player2.name = "P2"
   player2.w = 50
   player2.h = 50
   player2.y = ground.y - player2.h
   player2.x = 375
   player2.m_base = (player2.x + player2.w)/2
   player2.y_vel_base = -600
   player2.y_vel = player2.y_vel_base
   player2.x_vel = 200
   player2.x_vel2 = player2.x_vel*2
   player2.state = {
      jumping = "false",
      facing = "right",
      running = "false"
   }

   gravity = 1000
   releasekey = "none"

   inputTimer = {}
   inputTimer.l = 0
   inputTimer.r = 0

   inputTimer2 = {}
   inputTimer2.l = 0
   inputTimer2.r = 0

   combo = {}
   combo.p1 = {
      l = "false",
      r = "false"
   }
   
   combo.p2 = {
      l = "false",
      r = "false"
   }

   countdown = 99
   doublejump = 1
   paused = false
end

function love.update(dt)
   if paused == false then
      countdown = countdown - dt
   elseif paused then
      countdown = countdown
   end

   if not paused then
      if love.keyboard.isDown(" ") then
         player1.health = player1.health - 500 * dt
      end
      if love.keyboard.isDown("up") and player1.state.jumping == "false" then
         player1.state.jumping = "true"
      end



      if player1.state.jumping == "true" then
         player1.y = player1.y + player1.y_vel * dt
         player1.y_vel = player1.y_vel + gravity * dt
         if player1.y + player1.h > ground.y then
            player1.y = ground.y - player1.h
            player1.y_vel = player1.y_vel_base
            player1.state.jumping = "false"
         end
      end


      if releasekey == "right" then
         inputTimer.r = inputTimer.r + dt
         if inputTimer.r < 0.3 then
            combo.p1.r = "true"
         else 
            inputTimer.r = 0
            releasekey = "none"
            combo.p1.r = "false"
            combo.p1.l = "false"
         end
      end

      if love.keyboard.isDown("right") then
         player1.x = player1.x + player1.x_vel * dt
         if combo.p1.r == "true" and love.keyboard.isDown("right") then
            player1.x = player1.x + player1.x_vel2 * dt
         else
            inputTimer.r = 0
         end
      end

      if releasekey == "left" then
         inputTimer.l = inputTimer.l + dt
         if inputTimer.l < 0.3 then
            combo.p1.l = "true"
         else 
            inputTimer.l = 0
            releasekey = "none"
            combo.p1.l = "false"
            combo.p1.r = "false"
         end
      end

      if love.keyboard.isDown("left") then
         player1.x = player1.x - player1.x_vel * dt
         if combo.p1.l == "true" and love.keyboard.isDown("left") then
            player1.x = player1.x - player1.x_vel2 * dt
         else
            inputTimer.l = 0
         end
      end


      -- map collision
      if player1.x < 0 then
         player1.x = 0
      end

      if player1.x + player1.w > love.graphics.getWidth() then
         player1.x = love.graphics.getWidth() - player1.w
      end

      --=======determining which direction to face=========--

      player2.m = (player2.x + (player2.x + player2.w))/2
      player1.m = (player1.x + (player1.x + player1.w))/2

      if player1.m < player2.m then
         player1.state.facing = "right"
         player2.state.facing = "left"
      end
      if player1.m > player2.m then
         player1.state.facing = "left"
         player2.state.facing = "right"
      end

   --||===========================================||
   --||=================PLAYER 2==================||
   --||===========================================||   

      if love.keyboard.isDown("m") then
         player2.health = player2.health - 500 * dt
      end
      if love.keyboard.isDown("w") and player2.state.jumping == "false" then
         player2.state.jumping = "true"
      end



      if player2.state.jumping == "true" then
         player2.y = player2.y + player2.y_vel * dt
         player2.y_vel = player2.y_vel + gravity * dt
         if player2.y + player2.h > ground.y then
            player2.y = ground.y - player2.h
            player2.y_vel = player2.y_vel_base
            player2.state.jumping = "false"
         end
      end
      

      if releasekey == "d" then
         inputTimer2.r = InputTimer2.r + dt
         if inputTimer2.r < 0.3 then
            combo.p2.r = "true"
         else 
            InputTimer2.r = 0
            releasekey = "none"
            combo.p2.r = "false"
            combo.p2.l = "false"
         end
      end

      if love.keyboard.isDown("d") then
         player2.x = player2.x + player2.x_vel * dt
         if combo.p2.r == "true" and love.keyboard.isDown("d") then
            player2.x = player2.x + player2.x_vel2 * dt
         else
            InputTimer2.r = 0
         end
      end

      if releasekey == "a" then
         InputTimer2.l = InputTimer2.l + dt
         if InputTimer2.l < 0.3 then
            combo.p2.l = "true"
         else 
            InputTimer2.l = 0
            releasekey = "none"
            combo.p2.l = "false"
            combo.p2.r = "false"
         end
      end

      if love.keyboard.isDown("a") then
         player2.x = player2.x - player2.x_vel * dt
         if combo.p2.l == "true" and love.keyboard.isDown("a") then
            player2.x = player2.x - player2.x_vel2 * dt
         else
            InputTimer2.l = 0
         end
      end


      -- map collision
      if player2.x < 0 then
         player2.x = 0
      end

      if player2.x + player2.w > love.graphics.getWidth() then
         player2.x = love.graphics.getWidth() - player2.w
      end
   end

end

function love.draw()
   love.graphics.setColor(0,0,255)
   love.graphics.rectangle("fill",player1.x,player1.y,player1.w,player1.h)
   love.graphics.setColor(95,95,95)
   love.graphics.rectangle("fill",0,ground.y,ground.w,ground.h)
   love.graphics.setColor(255,0,0)
   love.graphics.rectangle("fill",player2.x,player2.y,player2.w,player2.h)
   love.graphics.setColor(255,255,255)
   love.graphics.print(string.format("%.f", countdown), love.graphics.getWidth()/2-24, 50, 0, 3,3)
   love.graphics.print(string.format("%.f",player1.health), 100,(love.graphics.getHeight() + ground.y)/2,0,3,3)
   if debug then
      love.graphics.print("Jumping: " ..player1.state.jumping,50,50,0,1,1)
      love.graphics.print("y: " ..player1.y,200,50,0,1,1)
      love.graphics.print("x: " ..player1.x,200,65,0,1,1)
      love.graphics.print("last key release: " ..releasekey,50,65,0,1,1)
      love.graphics.print("combo (r): " ..combo.p1.r, 50,80,0,1,1)
      love.graphics.print("combo (l): " ..combo.p1.l, 50,95,0,1,1)
      love.graphics.print("P1 facing: " ..player1.state.facing,50,110,0,1,1)
      love.graphics.print("P2 facing: " ..player2.state.facing,50,125,0,1,1)
      love.graphics.printf(player1.state.facing, player1.m, (player1.y + (player1.y + player1.h))/2, 0,"center")
      love.graphics.printf(player2.state.facing, player2.m, (player2.y + (player2.y + player2.h))/2, 0,"center")
      love.graphics.printf(player1.name, player1.m, (player1.y + (player1.y + player1.h))/2 - 15, 0,"center")
      love.graphics.printf(player2.name, player2.m, (player2.y + (player2.y + player2.h))/2 - 15, 0,"center")
   end
   if paused then
      love.graphics.printf("Paused", love.graphics.getWidth()/2, love.graphics.getHeight()/2, 0, "center")
   end
end

function love.keypressed(key)

end

function love.keyreleased(key)
   if key == "tab" then
      debug = not debug
   elseif key == "right" then
      releasekey = "right"
   elseif key == "left" then
      releasekey = "left"
   elseif key == "return" then
      paused = not paused
   elseif key == "d" then
      releasekey = "d"
   elseif key == "a" then
      releasekey = "a"
   end


end
Attachments
Archive.love
(1.86 KiB) Downloaded 109 times
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: attempt to index global 'InputTimer2' (a nil value)

Post by Lynesth »

Hi again. (Try to keep all your demands in 1 same topic if possible, especially if it's for a similar issue)

Your problem here is that your variable is called "inputTimer" and you're trying to do stuff with "InputTimer" sometimes (I'll let you find out where, shouldn't be hard).
Be careful cause it's case sensitive. :)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: attempt to index global 'InputTimer2' (a nil value)

Post by onedaysnotice »

hi again indeed :D Okay I'll keep that in mind next time :)

omfg... thanks for solving the mystery LOL. that was a real face palm moment for me xD I'll be more cautious next time when using find and replace :D
Post Reply

Who is online

Users browsing this forum: 1Minus2P1Stringer2, Ahrefs [Bot], Google [Bot] and 3 guests