Some questions from a new Codea user

I am new to Codea and just trying to get a few simple things going and having difficulties. I put together a small app that records the time when you tap the screen and then plots the data afterwards, very vanilla compared to some of the things I’ve seen on this forum. Here are some observations/questions:

  • Touch events don’t have timestamps on them. In particular, I couldn’t tell how long I had touched the screen. MOVING wasn’t documented as working for stationary touches.
  • There was no way to get the current date/time. (os.time provides this now?)
  • writeProjectData is documented as able to save any value. No–it only saves a number or a string. I needed to save an array of strings. In general, de/serialization of any reasonable data structure should be possible. So I had to concatenate everything into a big string which will then need parsing.

Now I’m trying to display the data that was created.

  • Parsing the string isn’t working. Maybe string.gmatch patterns don’t work like I expect? I thought the pattern “(%d-):(%d-),?” could get the time values out of a string like “2:30, 2:45, 2:55”, but only the minute is extracted from each of the 3 time values. With the “?” omitted, the minute and second values are extracted for the first 2 time values and the third is ignored. How do I write this pattern?
  • The graph is a straight line representing an hour’s duration, with x’s plotted on it according to the minute and second values. It only needs to be plotted once. I tried to do the plot in the setup() routine but that failed in both versions 1.2 and 1.3 so I tried to do the plot once in the draw() routine, then set a flag so that when draw() is called again it immediately returns–since the picture area isn’t drawn on further, what’s plotted there should remain, right? What happens is that the plot appears for an instant then flickers out. I tried setting backingMode() but that made the plot not appear at all. At the moment I can’t get even a simple line with an “x” plotted on it to stay onscreen.

Request for new feature:

  • When you return from executing your code, make the text cursor reappear in the location you left it–then one can start typing right away. As it is, one has to stop and touch the screen first to make the cursor reappear. (I’m using a Bluetooth keyboard.)
  • There should be a link somewhere in the editor directly to the Lua manual.
  • “Find/replace” command in the editor, both inside and between tabs. I have to copy tabs to another editor to do this now.
  • Any chance of real de/serialization for the different read/writeXXXdata routines?

One final question for future reference:

  • How does one copy code into the forum, preferably without lots of escaping?

  • Bob

The last one is easy. Use 3~ before and after your code.
Do you have a background() statement in your draw routine? That will erase the screen each draw.

@starblue I’m going to try to address some of your questions now and come back to some others later when I’ve looked into them.

• Touch events don’t have timestamps. At the moment you can use ElapsedTime to count the time between touches, but it might be a good feature to add.

• writeProjectData can only save numbers and strings for now. We want to fix this, but you’re correct that the documentation is wrong at the moment.

• I’m not too familiar with string.gmatch. Perhaps try your gmatch code on (choose Lua) and see if the behaviour is as you expect.

• The graph only needing to be rendered once: you can do this by setting backingMode(RETAINED). This will force the iPad to re-draw the previous frame buffer every frame. So your drawing operations will persist (don’t call background() in draw, though).

New features

Very good, practical, suggestions. I’m working on the first because it drives me crazy as well. Find and replace will come too.

Real serialisation is a bit trickier, given the sorts of things that can be stored in tables - classes and userdata in particular. But it’s in our thoughts for the future.

As @jlslate mentioned, fencing your code with ~~~ (3xTilde) will render it correctly.

@starblue: maybe try this for your time extraction

string = "my time is 2:30, 3:45, and some more 5:35"
    string = string .. "and again a few more 3:23, 6:26, ..., finally 8:45"
    for w in string.gmatch(string,"%d+:%d+") do
    for i=1, #extract do
        print("number "..i.." has value "..extract[i])