Unable to convert NaN to a number

URL of experiment: https://run.pavlovia.org/Wake/affect-grid/

Description of the problem: I wrote an affect grid routine this week with polygons and text components created in code (instead of making an image).

It works - unless you take too long to respond, at which point it gives the above error.

The experiment is public and has just two routines (the affect grid and a results display).

The Each Frame code looks like this:

[mousex,mousey] = mouse.getPos()
mousex-=xOffset
if abs(mousex) < maxScore*gridSize and abs(mousey) < maxScore*gridSize:
    if mousex > gridSize/2:
        xpos = round(mousex/gridSize-.5)*gridSize+gridSize/2
    elif mousex < -gridSize/2:
        xpos = round(mousex/gridSize+.5)*gridSize-gridSize/2
    elif mousex > 0:
        xpos = gridSize/2
    else:
        xpos = -gridSize/2
    if mousey > gridSize/2:
        ypos = round(mousey/gridSize-.5)*gridSize+gridSize/2
    elif mousey < -gridSize/2:
        ypos = round(mousey/gridSize+.5)*gridSize-gridSize/2
    elif mousey > 0:
        ypos = gridSize/2
    else:
        ypos = -gridSize/2

The offending line (based on console logs) is
gridCursor.setPos([xpos+xOffset, ypos])

I have tried the following:

    if xpos == NaN or ypos == NaN:
        pass
    elif xpos != oldX or ypos != oldY:
        print(xpos,ypos)
        gridCursor.setPos([xpos+xOffset, ypos])
        oldX = xpos
        oldY = ypos

which translates to:

    if (((xpos === NaN) || (ypos === NaN))) {
    } else {
        if (((xpos !== oldX) || (ypos !== oldY))) {
            console.log(xpos, ypos);
            gridCursor.setPos([(xpos + xOffset), ypos]);
            oldX = xpos;
            oldY = ypos;
        }

and yet I’m still getting a crash. The test I just ran had a final console log of -0.05 NaN

The error is
{origin: “util.toNumerical”, context: “when converting an object to its numerical form”, error: “unable to convert NaN to a number”}

How can I skip the setPos command on frames where I’ve lost the y coordinate?

I did wonder if it was because I also had a mouse component in the same routine but having removed it I still get the same error -0.25 NaN

Hi @wakecarter, looks like the input to setPos() is taken through toNumerical() for sanity checking. That the latter is throwing means it is working as expected I believe. I would need to look closer to determine why ypos comes up NaN in the first place, but for now you should be able to use Number.isNaN() instead of the auto-translated ypos === NaN to check if ypos is usable or not, x

2 Likes