In-Game Programming with JavaScript


I am really excited about this feature. There is now a "Code Editor" button in the world options panel that will bring up a code editor. Check out this video :

The Code Editor accepts JavaScript and will execute your code whenever you are in Play Mode. Right now you can print to the chat screen, read input, and even call functions from the Unity Update() loop. The possibilities really are endless, and I'm excited to see how users will utilize this in their worlds. I've been having fun just brushing up on my JavaScript and making cubes move around the screen. 

Many Unity functions are exposed in JavaScript. For example, you can access Input.GetKey, Input.GetKeyDown, etc. Mathf, Vector2, Vector3, and others are available as well. Additionally, you'll find certain callback functions you can use to help respond to events.

If you're looking to play around with the new feature, here are a few fun code snippets to get you started. Just paste them into the code editor, then switch to Play Mode to make sure they run. 

If you provide an Update() function, it will be called every frame :

Spawn a Cube on button press :

    // Spawns a random cube on key press
    function Update() {
        if (Input.GetKeyDown("space")) {
            print("Spawning Cube");
            SpawnRandomCube();
        }
    }


Play Piano Notes on Keypress :

    // Play Piano Notes on KeyPress
    function Update() {
        if (Input.GetKeyDown("z")) {
           // piano notes 
            PlayNote("a");            
        }
        if (Input.GetKeyDown("x")) {           
            PlayNote("c");            
        }
        if (Input.GetKeyDown("c")) {           
            PlayNote("d");            
        }
        if (Input.GetKeyDown("v")) {           
            PlayNote("e");            
        }
    }


Use OnChattMessage to respond to chat message events :

    
    function OnChatMessage(message) {
        // If someone types in "Cube" to chat, spawn a random cube
        if(message == "Cube") {
           SpawnRandomCube();
        }
        // If someone types in "/clear" clear the chat log
        if(message == "/clear") {
           clear();
        }
    }


Hold down Left Mouse Button / Right Mouse Button to change the Time of Day :

    // Change Time of Day on Hold Left Click / Right Click    
    clear();
    currentTime =  GetCurrentTime();
    speed = 3;
    
    function Update() {
        // Don't respond to user input if a window or menu is open
        if(!InputReady) {
            return;
        }
        // Left Mouse button is down = progress time
        if(Input.GetMouseButton(0)) {
            currentTime += deltaTime * speed;
            if(currentTime > 24) {
                currentTime = 0;
            }
        }
        // Right Mouse Button is down = revert time
        else if(Input.GetMouseButton(1)) {
            currentTime -= deltaTime * speed;
            if(currentTime < 0) {
                currentTime = 24;
            }
         }
        SetTime(currentTime);
     }
     


Animated Lines in Chat :

    // Animate Lines in Chat
     clear();
     count = 0;
     maxCount = 5; 
     spacingCharacter = "-";
     endCharacter = "+";
     output = "";
     reverse = false;
     function Update() {
       output = "";
       if(count > maxCount || count < 0) {
         reverse = !reverse;
       }
       // If we've reached max, go backwards
       // If we've gone back to far, go forwards
       count = reverse ? count - 1 : count + 1;
       
       print(getText(count));
     }
     function getText(count) {
       charOutput = "";
       for(x = 0; x < count; x++) {
         charOutput += spacingCharacter;
       }
       charOutput += endCharacter;
       return charOutput;
      }

Get Super Voxel World

Leave a comment

Log in with itch.io to leave a comment.