# Chapter 21, SCNAction reference or value

**URL:** <https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521>\
**Category:** 3D iOS Games by Tutorials\
**Created:** [January 11, 2019, 9:06am UTC](https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521 "2019-01-11T09:06:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergeydi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sergeydi/32/81494_2.png) [@sergeydi](https://forums.kodeco.com/u/sergeydi)\
**Post date:** [January 11, 2019, 9:06am UTC](https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521/1 "2019-01-11T09:06:12Z")

</div>

Hi,  
In Chapter 21 in **Coded actions** we do:

```
var driveLeftAction: SCNAction!
var driveRightAction: SCNAction!

driveLeftAction = SCNAction.repeatForever(SCNAction.move(by: SCNVector3Make(-2.0, 0, 0), duration: 1.0))
driveRightAction = SCNAction.repeatForever(SCNAction.move(by: SCNVector3Make(2.0, 0, 0), duration: 1.0))

 for node in trafficNode.childNodes {
  // 3 Let vehicle drive towards its facing direction
  if node.eulerAngles.y > 0 {
    node.runAction(driveLeftAction)
  } else {
    node.runAction(driveRightAction)
  }
}

```

As **SCNAction** is a subclass of **NSObject** and it is reference type, so each  
`node.runAction(driveLeftAction)`  
will execute the same **SCNAction** instance.  
The question is why when we change speed, we change it for instance used all nodes of the one side:

```
if node.name?.contains("Bus") == true {
    driveLeftAction.speed = 1.0
    driveRightAction.speed = 1.0
  } else {
    driveLeftAction.speed = 2.0
    driveRightAction.speed = 2.0
  }

```

It is not changed for both bus and cars?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 11, 2019, 11:37am UTC](https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521/2 "2019-01-11T11:37:50Z")

</div>

@chrislanguage Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![sgerrard](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sgerrard/32/93812_2.png) [@sgerrard](https://forums.kodeco.com/u/sgerrard)\
**Post date:** [January 12, 2019, 4:38am UTC](https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521/3 "2019-01-12T04:38:00Z")

</div>

I believe that SCNAction, as an NSObject, does “copy-on-write” semantics, meaning that if you assign it to one node, then change something like the speed, you actually get a new object that is a copy of the old one with the new speed value. So you are actually creating a number of SCNAction objects as you go through the loop.  
It is not easy to pin down in documentation that it is copy-on-write, but I have seen that in other code examples and online discussion posts.

---

<div class="post-metadata">

**Author:** ![sergeydi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sergeydi/32/81494_2.png) [@sergeydi](https://forums.kodeco.com/u/sergeydi)\
**Post date:** [January 12, 2019, 6:10am UTC](https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521/4 "2019-01-12T06:10:48Z")

</div>

@sgerrard Thank you, I understand.  
I found, that SCNAction conforms to **NSCopying** ([Apple Developer Documentation](https://developer.apple.com/documentation/scenekit/scnaction))  
So copy of SCNAction can be done. But I couldn’t found any source code of SCNAction to be sure, that it really copy SCNAction object during **runAction(\_)** method

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [June 27, 2019, 1:06am UTC](https://forums.kodeco.com/t/chapter-21-scnaction-reference-or-value/59521/5 "2019-06-27T01:06:14Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
