Button and Score System help

I’m trying to implement a scoring system but I can’t figure out how I should go about it. I have three questions but let me explain what I’m trying to accomplish first.
In cargo bot (example pic below) you put sprites from the toolbox to the Prog then press the play button and it carries out the order. In my code I have 3 sprites. I want to give them all values, let’s say 1,2 and 3 to be simple. Once I put them in the three big circles I want to press a button and it will add up the value of the three sprites to the score text.

So the questions are
1)how do I give my sprites a value?
2)how do I incorporate a button that waits for you to have all 3 circles filled then becomes selectable?
3)how do I make that selected button add up the 3 values of the sprites while displaying it to the score text?

If anyone has any links or tutorials that would help answer my questions post them , it will be much appreciated.

Less confusing with a visual

function setup()  
    
    displayMode(FULLSCREEN) 
tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0)}    -- position of large circles and occupied
    
tab2={vec2(835,335),vec2(835,265),vec2(835,195),}  -- position of small circles

tab3={vec3(835,335,.5),vec3(835,265,.5),vec3(835,195,.5)}  -- position of sprite and scaler value
   tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
        }
    imageSize = spriteSize(tab4[1])
    tab5={}
    for z=1,#tab4 do
        a=math.random(#tab4)
        table.insert(tab5,tab4[a])
        table.remove(tab4,a)
    end
    scaler = .50
    cnt=0
    loop=0   
end

function draw()   
    background(40, 40, 50) 
    
    sprite(asset.builtin.Cargo_Bot.Play_Button, 400,100)
    
    pushStyle()
    fontSize(30)
    fill(40,40,50) 
    fill(40,40,50)
    popStyle()  
    
    fontSize(30)
    --Score text
    text("0",400,400)
    
         delay()
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)  
            end 
    
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
                   delay()
            end
    for z=1,cnt do
        pushMatrix()
        zLevel(.3)
        sprite(tab5[z],tab3[z].x,tab3[z].y,imageSize*tab3[z].z)    
        popMatrix()    
    end
    end
function delay()
    if cnt<#tab5 then
        loop=loop+1
        if loop>60 then 
            cnt=cnt+1
            loop=0
        end  
    end          
end

function touched(touch)    
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end

@Jarc I think you need to start over. You’ve been doing everything in piecemeal and then adding something new which just adds to the confusion of the code. Figure out what the final code is supposed to do, break it down into smaller chunks, then write code for those small chunks. It’s hard to write code a piece at a time without knowing what the outcome is supposed to be. How you start depends on knowing how you’re supposed to end. Otherwise, you’re going to be constantly changing what’s already been written to get what comes next to work.

@Jarc Here’s some code that does what you want. Once the sprites are in the larger circles, a circle will show above them. Tap the circle and the sum of their values will show. This code is getting quite messy and needs to be rewritten based on what else you’re going to add and what the program is going to do.

function setup()  
    displayMode(FULLSCREEN)
    t={1,2,3}   -- number for each entry in tab4
    -- position of large circles and occupied
    tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0)}
    -- position of small circles
    tab2={vec2(835,335),vec2(835,265),vec2(835,195),}  
    -- name, value, original x,y position, current x,y position, size
    tab4={  {n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Boy,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5}, }
    imageSize = spriteSize(tab4[1].n)
    for z=1,#t do
        a=math.random(#t)
        tab4[z].x1=tab2[t[a]].x
        tab4[z].y1=tab2[t[a]].y
        tab4[z].x2=tab2[t[a]].x
        tab4[z].y2=tab2[t[a]].y
        table.remove(t,a)
    end
    cnt=0
    loop=0   
end

function draw()   
    background(40, 40, 50) 

    sprite(asset.builtin.Cargo_Bot.Play_Button, 400,100)

    pushStyle()
    fontSize(30)
    fill(40,40,50) 
    popStyle()  

    fontSize(30)
    --Score text
    if cc==3 then
        noFill()
        ellipse(400,500,100)
        if showScore then
            fill(255,0,0)
            text(score,400,500)
        end
    end

    delay()
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)  
    end 

    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
    for z=1,cnt do
        pushMatrix()
        zLevel(.3)
        sprite(tab4[z].n,tab4[z].x2,tab4[z].y2,imageSize*tab4[z].s)  
        popMatrix()    
    end  
end

function delay()
    if cnt<#tab4 then
        loop=loop+1
        if loop>60 then 
            cnt=cnt+1
            loop=0
        end  
    end          
end

function touched(touch)    
    if touch.state==BEGAN then
        if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
            showScore=true
        end
        pos=0
        for a,b in pairs(tab4) do
            if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                        touch.y<b.y2+40 then
                b.s=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x2==d.x and b.y2==d.y then
                        d.z=0
                    end
                end
            end
        end
    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab4[pos].x2=tab4[pos].x2+touch.deltaX
        tab4[pos].y2=tab4[pos].y2+touch.deltaY
    end

    if touch.state==ENDED then  
        score=0
        cc=0
        showScore=false
        for a,b in pairs(tab1) do
            if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and 
                    tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and
                    b.z==0 then
                tab4[pos].x2=b.x
                tab4[pos].y2=b.y
                b.z=1
                for c,d in pairs(tab4) do
                    if d.x1~=d.x2 and d.y1~=d.y2 then
                        score=score+d.v
                        cc=cc+1
                    end
                end            
                return
            end            
        end
        tab4[pos].x2=tab4[pos].x1
        tab4[pos].y2=tab4[pos].y1
        tab4[pos].s=.5
    end
end

@dave1707 Thanks Dave! My main project code for this is more organized and I have everything in classes. I just put together this example code so anyone helping me would just have to copy and paste a non-lengthy code once. I already know what I want the final outcome to be ,I sketched it out, but I’ve been working on this first to last. Other that classes and tables how could I make this less messy? A metatable?

@Jarc As long as you know what you’re doing, that’s fine. I’m just saying it messy on my part because I seem to be changing things each time you add something. Writing code for one routine could be totally different for that same routine based on what the whole program is going to do.

Try to keep all your questions about this code in one discussion instead of starting a new discussion for each question. It’s easier to keep track of what’s happening when there’s only one place to look for it.

@dave1707 - just a little note, I use similar code in a little program that I made some time ago but never properly finished. I noticed an effect with the background() command. If you omit it you get a stream of icons along the path of dragging your sprite along the screen, which I also noticed it in your code. I know the background is a convenient way of hiding these sprite iteration but I have always fealt that it is untidy and wasteful. Do you know of any way to avoid this?

Edit: This code has prompted me to finish the program.

@Bri_G I not following your response very well. Are you saying the background command is untidy and wasteful. The whole purpose of the background command is to get the screen ready for the next sequence of drawing. If you don’t clear the screen, then eventually it will just fill up with junk that you see when you drag the icons without clearing the screen. The other way would be to draw a rectangle filling the whole screen before doing all the other draws. So basically, the background command is just a shortcut rect command to fill the whole screen.

@dave1707 - yup, that’s the point - I’ve known about this for ages but just think it’s wasteful accumulating all those images. Like papering over cracks in the wall. If it’s a necessary part of the system fair enough - just wondered if it could be removed.

@Bri_G I still think I’m not understanding your post, accumulating all those images. Nothing is being accumulated when using background(). Each draw cycle is drawing new images, anything from the previous draw cycle is gone. If you use backingMode(RETAINED), then what was drawn in the previous draw cycle is copied to the current draw cycle and every image drawn is kept. When not using background or not using retained mode, then the draw cycle does keep the images, but you also get flickering because of the way draw works.