Log In  

Okay, so I've been working on a cart as of late, and it's been really fun! However, I've hit a roadblock and I'm not sure how to overcome it, could I have some help?
So the game I'm making is a little factory game, and I'm currently trying to get conveyor belts working, specifically getting objects to stop when they run into each other.
I have two tables for co-ordinates; one for x-values of objects, and one for y-values. There are some subtables for certain objects, but that's not important.
Basically, my collision function checks those tables, sees if there's anything in the way, and if there isn't, it moves the object forward. However, whenever it checks the tables, it checks the entire X table, and then the entire Y table. So if there's an object that's in the obstruction area in the Y table and a separate object in the X table, the code thinks there's something in the way, and it stops the object.
So the million-dollar question is: Is there a way to have for functions stepped through one at a time, so as to keep the X and Y co-ordinates in sync? Or is there a better way? Let me know if ya'll need clarification or code examples.

P#135276 2023-10-02 14:11

i don't fully understand your problem, but maybe this could help:

typ=split"a,b,c,d"

x=split"1,2,3,4"
y=split"5,6,7,8"

for nb,t in pairs(typ) do
 ?t.." on "..x[nb].." "..y[nb] 
end

?"----"
for nb=1,#typ do
 ?typ[nb].." on "..x[nb].." "..y[nb] 
end
P#135280 2023-10-02 15:19

Sorry, but my issue is a bit more complicated than that. I want to iterate through two separate tables at the same time; I want to get the first value from the first table and the first value from the second table, run some separate code using both of the values, and move to the second values of each table, etc etc. Is there a way to do that? It's also important that the values are kept in order, so using pairs won't work.

P#135283 2023-10-02 15:34 ( Edited 2023-10-02 15:35)

Given table names t1 and t2:

for i=1,#t1 do
  v1,v2 = t1[i],t2[i]
  -- run code using the values.
end

If it's possible that the two tables have different lengths, min(#t1, #t2) can be used to take the smaller length. Alternatively, something like v1,v2 = t1[i] or 0, t2[i] or 0 can be used to ensure that any math is still valid.

P#135290 2023-10-02 17:56 ( Edited 2023-10-02 17:57)
1

It might be easier to have a single table, each element of which is a table holding the x and y coordinates of each object. Something like this...

objects = {
  {x=10,y=2},
  {x=1,y=24},
  {x=3,y=12}
}
for i = 1,#objects do
  for j = i+1,#objects do
    if collide(objects[i],objects[j]) then
        -- stop i and j
    end
  end
end
P#135293 2023-10-02 18:18 ( Edited 2023-10-02 18:18)

[Please log in to post a comment]