Page 1 of 1

[solved] Code Freezes when requiring classes

Posted: Mon May 18, 2015 1:37 pm
by ElmuKelmuZ
EDIT2:

It seems that I made a class inherit itself, so it probably got stuck in a never ending loop of recursion, or something along those lines
Issue fixed now!


EDIT:
I have narrowed the issue down, and it seems the code hangs at requiring the classes. I have no clue why it happens.


The project spans multiple files, so I attached the .love -file.
Also here's the main.lua if you cant be bothered to download the attachment:

Code: Select all

--Load required modules
local Orakel = require("Orakel")

--Load required classes
local Player = require("System/Player")
local Enemy = require("System/Enemy")
local StatTrack = require("System/StatTracker")
local Projectile = require("System/Projectile")

--Debug vars
local printSafeErrors = false --This is not even remotely useful, don't use
local allowModifications = false

--Game vars
local bulletSpeed = 500
local playerSpeed = 200


--Internal stuff
local loadedMods = {}
local drawList = {}
local bullets = {}

local player = nil
local canShoot = true
local canShootTimerMax = 0.2 
local canShootTimer = canShootTimerMax




local function loadModification(mod)
  local file = mod:gsub(".lua", "")
  mod = require("Mods/"..file)
  table.insert(loadedMods, mod)
end


local function loadModifications()
  local mods = love.filesystem.getDirectoryItems("Mods")
  for _, mod in pairs(mods) do
    print("Loading mod '"..mod.."'")
    local modSuccess, modError = pcall(loadModification, mod)
    if not modSuccess then
      error("Failed to load mod '"..mod.."'\nPlease contact the mod author or disable it if you keep running into this same error\n\nFurther Details:\n"..modError, 0)
    end
  end
end



local function safeExecute(f, ...)
  local status, err = pcall(f, ...)
  if printSafeErrors and not status then
    error("Function "..tostring(f).." encountered an exception! Additional Information:\n\n"..err, 0)
  end
end


function love.load()
  print("Loading Stuff")
  playerSaveDir = love.filesystem.getSaveDirectory()
  print("Saves dir: "..playerSaveDir)
  print("Loading Textures")
  spr_player = love.graphics.newImage("Assets/player.png")
  spr_player_f = love.graphics.newImage("Assets/player_muzzle.png")
  spr_enemy = love.graphics.newImage("Assets/enemy.png")
  spr_enemy_f = love.graphics.newImage("Assets/enemy_muzzle.png")
  spr_bullet = love.graphics.newImage("Assets/bullet.png")
  love.graphics.setBackgroundColor(255, 255, 255)
  print("Textures Loaded")
  print("Creating player")
  player = Player.Create(128, 128, spr_player, playerSpeed)
  print("Created player")
  if allowModifications then
    loadModifications()
    for _, mod in pairs(loadedMods) do
      safeExecute(mod.load)
    end
  end
  print("Loaded successfully")
end



function love.update(dt)
  if player ~= nil then
    if love.keyboard.isDown("left","a") then
      if player.x > 0 then -- binds us to the map
        player.x = player.x - (player.Speed * dt)
      end
    end
    if love.keyboard.isDown("right", "d") then
      if player.x < (love.graphics.getWidth() - player.Sprite:getWidth()) then
        player.x = player.x + (player.Speed * dt)
      end
    end
    if love.keyboard.isDown("up", "w") then
      if player.y < (love.graphics.getHeight() - player.Sprite:getWidth()) then
        player.y = player.y - (player.Speed * dt)
      end
    end
    if love.keyboard.isDown("down", "s") then
      if player.y < (love.graphics.getHeight() - player.Sprite:getWidth()) then
        player.y = player.y + (player.Speed * dt)
      end
    end
    
    if love.keyboard.isDown(' ') and player.Ammo > 0 and canShoot then
      player.Ammo = player.Ammo - 1
      player.Sprite = spr_player_f
      local bullet = Projectile.Create(player.x, player.y + (player.Texture:getHeight()/2), spr_bullet, 350, "Bullet")
      table.insert(bullets, bullet)
      canShootTimer = canShootTimerMax
    else
      player.Sprite = spr_player
    end
    
    canShootTimer = canShootTimer - (1 * dt)
    if canShootTimer < 0 then
      canShoot = true
    end
  end
  
  for i, bullet in ipairs(bullets) do
    bullet.y = bullet.y - (bulletSpeed * dt)
    if bullet.y < 0 or bullet.x < 0 or bullet.y > love.graphics.getHeight() or bullet.x > love.graphics.getWidth()then -- remove bullets when they pass off the screen
      table.remove(bullets, i)
    end
  end
  
  for _, mod in pairs(loadedMods) do
    safeExecute(mod.update)
  end
end



function love.draw()
  if player.Health > 0 then
    love.graphics.draw(player.Sprite, player.x, player.y)
  end

  for _, mod in pairs(loadedMods) do
    safeExecute(mod.draw)
  end
  
  for i, bullet in ipairs(bullets) do
    love.graphics.draw(bullet.img, bullet.x, bullet.y)
  end
end