Notational Issue

…and it’s driving me nuts! 8-}

I have a class called Board which holds some metadata and a two-dimensional array to hold Tile objects. Let’s say that a Tile object holds a table of values, one of which is t.

As I iterate through my table, should not

board.array[i][j] = Tile.init() where the init sets t to 0.
board.array[i][j].t = 12

be a valid assignment? I seem to get all of the array locations pointing to the last Tile I assigned. How do I properly write this so that each array[i][j] gets a unique Tile object? What is the secret syntax here?

Wouldn’t it be board.array[i][j] = Tile()

Andrew it right. To create an instance of Tile you must do Tile(). The function Tile:init() is not defined to be called dirctly by you. It is called by Tile().

@Andrew_Stacey, correct

That was it! Thank you!

I’m still in Java mode and keep getting hung up in that syntax. For some reason, I thought I had a static method. Cart before horse, again. :-w

One more question: can I pass a class method in a closure?

In my example, if the user touches a tile, I have a handler stored to handle the action.

Thus, for an instance tile of Class Tile(), there is a

self.action = Tile:spin()

How do I properly notate the call to spin()? Or, do I just pass self to a plain function? Plain functions seem to work fine for me. Thanks!

self.action = self:spin() or self.action = Tile.spin(self)

@JakAttak. Thanks for trying, but I tried every possibility of self vs Tile and . vs : and passing self.

Most were not grammatical and threw a parsing error. All the rest refused to pass any values. I finally just made it a plain global function and passed my object as the args. It works, although not as elegant as I would like. I don’t care for too many globals running around.

Edit: Looking at CargoBot, I think I see how it’s done… Going to try again.

@Syntonica not quite sure what you ask for. If you want to pass the class method spin to self.action just do: self.action = Tile.spin and then you run it with self:action() or self.action(self). Might not be your question, though.

I’m having an argument with the tween function at the moment, but I give that a try again when I have a moment… Thanks!

@Jvm38 Finally got it with:

self.action = Tile.spin
....
self:action()

and finally got a non-nil self.

Thank you so much! (Too many late nights for me… But, man, am I loving the power of Lua!)

@syntonica good to know i could help you. But be careful: i started programming in Codea 1.1/2 year ago, and that was a one way trip… You might never want to go back! :wink:

If they’d put closures in Java, I could live with it. I’m tired of writing wrappers for functions when I’d rather just pass the function (actions :-@).

But, once I get Codea down, I’m tackling JLua. :smiley: