[SOLVED] Variables that have integers being read as nil

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
Artres102
Prole
Posts: 3
Joined: Wed Nov 29, 2023 1:40 pm

[SOLVED] Variables that have integers being read as nil

Post 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
Last edited by Artres102 on Fri Jan 05, 2024 12:57 am, edited 1 time in total.
User avatar
marclurr
Party member
Posts: 105
Joined: Fri Apr 22, 2022 9:25 am

Re: Variables that have integers being read as nil

Post 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.
Artres102
Prole
Posts: 3
Joined: Wed Nov 29, 2023 1:40 pm

Re: Variables that have integers being read as nil

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], pgimeno and 58 guests