Connecting PlayCanvas to a Database

Hi,

Just to give you a steer i’m no coding genius so please be gentle.

I’d like to connect Playcanvas to a simple read only database. I have a client who wants to be able to define some basic parameters outside of Playcanvas and have them instantly updated in ‘game’ when it loads (even a csv or similar file hosted on dropbox/box etc would do it, it just needs to be really easy to update their end).

The database isn’t large or complicated, just a list of on/off parameters for about 20 related entities I have in my project, but this could swell to 200 or more.

I’ve had a play around with Node.js & sqlite in the past so i’m more familiar with those than anything else, but like i said i’m no pro when it comes to back-end.

Could someone give me a step by step/ hello world or failing that list out the stack I’d need to make something like this work and how i’d implement it in PlayCanvas.

Thanks,

PandaC

I guess it’s pretty simple.

You have to install nodejs, mysql (any database, I will use mysql for example) and forever (npm install forever -g)

Then you have to config your database, create user, etc. It depends on your tasks, but IIRC you can do it along installation process.

Since you have nodejs and mysql, you have to create server. Probably sockets is your chose because it’s fast, easy and you can update your params right from server.

Read this article, when your server starts, you have to connect to mysql database and store your connection.

Then, when client connects from web, postMessage him with current settings.

When some settings have updated, just postMessage again with that.

On client you have to parse messages from server and do things.

I know it’s not quite straightforward, but try to start on that and ask when you got in trouble.

Hi Mike,

I think when it gets to node i’m OK as there are plenty of tutorials out there, but it’s making the initial connection from PlayCanvas that i’m struggling with. Is this something you’ve done before in PC?

Yes, just create script component and preload it.
Handle you connection there.

Hi, I’m sorry but this is going completely over my head.

Would it be possible to break this down a bit with script examples?

Thanks

Oh, just create script component.
in Initialize method init your socket.

I don’t have example :frowning:

In the end the data that i needed was so simple it was easier to just do it with JSON, there’s a really good example here on the tutorials page if anyone is interested:
https://developer.playcanvas.com/en/tutorials/loading-json/

Thanks

Check out this post i found it uses mongodb in js:
https://theoverpoweredpc.tk/posts/how-to-use-mongodb-in-html