0

I was doing a tutorial in pico8 just a while ago when I ran into this problem when I ran the code:

"Attempt to compare nil with number"

It was specifically refering to line 134, so I am assumming that it refering to the "x" as being nil. However, I don't know why this is happening.

Thanks for your help!

function _init()

    --variables
    x=63
    y=110
    xv=1.5
    yv=1.5
    bulx=63
    buly=110
    bulv=0
    bullet_moving = 0 
    ship_sprite=2
    exhaust_sprite=5
    muzzle_flash=0
    score=0
    lives=3
    bombs=3
     
    starx={}
    stary={}
    starspd={}
    for i=1,100 do
        add(starx,rnd(127))
        add(stary,rnd(127))
        add(starspd,rnd(1))
 end
    
end

function _draw()

    cls(0)
    
    background()
    
    spr(16,bulx,buly)
    
    spr(ship_sprite,x,y)
    
    spr(exhaust_sprite, x, y+6)
    
    if muzzle_flash>0 then
        circfill(x+3,y+2,muzzle_flash,7)
    end
    
    print("score: "..score,46,3)
    
    spr(13,1,1)
    
    for i=1,3 do
        if lives>=i then
            spr(11,i*9-6,2)
        else
            spr(12,i*9-6,2)
        end
    end
    
    for i=1,3 do
        if bombs>=i then
            spr(14,i*9+90,2)
        else
            spr(15,i*9+90,2)
        end
    end
    
end

function _update()
    
    --player movement
    
    if btn(0) and not btn(2) and not btn(3) then 
        x=x-xv
        ship_sprite=1
        
        for i=1,#starx do
            if starx[i]<128 then
                starx[i]=starx[i]+starspd[i]
            else
                starx[i]=starx[i]-128
            end
        end
        
 end
    
    if btn(1) and not btn(2) and not btn(3) then 
        x=x+xv
        ship_sprite=3
        
        for i=1,#starx do
            if starx[i]>0 then
                starx[i]=starx[i]-starspd[i]
            else
                starx[i]=starx[i]+128
            end
        end
        
 end
 
 if not btn(1) and not btn(0) then
    ship_sprite=2
    end
    
    if btn(2) then 
        y=y-yv
        
        for i=1,#stary do
            if stary[i]<128 then
                stary[i]=stary[i]+starspd[i]
            else
                stary[i]=stary[i]-128
            end
        end
        
    end
    
    if btn(3) then 
        y=y+yv
        
        for i=1,#stary do
            if stary[i]>0 then
                stary[i]=stary[i]-starspd[i]
            else
                stary[i]=stary[i]+128
            end
        end
    end
        
    end
    
    --player loop
    
    if x<-5 then 
        x=128
    end
    
    if x>132 then 
        x=-1
    end
    
    --shooting
    if btnp(5) and bullet_moving==0 then
        sfx (0)
        muzzle_flash=4
    end 
    
    if btnp(5) or bullet_moving==1 then
        buly=buly-2
        bullet_moving=1
    else
        bulx=x
        buly=y+1
    end
    
    if buly<-5 then 
        bullet_moving=0
        bulx=x
        buly=y+1
    end
    
    if muzzle_flash>0 then
        muzzle_flash=muzzle_flash-1
    end
    
    --ship_exhaust
    if exhaust_sprite>8 then
            exhaust_sprite=5
        elseif btn(2) then
            exhaust_sprite=exhaust_sprite+1
        else
            exhaust_sprite=4

end

function background()

    for i=1,#starx do
        pset(starx[i],stary[i],7)
    end

end

I tried asking chat gpt, but it failed to give a meaningful response. I am expecting for the code to run without this error.

Sam Cao
  • 1
  • 2
  • The mistake is in number of nested `end`. Format the code (apply the correct indentation) to make it obvious. – ESkri Jun 14 '23 at 07:47

1 Answers1

0

As pointed out by @ESkri, when a more correct indentation is applied to this code it shows the code following the --player loop comment is not contained within the _update() function:

function _update()
    --player movement
    --[[ ... ]]
end

--player loop

if x<-5 then
    x=128
end

This means x<-5 will be evaluated when the file is loaded, and x will be nil, as it is not given a value until _init() runs:

function _init()
    --variables
    x=63

You must move the end found above the --player loop comment:

   end --[[ remove this ]]
    
    --player loop

to after exhaust_sprite=4

    else
        exhaust_sprite=4
    end --[[ add this ]]
Oka
  • 23,367
  • 6
  • 42
  • 53