Anthony Posted November 22, 2022 Share Posted November 22, 2022 This code is for a script in a virtual reality application. The script includes several functions that are called at different points in the application's lifecycle, such as when a user connects or disconnects, and when the application is loaded. The script also includes several lines of code that create and manipulate various assets, such as materials, textures, sounds, and actors. Additionally, the script includes event listeners that are triggered when a user enters or leaves the zone, and a "tickEvent" that runs a function every 1 second. The function showUI is used to display a UI with buttons to play sound and create environment. "use strict"; // @name HelloWorld // @author Anthony // @version 1.0.1 (function HelloWorldApp() { let tickEvent = null; let textTemplate = null; let touchEvent = null; let currentPlayerCount = 0; let trackedGlobalActors = []; // get the zone that belongs to the actor const zone = zoneManager.getZoneById(app.getZoneId()); // prefix our assets with app id to prevent collisions const _prefix = app.buildGlobalPrefix(zone, null); // called everytime a user connects app.onUserConnected = (player) => { showUI(zone, player); }; // called everytime a user disconnects (cleanup and gc) app.onUserDisconnected = (player) => { // handle actions when user disconnects }; // called when application is loaded app.start = () => { // enable touch event touchEvent = sdk.enableTouchEvent(app); // define some materials sdk.createColor3Material(_prefix + ".color1", 0, 100, 100); sdk.createColor4Material(_prefix + ".color2", 100, 100, 100, 170); // prepare textures sdk.createTexturedMaterial(_prefix + ".business_logo", "https://upload.metascript.cloud/api/v1/file/f790edac-1ffa-49c7-a6ce-c7d203ef8b02"); // sounds sdk.createSound(_prefix + ".test", "https://raw.githubusercontent.com/rse/soundfx/master/soundfx.d/chime1.mp3"); // create test vjdeo sdk.createVideo(_prefix + ".video_0", "https://download.samplelib.com/mp4/sample-5s.mp4"); // create new test actor sdk.createActor({ name: _prefix + ".desk_top", color: "#00FF00", parentId: app.getZoneId(), dimensions: new Dimension(0, 0.5, 1), position: new Position(0, 0.1, 0), rotation: new Rotation(0, 0, 90), materialId: "idk", appId: app.getId(), physics: false, layer: "navigation" }); // collision event tickEvent = submitEvent(() => { // everything in here is actually being called every 1 second }, 1000); zone.onUserJoin = (player) => { // called everyone someone enter the zone } zone.onUserLeft = (player) => { // called everyone leaves the zone } }; function showUI(zone, player) { const ui = player.getComponent(app.buildGlobalPrefix(zone, player) + ".example.ui"); if (ui) { return; } const uiButtons = []; // play sound uiButtons[0] = new DefaultButton("Sound", () => { sdk.playSound(_prefix + ".test", player.getActor("location").getId()); }); // create environment uiButtons[1] = new DefaultButton("Image", () => { showImage(player); }); uiButtons[2] = new DefaultButton("Kit Item", () => { showKitItem(player); }); uiButtons[3] = new DefaultButton("Empty", () => { // TODO handle the logic here }); uiButtons[4] = new DefaultButton("Video", () => { showVideo(player); }); uiButtons[5] = new DefaultButton("Attributes", () => { showAttributes(player); }); uiButtons[6] = new DefaultButton("Equip", () => { attachActor("1319923140712727070", "spine-middle", new Position(0, 0, 1), new Rotation(0, 0, 360), 1, player); }); uiButtons[7] = new DefaultButton("Detach", () => { detachActor("1319923140712727070", player); }); const exampleUI = new ButtonUITemplate({ name: "example", title: "Demo", app, sdk, zone, position: new Position(0.10, 0.2, 0.35), rotation: new Rotation(0, 180, -270), parentId: app.getZoneId(), height: 1.50, buttons: uiButtons, buttonsPerRow: 2, scale : 0.1 }); exampleUI.showUI(player); // create a basic text template and assign it to a variable so we can reuse it textTemplate = new TextUITemplate({ name: "example.text", title: "Output", app, sdk, zone, position: new Position(0.85, 0.025, 0), parentId: exampleUI.getId(), scale : 1 }); textTemplate.showUI(player); } // attributes example function showAttributes(player) { // set player attribute player.getAttributes().set("name", player.getName()); // get player attribute const name = player.getAttributes().get("name"); sdk.sendMessage(name, player.getId()); } // show kit item helper function function showKitItem(player) { const kitItemId = _prefix + ".kit_item_0"; const existingKitItem = player.getActor(kitItemId) if(existingKitItem) { // delete existing kit item if we click the button twice sdk.destroyActor(existingKitItem.getId(), player.getId()); return; } // load monitor const kitItemActor = sdk.createActorFromLibrary({ name: kitItemId, parentId: app.getZoneId(), position: new Position(0.1, 0.2, 0), rotation: new Rotation(0, -90, 0), scale: new Scale(1, 1, 1), artifactId: "1319923140712727070", grabable: true, physics: true, appId: app.getId() }, player.getId(), null); // you can modify actors to make them grabable sdk.modifyActor(kitItemActor.getId(), { grabable: true }); // keep track of global actors trackedGlobalActors.push(kitItemActor); } // show image helper function function showImage(player) { const myImageId = _prefix + ".image_0"; const existingImage = player.getActor(myImageId) if(existingImage) { // delete existing existingImage if we click the button twice sdk.destroyActor(existingImage.getId(), player.getId()); return; } const imageActor = sdk.createActor({ name: myImageId, color: "#00FF00", parentId: app.getZoneId(), dimensions: new Dimension(0.0025, 0.125, 0.25), position: new Position(0, 0.25, 0), rotation: new Rotation(0, 0, 0), materialId: _prefix + ".business_logo", appId: app.getId(), lightIntensity: 1, grabable: true, physics: false, layer: "navigation" }, player.getId(), null); // schedule animation to happen to the newly created actor const animationEvent = submitEvent(() => { sdk.animateTo(imageActor.getId(), { position: new Position(0, 1, 0), rotation: new Rotation(0, 0, 0), scale : new Scale(2, 2, 2), duration : 2.0, algorithm: "Linear" }); animationEvent.stop(); }, 1000); // keep track of the new actor trackedGlobalActors.push(imageActor); } // show video helper function function showVideo(player) { const videoActorId = _prefix + ".video_parent"; const existingVideo = player.getActor(videoActorId) if(existingVideo) { // delete existing actor if we click the button twice sdk.destroyActor(existingVideo.getId(), player.getId()); return; } // spawning new video if none exists const videoParent = sdk.createActor({ name: videoActorId, color: "#00FF00", parentId: app.getZoneId(), dimensions: new Dimension(0.001, 0.001, 0.001), position: new Position(0, 0.5, 0), rotation: new Rotation(0, -90, 0), materialId: "idk", appId: app.getId(), grabable: true, physics: false, layer: "navigation" }, player.getId(), null); // play video stream. sdk.playVideo(_prefix + ".video_0", videoParent.getId()); trackedGlobalActors.push(videoParent); } // detach actor helper function function detachActor(artifactId, player) { const attachmentKey = _prefix + ".attachment-" + artifactId; const actor = player.getActor(attachmentKey); if(actor) { sdk.destroyActor(actor.getId(), player.getId()); } } // attach actor helper function function attachActor(artifactId, attachmentPoint, position, rotation, scaleN, player) { const attachmentKey = _prefix + ".attachment-" + artifactId; const actor = player.getActor(attachmentKey); if(actor) { // prevent spawning multiple actors of same type return; } sdk.attachActor({ name: _prefix + ".attachment-" + artifactId, parentId: attachmentPoint, artifactId: artifactId, position: position, position: position, rotation: rotation, scale: new Scale(scaleN, scaleN, scaleN) }, player.getId(), null); } // perform cleanup of global actors app.stop = () => { if(touchEvent) { // important to stop listening to touch events touchEvent.stop(); } if(tickEvent) { // calling stop on an event will stop the timer tickEvent.stop(); } // this means we already have global actors, we need to remove them trackedGlobalActors.forEach((actor) => { // you can destroy any actor given the id sdk.destroyActor(actor.getId()); }); // reset the array so they can spawn again if we press button trackedGlobalActors = []; }; }()); Link to comment Share on other sites More sharing options...
Recommended Posts