140 likes | 247 Views
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.
E N D
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 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
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; } }
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"); } }
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;
Static Variable Referenced in Transmit.js // Transmit.js if(Random.value > 0.5f){ Receive.messageReceived = true; } else { Receive.messageReceived = false; } }
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.”
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
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
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 • }
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 • }
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; } • } • }
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).