# Id Handling

This chapter explains how to get access to other nodes via the id of their attached script. Therefore, create a new Scene Composer solution using the "3D Getting started" template, which includes a 3D model of a car.

To start it's important to know that there are two functions that are fundamental to get access to other nodes:

<div class="contents" id="bkmrk-candera.getscriptids"><div class="contents"><div class="textblock">- **Candera.GetScriptIds(id)** returns all ids of script components that are attached to the same node as 'id'.
- **Candera.GetChildScriptIds(id)** returns all ids of script components that are attached to any node in the subtree of the node identified by 'id'.

</div></div></div>Once you have the id of the child nodes you can get the script itself with:

<div class="contents" id="bkmrk-candera.idtoscriptco"><div class="contents"><div class="textblock">- **Candera.IdToScriptComponent(scriptId)** returns the public table of the script identified by 'scriptId'

</div></div></div>The first goal is to rotate all four wheels in the same speed and to rotate the front wheels according to the steering lock.

<div drawio-diagram="2081"><img src="https://doc316en.candera.eu/uploads/images/drawio/2023-02/drawing-4-1676958053.png" alt=""/></div>

<div class="contents" id="bkmrk--0"><div class="contents"><div class="textblock">  
</div></div></div>Therefore it is necessary that a script from the root node of the car communicates with all four wheels. But as we have already learned it's absolutely necessary that each node we want to find also has a script component. So before we start to get access to the child nodes we equip all four wheels with a script. As front wheels and back wheels have different behaviors (steering lock) create two different scripts called "Wheel\_back" and "Wheel\_front":

#### <a class="anchor" id="bkmrk--5"></a>Wheel\_back Script:

Get the current rotation of the wheel and set the new rotation with a modified x-value. In this case we add the 'rotationSpeed' value from the public table to the current x-rotation. Afterwards the public value 'rotationSpeed' will get set by the root script.

```
    local function Update(self, id)
        local pitch, yaw, roll =Candera.GetRotation(id)                                
        Candera.SetRotation(id,  pitch+self.rotationSpeed, yaw, roll) 
    end
    
    return { 
      <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a> = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>,
      Update = Update,
      GetName = GetName, 
      rotationSpeed =0.0
    }
```

To enable the root script to identify the wheel scripts it's also important that the wheel scripts get a value which contains a proper name. This value must not get changed by SceneComposer's GUI so we enable the access with a public function called GetName():

```
    local scriptName = "Wheel_back"
    local function GetName()
        return scriptName
    end
```

(don't forget to add the function to the public table: GetName = GetName, )

#### <a class="anchor" id="bkmrk--6"></a>Wheel\_front Script:

Just repeat the same steps for the front wheel with a modification in the update concerning the z-rotation which gets set to the steering lock angle.

```
    local function Update(self, id)
        local pitch, yaw, roll =Candera.GetRotation(id) 
        Candera.SetRotation(id,  pitch+self.rotationSpeed, yaw, self.steerLock) 
    end
    
    return { 
      <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a> = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>,
      Update = Update,
      GetName = GetName, 
      rotationSpeed =0.0,
      steerLock =0.0
    }
```

The GetName functions is nearly the same except a different value for the name (Wheel\_front).

The next step is to drag and drop the scripts to the proper nodes.

Now we can concentrate on the root node:

#### <a class="anchor" id="bkmrk--7"></a>CarRootController Script:

Switch back to the root script called "CarRootController". The first thing we have to do is to get the id of every script component which is attached to any child of the root and store it to a local variable which we will need later:

```
    local frontWheelScripts={}
    local backWheelsScripts={}
    local childIds = {}
    local function <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>(self, id)
        childIds = Candera.GetChildScriptIds(id)
    end
```

Now it's possible to iterate through the array "childIds" (also in the **Init-Function**) to get their scripts and to look for a special script via its name. But to be able to call their GetName function and to use the scripts afterwards in the Update-Function we first have to get access to the scripts public table via Candera.IdToScriptComponent\[childId\]. Now as you have the script you can call the GetName function to search for the desired script name. Then insert each of them to a local array which got defined above the Init-Function (shown in the previous paragraph).

```
    for i, childId in ipairs(childIds) do
        local script = Candera.IdToScriptComponent[childId]
        
        if script and script.GetName() == "Wheel_front"  then
                      
            table.insert(frontWheelScripts, script)
            --table.insert(value, array) inserts the element in the last position of the array
            
        elseif script and script.GetName() == "Wheel_back" then
            table.insert(backWheelsScripts, script)
            
        end    
        
    end
```

Now we have access to each wheel script so we can also change their public values. Therefore scroll down to the **Update-Function** where you can iterate through the scripts and change the rotationSpeed and for the front wheels also the steer lock.

```
    local function Update(self, id)
        for i, frontWheelScript in ipairs(frontWheelScripts) do
            frontWheelScript.rotationSpeed = self.rotationSpeed
            frontWheelScript.steerLock = self.steerLock
        end
         
        for i, backWheelScript in ipairs(backWheelsScripts) do
            backWheelScript.rotationSpeed = self.rotationSpeed
        end
    
    end 
```

Be sure that every node has its proper script and set the rotationSpeed and the steerLock of the root node to a proper value (20;30). If you start the execution of the script system the wheels will rotate.

<p class="callout info">**Tip:** Also check the Sample Solution for further information and scripts.</p>