Hi there, I was wondering if its possible to save entities that are created outside the editor like in this code below
if(app.keyboard.wasReleased(pc.KEY_R) ){
player = this.app.root.findByName('Player');
var playerpos = this.player.getPosition();
var playerrot = this.player.getEulerAngles();
var entity = new pc.Entity('Model');
pc.app.root.addChild(entity);
console.log(playerrot);
entity.setLocalPosition(playerpos.x, playerpos.y - 0.932, playerpos.z + 1);
entity.setEulerAngles(playerrot.x, playerrot.y, playerrot.z);
app.assets.loadFromUrlAndFilename('https://threejs.org/examples/models/gltf/Soldier.glb', null, "container", function (err, asset) {
// Add the loaded scene to the hierarchy
entity.addComponent('model', {
asset: asset.resource.model
});
entity.addComponent('collision', {
type: 'mesh',
asset: asset.resource.model
});
entity.addComponent('rigidbody', {
type: pc.BODYTYPE_KINEMATIC
});
if (asset.resource.animations.length > 0) {
entity.addComponent('animation', {
assets: asset.resource.animations
});
}
});
}
And update the world for other players as well? (Multiplayer)
What if we use pure Js (on the bind 'R) to make a new file that loads and creates models/entities?
Okay I have an idea, let’s say this code is for local player, once he reloads it’s all gone (Same does for other players)
I could make an receiver that would write contents to a new file
Once I press the key I could make a new API call that would pass model url, position, rotation etc and put the full code in file, on editor side I could read that file so when player joins again his createad entities would be here + for other players as well and wice versa
The problem is reloading, hot reloading? is it possible?
Update: The logic seems to be working out, so I just created a new ‘Plane’
Added a script Loader
Loader cointais:
var Loader = pc.createScript('loader');
// initialize code called once per entity
Loader.prototype.initialize = function() {
this.entity.script.create('player');
function loadScript(url) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script');
script.src = url;
script.async = false;
script.onload = function() {
resolve(url);
};
script.onerror = function() {
reject(url);
};
document.head.appendChild(script);
});
}
let scripts = [
'https://political-sweet-net.glitch.me/Objects.js'
];
// save all Promises as array
let promises = [];
scripts.forEach(function(url) {
promises.push(loadScript(url));
});
Promise.all(promises)
.then(function() {
console.log('all scripts loaded');
}).catch(function(script) {
console.log(script + ' failed to load');
});
};
// update code called every frame
Loader.prototype.update = function(dt) {
};
// swap method called for script hot-reloading
// inherit your script state here
// Loader.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/
And then the external script:
var Player = pc.createScript('player');
// initialize code called once per entity
Player.prototype.initialize = function() {
var box = new pc.Entity();
this.app.root.addChild(box);
box.addComponent("model", {
type: 'box',
});
box.addComponent("script", {
type: 'script',
});
console.log("Box created");
};
// update code called every frame
Player.prototype.update = function(dt) {
};
Box appears as it should
Whats left: PHP Receiver > Read/Write, javascript call to receiver, hot reloading (no idea)
Yep, ultimately it sounds like you are asking to save the runtime state of a game world somewhere and for a multiplayer game, this would like to be on the server on a database or something similar.
The server would also be responsible for keeping the clients in sync with each other with the client code handling changes in the game state/world from the server such as creating/destroying objects.