Log In Tables, variables and for loops DrMcRobot Resolved   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  You cannot reference pin_angle from within the table initialisation code.
Table members are not available in the scope.

See fixed code below, where pin_angle is declared before table creation:

 ``` for i=1,a do --We want the angle from 12 o'clock to this pin specifically local pin_angle=angle_between_pins*i print(pin_angle,16*i,8,3) add(pin,{ x=sin(pin_angle)*boardradius/2, y=cos(pin_angle)*boardradius/2, draw=function(self) circfill(self.x,self.y,8,6) end, }) end ```
P#137645 2023-11-19 21:07  Of course. An hour spent banging my head against this, and I figure it out about 90s after posting this.

Turns out that the pin table definition was coming after the point in _init() where I went off to set up the pins, so the whole function was referencing a table that didn't exist yet!

P#137646 2023-11-19 21:10  ... Though now I've seen your post, and all of a sudden that makes a lot more sense than my kludgy fix. Thank you!

P#137669 2023-11-20 10:10