[SOLVED] Variables that have integers being read as nil
Posted: Thu Jan 04, 2024 4:48 pm
So I have a problem where all variables I create that represent integer numbers are being read as nil.
This started happening out of nowhere and I don't know why. Here's the code that seems to have a problem.
The numbers that have this problem are enemy.damage and enemy.attackCooldown, whenever they are called they come back as nil
This is all the situations in which they are called
This started happening out of nowhere and I don't know why. Here's the code that seems to have a problem.
Code: Select all
function CreateEnemy(world, x, y, i)
local enemy = {}
enemy.body = love.physics.newBody(world, x, y, "dynamic")
enemy.shape = love.physics.newRectangleShape(50, 50)
enemy.fixture = love.physics.newFixture(enemy.body, enemy.shape, 2)
enemy.fixture:setUserData({type = "enemy", index = i, ref = enemy})
enemy.fixture:setFriction(1)
enemy.maxvelocity = 90
enemy.body:setFixedRotation(true)
enemy.health = 100
enemy.fixture:setCategory(4)
enemy.fixture:setMask(4)
enemy.triggerBody = love.physics.newBody(world,x,y,"dynamic") -- Creates body for the range of the enemy
enemy.triggerShape = love.physics.newCircleShape(100) -- Creates shape for the range of the enemy
enemy.triggerFixture = love.physics.newFixture(enemy.triggerBody,enemy.triggerShape,0) -- Creates the fixture for the range of the enemy
enemy.triggerFixture:setSensor(true)
enemy.triggerFixture:setUserData({type = "enemyTrigger", ref = enemy})
enemy.attacking = false
enemy.attackingTower = false
enemy.attackingCastle = false
enemy.attackingBarricade = false
enemy.attackCooldown = 2 -- Max cooldown on the attack
enemy.attackTimer = 2 -- Timer of the attack
enemy.towerAttacked = 0
enemy.damage = 100
enemy.joint = love.physics.newRevoluteJoint(enemy.body,enemy.triggerBody,enemy.body:getX(),enemy.body:getY()) -- Joint for connecting the range
return enemy
end
Code: Select all
for i = 1, #enemies, 1 do
if enemies[i].attacking then
enemies[i].attackTimer = enemies[i].attackTimer + dt
if enemies[i].attackTimer >= enemy.attackCooldown then
player.health = player.health - enemy.damage
player.regenTimer = 0
enemies[i].attackTimer = 0
print(enemy.damage)
end
end
if enemies[i].attackingCastle == true then
enemies[i].attackTimer = enemies[i].attackTimer + dt
if enemies[i].attackTimer >= enemy.attackCooldown then
castle.health = castle.health - enemy.damage
print("Oh my gahhh")
enemies[i].attackTimer = 0
end
end
if enemies[i].attackingTower then
print(enemies[i].towerAttacked.health)
if enemies[i].towerAttacked then
enemies[i].attackTimer = enemies[i].attackTimer + dt
if enemies[i].attackTimer >= 2 then
enemies[i].towerAttacked.health = enemies[i].towerAttacked.health - enemy.damage
print("Oh minha torre")
enemies[i].attackTimer = 0
end
end
end
if enemies[i].attackingBarricade then
if enemies[i].towerAttacked then
enemies[i].attackTimer = enemies[i].attackTimer + dt
if enemies[i].attackTimer >= enemy.attackCooldown then
enemies[i].towerAttacked.health = enemies[i].towerAttacked.health - enemy.damage
print("Oh minha barricada")
enemies[i].attackTimer = 0
end
end
end
end