# Function node question

**URL:** <https://discourse.nodered.org/t/function-node-question/85308>\
**Category:** General\
**Created:** [8 February 2024 02:19 UTC](https://discourse.nodered.org/t/function-node-question/85308 "2024-02-08T02:19:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EaB11](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@EaB11](https://discourse.nodered.org/u/EaB11)\
**Post date:** [8 February 2024 02:19 UTC](https://discourse.nodered.org/t/function-node-question/85308/1 "2024-02-08T02:19:38Z")

</div>

hi all... i have a problem im trying to solve in function node.. i have 4 cameras named "Wide" "Left" "Center" "Right". At any given time one of them is active which is put in a context variable "active"..

i want a function node (when engaged) to learn the active camera, then make a random decision to put the other 3 cameras which arent active into function node variables called cam2, cam3, cam4.. each time the node is engaged i want cam2 cam3 and cam4 to have different variations of where they "land"..

I know how to put the active camera in the function node variable (flow.get) and i have some long winded method of making a random decision for the other 3 cameras but its not very good.. wondering if anyone might know an effective way ? thanks..

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [8 February 2024 03:23 UTC](https://discourse.nodered.org/t/function-node-question/85308/2 "2024-02-08T03:23:46Z")

</div>

It might help if you share your flow so we can better understand what you are trying to do.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [8 February 2024 03:46 UTC](https://discourse.nodered.org/t/function-node-question/85308/3 "2024-02-08T03:46:01Z")

</div>

I think this is what you are looking for, let me know if you need more help ?

```auto
[{"id":"6473791628a7893b","type":"function","z":"b42d972d049a7da8","name":"function 32","func":"flow.set(\"active\", msg.payload)\n\n// active camera from flow\nlet activeCamera = flow.get(\"active\");\n\n\nlet positions = [\"Wide\", \"Left\", \"Center\", \"Right\"];\n\n// Remove the active camera\nif (activeCamera) {\n const index = positions.indexOf(activeCamera);\n if (index !== -1) {\n positions.splice(index, 1);\n }\n}\n\n// shuffle \nfor (let i = positions.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [positions[i], positions[j]] = [positions[j], positions[i]];\n}\n\n// Store values in flow variables\nflow.set(\"cam1\", positions[0] || \"\");\nflow.set(\"cam2\", positions[1] || \"\");\nflow.set(\"cam3\", positions[2] || \"\");\n\nmsg.payload = positions;\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":435,"wires":[["a0d9bfd6cec59452"]]},{"id":"eb93680a217d607c","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Wide","payloadType":"str","x":515,"y":360,"wires":[["6473791628a7893b"]]},{"id":"a0d9bfd6cec59452","type":"debug","z":"b42d972d049a7da8","name":"debug 393","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":435,"wires":[]},{"id":"2c1b86b5890cdeb5","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Left","payloadType":"str","x":515,"y":405,"wires":[["6473791628a7893b"]]},{"id":"afa3490cedfe8993","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Center","payloadType":"str","x":515,"y":450,"wires":[["6473791628a7893b"]]},{"id":"fef2735830088598","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Right","payloadType":"str","x":515,"y":495,"wires":[["6473791628a7893b"]]}]

```

---

<div class="post-metadata">

**Author:** ![EaB11](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@EaB11](https://discourse.nodered.org/u/EaB11)\
**Post date:** [9 February 2024 06:10 UTC](https://discourse.nodered.org/t/function-node-question/85308/4 "2024-02-09T06:10:32Z")

</div>

thanks i'll give it go..

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 May 2024 06:10 UTC](https://discourse.nodered.org/t/function-node-question/85308/5 "2024-05-09T06:10:59Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
