Log In  Follow DrMcRobot
 Tables, variables and for loops DrMcRobot Resolved   :: Unfold ::

Hi!

New user here, still trying to find my feet. I have a starter project where I'm trying to draw a Crokinole table, which is a circular board with a circle of pins halfway from the centre to the outer edge.

I have a function that I'm calling from _Init() to set up the pins at the start of the game:

 ```function init_pins(a) --Pins are in a circle, so we init the pins with a number to say how many there are --then figure out based on how many what the angle is between them if they're spaced --equally angle_between_pins=1/a --Now, for each pin, we call the add pin functionality for i=1,a do add(pin,{ --We want the angle from 12 o'clock to this pin specifically pin_angle=angle_between_pins*i, print(pin_angle,16*i,8,3), x=sin(pin_angle)*boardradius/2, y=cos(pin_angle)*boardradius/2, draw=function(self) circfill(self.x,self.y,8,6) end, }) end end```

I am confused as hell, because while trying to debug this, that print(pin_angle) just returns a load of null values. But if I replace print(pin_angle) with print(angle_between_pins*i) (the exact thing I just just pin_angle to) then I get the values I'm expecting. Something about putting those angles into a variable within the table entry is setting it to null.

I've tried variations of syntax to see if I can luck into where I'm going wrong, like self. pin_angle etc., but that throws up errors immediately.

I kinda want to understand what I'm doing wrong, as I expect it'll help clarify in my head how tables work (or more likely, where variables exist in the hierarchy and where I can access them from).

Can anyone help me with where these null values are coming from, please?

P#137644 2023-11-19 21:00

Generated 2023-12-11 09:06:00 | 0.058s | Q:6