Stupid Mistake crashing Codea reading tables in Classes

@jfperusse - never tried to provide data from a table to a Class before, but tried recently to see if I could simplify my class code. So the code below locks up Codea and later crashes it. Sometimes I have closed closed Codea re-loaded it but Codea is unresponsive and eventually crashes. Could you point out my error and the correct way to address Tables to Classes.

Also should this error be trapped to avoid crashing Codea ?

Viewer.mode = FULLSCREEN
function setup()
    --
    sW, sH, cW, cH = WIDTH, HEIGHT, WIDTH//2, HEIGHT//2
    aspect = WIDTH/HEIGHT
    name = "Problem"
end

function draw()
    -- 
    background(0)
    pushStyle()
        fill(255, 14, 0)
        font("AmericanTypewriter-Bold")
        fontSize(32)
        textMode(CENTER)
        text(name, cW, cH)
    popStyle()
end

function touched(touch)
    --
    t = touch
    if t.state == BEGAN then
        
    end
end

butR = {WIDTH//2, HEIGHT//2, 200, 50}

--

Button = class()

function Button:init(but)
    -- you can accept and set parameters here
    print("hello")
    self.x = but[1]
    print(self.x)
    self.y = but[2]
    self.w = but[3]
    self.h = but[4]
end

function Button:draw()
    -- Codea does not automatically call this method
end

function Button:touched(touch)
    -- Codea does not automatically call this method
end

Edit: Just noticed in the posted code in the forum that in the touched() function the end on the if construction is in a different case and highlighted - could that be causing the error or is it a forum posting issue.

The highlighting of the end is because you haven’t formatted your code in a code block, so the indentation is triggering just that line being recognised as code.

Put

```

on the line before and after your code.

Where are you initialising the button objects? Using a table to pass parameters when initialising an object is absolutely fine, I’ve been doing that for years with no problems.

@LoopSpace - thanks for the reply. I set up a table as butR and used the array name in the Button:init() function to set up the butR object. I added the print statements in the Button:draw() function to check that the table parameters had been read.

My basic idea was to define a range of buttons in tables so that all I needed to do in the Class was to pass the tablereference to the Class.

Whatever I have done hangs up Codea which eventually crashed. Have loaded the code yourself ?

Thanks for the pointer on the forum format/error.

Could you point me to a simple reference for tabled data use in Classes.

I’ve spent too much time trying to unravel this and I’m sure the answer is simple and straightforward.

In the code you posted then there are no calls to the Button creator, so if it crashes then it must be something else.

Here’s an example of code that uses a table to pass parameters. It won’t run as-is since it is a module that depends on other things, but you can see the syntax that I use.

@LoopSpace - thanks for the example, very useful detailed and efficient - up to your usual standard.

@jfperusse - I think there is something wrong with the latest beta that may have occurred and, possibly corrupted, my Codea installation. I mentioned the menu bar text for the commands disappearing in a previous post and, after loading @loopspace ‘s code they re-appeared temporarily. I have had multiple freezing screens and Codea freezing after switching off and just loading (I have posted crash reports for them).

Is it possible for Codea to write over part of its own code ? I would think that has been protected. Or possibly some module has been lost/remarked out in the latest beta launch ?

Hi @Bri_G. No, Codea cannot mutate its code. If you have issues, they could have been introduced in recent versions, or related to your content. We’ll check the crash reports. Thanks.