ExclamationHelpClientless NodeJS - Looping, and storing data

Posts 14 of 4 · Page 1 of 1
Clientless NodeJS - Looping, and storing data
Using the clientless NodeJs, how do you loop/update, like some sort of tick system?

And how is it possible to store data, perhaps into local json files or local sql databases?

Im kinda noob, so try not to over generalize since Im new to this :/

Thanks
ok so there are different packets in the incoming you can hook I recomend looking throught the docs in the github endpoint so for example
Code:
export default class Test { @hookPacket(PacketType.NEWTICK)
onNewTick(client: Client, newTick: NewTickPacket): void{
console.log('NEW TICK WAS CALLED ON:' + client.alias);
}
}
Not that great but I hope it helps!
there's a few packets that get updated pretty often and are good to 'loop' in

use the newtick one posted above or another really popular one to do stuff in is the update packet as well, that being said, worlds your oyster, just hook the most relevant packet to what you're trying to achieve.
Quote Originally Posted by poopoorino View Post
Using the clientless NodeJs, how do you loop/update, like some sort of tick system?

And how is it possible to store data, perhaps into local json files or local sql databases?

Im kinda noob, so try not to over generalize since Im new to this :/

Thanks
If you want to run an event loop on a custom timer you can use the `setInterval` method which is built into Node.
Code:
setInterval(() => {
    console.log('tick');
}, 1000 / 60);
The first argument is the function that will get called when a tick occurs, in this case it will just log 'tick' over and over again.
The second argument (1000 /60) is the number of milliseconds between the ticks. 1000 milliseconds is 1 second, so dividing by 60 will mean that the loop will fire 60 times per second.
If at some point you want to stop the loop from running, you can use the `clearInterval` method.
Code:
let ticks: number = 0;
const loop = setInterval(() => {
    ticks++;
    if (ticks > 100) {
        clearInterval(loop);
        console.log('100 ticks have passed.');
    }
}, 1000 / 60);
As for storage, there are many npm modules which are available for storing data in a database. I use Firebase for a lot of projects since it uses JSON as the storage format, so you can pretty much interact directly with the data in a JavaScript program without transforming it first.

Storing data locally with JSON is also pretty easy. Have a look at the Node File System module docs (fs.writeFile is probably what you want to look at).
Posts 14 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?