Why does this script work in GGB 5 Classic but not in GGB Web?

hjbortol shared this question 3 months ago
Answered

Hi!


The attached construction works fine under GGB 5 Classic Java (please, press the button "Gera" to see it running). But, when the same construction is upload to GeoGebra Materials, the app doesn't work anymore: https://www.geogebra.org/m/hkvbrvgu (please, press the button "Gera").


Any hints are welcome!


Best regards, Humberto.

Comments (17)

photo
1

Hello, I am the author of the above construction, which my friend hjbortol above kindly mentioned.

Let me add a few things:

The construction simulates placing mines in a game of Mine Sweeper.

The darker cells are the mined field. The user should write numbers in these cells, as one would tipically see in a game. After you press the button 'Arma', this input is processed and stored in a variable 'abertas'. This step is performed corectly. In fact, after you then press 'Gera', which should place the mines several times, the corresponding variables are also computed correctly. For some strange reason, the actual darker cells are *not* written with the values stored in the variable 'campo'. Not only that, but after pressing 'Arma', which should only read the values in the written cells, whatever values we wrote on the field dissappear (the cells become 'undefined'). This happens even though the function that is called by the button does not have *any* commands that write (or delete) on cells. It only has commands that read the cells.

Any help is appreciated. If anyone needs further info, just ask.

photo
1

@rami, but it is the opposite: it's working in the offline version, but not in the online version ...

photo
1

You are right: prompt works (now?) only in GGB 5.0 and in GGtube (GGB 6.0 TUBE)

It's not the prompt something else is wrong.

photo
1

Prompt and alert work in my offline version (GeoGebra Classic 5.0.553.0-d (31 July 2019), but this does not seem to be the problem. I've explained it all in a previous post, but it seems that it is not visible (on moderation)

photo
1

Please try and make a smaller test-case to show the problem - it's a bit hard to follow what's supposed to happen in the example above :)

photo
1

you can change the number 10 next to Minas to 1

and the numbers 9 nexto to Linhas and Colunas to 2.

The code works, in the sense that all things get computed, but nothing is written on the spreadsheet cells.

For some reason, upon completion of execution of the js functions, the cells are deleted (without any command for that).

photo
1

So far, I've manage to find out that for some reason, the function DesenhaProbs is not being called on the online version. I print out its arguments right before the call, which seem ok, then I also print a message after the call, which also shows up. So, it all works as if the function is being called, but the prints I put into the body of the function are never performed.

I tried to write another test button, calling again the function, and got an error message, saying that the function is not defined!

photo
1

I downloaded the offline Classic GG version 6 and found out that it had similar problems.

I have switched things a bit here and there, fixed some minor mistakes (that could not have caused the problem) and figured out that the update routines for the cells containing the number of mines, columns and rows were being called even though there was no command invoking them.


By printing messages on the screen, I found out that, say, the function that was called by the 'Arma' button would run to completion... and then misteriously the update function for the above mentioned cells would be called.


I managed to work around the problem, since I was keeping the old values of these cells for other purposes. I added a test that would only do anything on these update routines if the value has actualluy been changed. This way, nothing was performed whenever I did not actually change the cells myself and the output got finally written.

So, this works, but the mistery of the unwanted execution of the update routines remains. I would still like an answer to that.

photo
1

oh, I also tried to make a test to figure out which version of GG was being used (I have to know, so I can choose between, say alert and confirm). This also did not work: getVersion for the version 6 would return 5.0.546.0 instead of 6.0.546.0.

It seems the whole universe is ganging up against me.

photo
1

Try this:

if (window && window.alert) {
alert("online")
} else {
alert("classic 5")
}

photo
1

Thanks.

I've made a much smaller test activity, as requested: It has a button A1 that, when pressed, writes 51 in the cell B1.

I've also created an onupdate function for the cell B2, that deletes cell B4 and sets its background color to lavender.

As in my larger file, pressing the button A1 causes not only B1 to be written with the number 51, but also executes the onupdate function of cell B4!

I thought the onupdate function was only supposed to be executed if one updated the given cell (manually or by some function call). This is the behaviour that I have noticed in version 5, but both online and on version 6, I get this function being executed also in these other cases, as explained.

Help is appreciated.

There is also the issue of not being able to press the button on my iPad or Android phone, but I've created another topic for that particular problem.

photo
1

Does it work better to replace


ggbApplet.evalCommand("B1= 51");

with

ggbApplet.evalCommand("SetValue(B1,51)");

photo
1

No. In this small example, as in my larger activity, it is after the info is written in the intended cells, that they are deleted by the update function of other, uncorrelated cells, which are not updated in any way by the code. This is the root of the problem, as fas as I can tell. I managed to work around this in my original (larger) activity by testing if the cells whose update functions get executed are actually being updated (comparing old and new values). This works, but it's awkward and makes the code somewhat harder to read.

photo
1

Please change your small example to use ggbApplet.evalCommand("SetValue(B1,51)"); and I'll have another look

photo
1

I don't see the point, but here it goes:

https://www.geogebra.org/cl...

I've also added 'confirm' commands, so you can see what happens at each step: button is pressed, onclick function's end is reached, inexplicably onupdate function of cell B2 gets executed, which deletes content of cell B4. This is also essentially what is happening with my larger, original, activity.

As I said previously: you can use whatever command you wish to write on cell B1. The cell that gets deleted is B4 and by running an update function of cell B2, which is never updated! Either directly (manually) or indirectly (by some command in some function).

photo
1

Thanks, we'll need to look into that!


BTW it's important to use SetValue(A,B) not A=B in scripts: https://wiki.geogebra.org/e...

photo
© 2019 International GeoGebra Institute