1 / 14

3D Technologies for the Web

3D Technologies for the Web. Scripting Fundamentals (review). Agenda. Review of the Transmit – Receive Scenario Adding code to allow the Receive script to communicate with the Transmit script, to signal when to stop sending messages once a condition is met. . Inter-Script Communication. Cube.

Download Presentation

3D Technologies for the Web

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3D Technologies for the Web Scripting Fundamentals (review)

  2. Agenda • Review of the Transmit – Receive Scenario • Adding code to allow the Receive script to communicate with the Transmit script, to signal when to stop sending messages once a condition is met. 

  3. Inter-Script Communication Cube Sphere Transmit Receive Transmit.js Code to send message to Receive.js attached tocube object Receive.js Code to handle message sent by Transmit.js attached to sphere object

  4. Transmit.jsAttached to Sphere Object #pragma strict // example of script - to - script communication // this script transmits messages to the script attached to the cube function Start () { } function Update () { if(Random.value > 0.5f){ Receive.messageReceived = true; } else Receive.messageReceived = false; } }

  5. Receive.jsAttached to Cube Object #pragma strict /// this script receive messages from the transmit script attached to the sphere static var messageReceived:boolean;// important! static variable var numberOfMessages:int; function Start () { messagesReceived = false; numberOfMessages = 0; } function Update () { if (messageReceived == true){ numberOfMessages++; Debug.Log("I have received " + numberOfMessages + " messages from the transmit script"); } else { Debug.Log("No messages received"); } }

  6. Static Variables • Declaring variables as static make them available to be referenced by other scripts in the 3D environment. • In programming parlance static variables have global scope // static variable declaration in Receive.js static varmessageReceived:boolean;

  7. Static Variable Referenced in Transmit.js // Transmit.js if(Random.value > 0.5f){ Receive.messageReceived = true; } else { Receive.messageReceived = false; } }

  8. Using the Receive Cube to Control the Transmit Sphere Question: “Think about how to make the Cube Object tell the Sphere Object to stop sending messages after a given number of messages has been received.”

  9. Stopping the Sphere Sending Messages Cube Sphere Transmit Receive Transmit.js Code to send message to Receive.js attached tocube object Receive.js Code to handle message sent by Transmit.js attached to sphere object

  10. Using the Receive Cube to Control the Transmit Sphere Messages • Need to declare a static variable in the Transmit script • Add code to the Receive script to communicate with the newly added static in variable in Transmit.js when a condition is met • Add some code to the Transmit script to control the sending of messages

  11. Using the Receive Cube to Control the Transmit Sphere • Adding a static variable in the Transmit script: • static varsendMessage:boolean; • // initialization function • public Start(){ • sendMessage = true; // assume messages will be sent • }

  12. Using the Receive Cube to Control the Transmit Sphere • Adding code to the Receive script to communicate with the static variable in the Transmit script when a condition is met (number of messages beyond some limit): • if (messages > 1000){ • Transmit.sendMessages = false • }

  13. Adding code to the Transmit script to stop sending messages when a condition is set by the Receive script: • public Update(){ • if(sendMessage == true){ • if(Random.value > 0.5f){ • Receive.messageReceived = true;else{ • Receive.messageReceived = false; } • } • }

  14. Summary • One static variable enables one-way (simplex)communication between scripts • Creating static variables in two scripts enables two-way (duplex) communication • This could be extended so one or several scripts could update values in other scripts as required • In a complex system, one script would act as the ‘control centre’ to handle and coordinate events occurring inside the 3D environment, (e.g. First Person Controller).

More Related