Page 1 of 1

[SOLVED] Variables that have integers being read as nil

Posted: Thu Jan 04, 2024 4:48 pm
by Artres102
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.

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
The numbers that have this problem are enemy.damage and enemy.attackCooldown, whenever they are called they come back as nil

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
This is all the situations in which they are called

Re: Variables that have integers being read as nil

Posted: Thu Jan 04, 2024 5:37 pm
by marclurr
You're accessing those fields like this:

Code: Select all

enemy.damage
...
enemy.attackCooldown
But you never assign enemy in your loop. You probably have a global table called enemy that you're referencing instead.

Re: Variables that have integers being read as nil

Posted: Fri Jan 05, 2024 12:56 am
by Artres102
Now that I reread the code in a calmer manner I saw the mistake and you're right. My brain was on auto-pilot since I had just changed similar stuff on other files where things were being declared the same as their main table. Thank you for the help!