Hi everyone!
I'm making a game where two players try to knock their opponent's balls off a table. Functionally it works just like pool, except instead of a cue ball you pick one of your balls on the table to shoot.
I'm stumped trying to make a game state where you can cycle through the balls you have on the table to pick which one you shoot.
I want to collect the xy of all p1's balls in a table, just once, each time p1 starts their turn. Is it possible to do that in the update function?
Currently my code looks like this, but it's not working as intended because it just keeps adding to the table infinitely as long as a ball meets the conditions:
function ballselect() foreach(balls, playerballs) end function playerballs(pb) if pb.ontable==true then --check ball still in play if player==1 then --check player if pb.c==12 then --check ball is p1 colour add(bc1,{ x=pb.x, y=pb.y, n=count(bc1) --assign number to cycle thru? }) end end end end |
Any advice/ideas on how I can get this to work?
Thanks in advance!



Before collecting the balls, initialize bc1 to an empty table.
function ballselect() bc1={} foreach(balls, playerballs) end |



The most straight-forward way to make a variable, such as balls_sorted, that gets set to false when it becomes a new player's turn. Then you can check it at the beginning of the update, run the sort only if it's false, and then set it to true after you've done that task.
[Please log in to post a comment]