- Messages
- 240
- Country
Surely you must all heard of ChatGPT, which has taken the internet by storm in a very short time. I found it very useful to help coding, so I tried asking this question:
And sure enough, after 1 second, I've got this:
I just copied the code as it was, replacing my example function with this output, fired up MSFS, tried the new message queue and it worked at the first try! Sure, this is not a very complex function, and I could have written it myself, but surely not in a 1 second, and it's impressive how ChatGPT understood precisely what I asked for, at the first try.
All the more interesting, considering Microsoft is heavily invested in OpenAI ( developers of ChatGPT and Dall-E ) and there are some rumours that is considering buying it altogether. A proper integration in Visual Studio or VS Code seems the next logical step.
This is a javascript function that displays a text message using CoherentGT:
JavaScript:
function showText( msg, timeout, posXRel = 40, posYRel = 40 )
{
Coherent.trigger("SHOW_TOOLTIP", 0, msg, posXRel, posYRel, msg.length * 100);
if( timeout > 0 )
{
setTimeout( () => { Coherent.trigger("HIDE_TOOLTIP", 0); }, timeout * 1000 )
}
}
Can you add a FIFO queue to use this function in a way that multiple calls to this function will not overlap, to allow all messages to be seen ?
And sure enough, after 1 second, I've got this:
Yes, you can add a FIFO queue to this function to prevent overlapping messages. Here's an example of how you could implement the queue:
JavaScript:
const messageQueue = [];
let isShowingMessage = false;
function showText(msg, timeout, posXRel = 40, posYRel = 40) {
messageQueue.push({ msg, timeout, posXRel, posYRel });
if (!isShowingMessage) {
showNextMessage();
}
}
function showNextMessage() {
if (messageQueue.length === 0) {
isShowingMessage = false;
return;
}
isShowingMessage = true;
const { msg, timeout, posXRel, posYRel } = messageQueue.shift();
Coherent.trigger("SHOW_TOOLTIP", 0, msg, posXRel, posYRel, msg.length * 100);
if (timeout > 0) {
setTimeout(() => {
Coherent.trigger("HIDE_TOOLTIP", 0);
showNextMessage();
}, timeout * 1000);
} else {
showNextMessage();
}
}
This implementation uses a global messageQueue array and a global isShowingMessage flag to keep track of the state of the messages. When the showText function is called, it pushes the message and its properties to the messageQueue. If a message is not currently being shown, the showNextMessage function is called. The showNextMessage function checks if there are any messages in the queue, if there are none it sets the isShowingMessage flag to false, if not it pops the first message in the queue, shows it by triggering the coherent.js events and starts a timeout. Once the timeout is done, it calls hide_tooltip event, and calls showNextMessage again to show next message in the queue if any.
I just copied the code as it was, replacing my example function with this output, fired up MSFS, tried the new message queue and it worked at the first try! Sure, this is not a very complex function, and I could have written it myself, but surely not in a 1 second, and it's impressive how ChatGPT understood precisely what I asked for, at the first try.
All the more interesting, considering Microsoft is heavily invested in OpenAI ( developers of ChatGPT and Dall-E ) and there are some rumours that is considering buying it altogether. A proper integration in Visual Studio or VS Code seems the next logical step.