- Home /
Can WebGL code interrupted?
My problem is a fringe problem- I'm actually hoping it is something out of my control because I have no idea where it happened, or how.
When running in a browser, is it possible for execution of a block of code to get interrupted , and/or the statements executed out of order, by something the user does with the computer?
I have a situation where I'm recording elapsed time across four variables in a row.
I can see how those times might vary by a few microseconds, but today I hit a case in a webGL game that someone was running (no idea what computer, OS, or browser they were using) and the times came out several seconds apart.
Imagine the situation, in reference to the code below, where stimulus_time is >= stimulus max time and g1=g2=g3=g4 = false. Latency_timer is a stopwatch. As soon as mode changes in the last line, this code will not be encountered again until the next "trial". This is the last opportunity for l1 to l4 to get set if they have not already been set on a particular trial. If they have already been set, they'd be less than stimulus max time (which was 14 seconds)
if (stimulus_time >= stimulus_max_time)
{
if (!g1)
{
l1 = latency_timer.elapsedmilliseconds*.001;
g1 = true;
}
if (!g2)
{
l2 = latency_timer.elapsedmilliseconds*.001;
g2 = true;
}
if (!g3)
{
l3 = latency_timer.elapsedmilliseconds*.001;
g3 = true;
}
if (!g4)
{
l4 = latency_timer.elapsedmilliseconds*.001;
g4 = true;
}
mode = Modes.context_change;
}
So far this has worked out just fine. Its given what appears to be correct data over hundreds of trials across many different users. If the "if (stimulus_timer >= stimulus_max_time)" is true, and all the bools are false, then l1 to l4 are always the same out to at least 2 decimals (e.g., 14, 14.02) thats as far as I print in my data file anyway. But today,on one trial for one person I got 21.24, 15.67, 16.55, and 17.63 for l1 to l4.
This is a fringe case- I've had 15 anonymous people run this game to completion, and there are 41 times that this code gets executed per person, and it's been accurate 764 out of 765 times.
Answer by Bunny83 · Nov 13, 2021 at 01:03 AM
You said "anonymous people". So where or how did you get that data in the first place? Are you sure noone actually forged that one messed up dataset?
I'm not sure how the Stopwatch is actually implemented in a WebGL build (probably something like performance.now). Though the timer is most likely independent from your framerate. Though your code should be executed in order. So it's very unlikely that you get a higher value first and then smaller values.
So I would assume that the one odd one out got manipulated. You probably have a backend where you send the data? Is that API at least a little bit secured against forgery? There are several things you can do to improve the reliability of the data. For example calculating a checksum of the values. Of course someone who wants to manipulate the values may figure out how that checksum is calculated, but it makes it harder fo manipulate.
Another thing may be to use a simple encryption of the data. I remember this old flash game send tons of data when you submitted your highscore. It even submitted where and when you placed towers. This makes it possible for the server to do some sanity checks. Though they still had a lot of the top scores which were forged / cheated. You simply can't prevent that. Some attempts could be easily identified, either automatically or manually. Though if the essentially game logic runs on the clients machine, it will never be secure.
Answer by DrJBN · Nov 13, 2021 at 10:08 AM
Thank you @Bunny83, The game is a method to study associative learning and I have lights that signal oncoming spaceships. People learn to press keys (TAB, BACKSPACE, LEFT CONTROL, RIGHT CONTROL) to prepare weapons associated with repelling each ship. The data (number of presses on each second of each trial and latency to first press) are stored in the game in arrays of doubles. At the end of the game they are put into a string (e.g., Data_String += data[i].ToString() ) that is sent to my save_data.php file on my server (Dinahosting) and stored in an ASCII text file.
I'll put the rest of my responses in a list below because I cannot figure out how to get line breaks to show up in the output from this input box...
--- Unfortunately I'm an experimental psychologist and a hack at programming. As far as server security, I have no idea there- I just created and pay for the server account where the game and data files sit
--The participants are recruited through Prolific academic and get 7 GBP per hour for playing. They read a study description and if they choose to participate, they click the link to the game. The game usually takes about 30 minutes. For the person in question, they took 31 minutes.
---Their payment is conditional only on playing the game and finishing, or reporting a crash in the case something goes amiss. If someone wanted to fiddle with the data, I suppose someone sufficiently motivated could, but there's no benefit to it for them as their payment isn't conditional on the actual values of the data.
--The period in which a latency could be recorded was 14 seconds long. If the person responded, the latency on that key was recorded. If no response was made, it would be set to the actual length of the period as recorded by the stopwatch.
--The latencies recorded were 21.24, 15.67, 16.55, and 17.63 seconds. I looked at their actual response counts on that trial on those keys, and the person did respond on each key so the error took place in a different section of code (e.g., if keypressed then record latency and flag as recorded.) The first response should be when the latencies were recorded. On the four keys, the first response on the Tab was during second 11, the first response on the Backspace was during second 7, the first response on the Right Control was during second 8, and the first response on the Left Control was during second 9. It is curious that the times recorded are pretty close to double of the times they should have been- that might help me track down my bug, if its something I've done.
--Could I be running into problems with my choice of keys? I have tested pressing all the different keys and their combinations during the game and haven't had any issues, but could a certain combination of keys with a particular browser running in Windows be getting processed by the broswer before/during/after the game gets it and doing something funny?
. Many thanks for looking
Your answer
Follow this Question
Related Questions
WebGL + Mobile + Sound? 0 Answers
WebGL Input Manager reacting 0 Answers
Making antivirus play nice with Unity 1 Answer
Switch Platform from PC to WEBGL Load into game is very slow 0 Answers
Is WebGL compatible with mobile controls/platform? 0 Answers