# Global variable alternative

**URL:** <https://in-thread.sonic-pi.net/t/global-variable-alternative/2368>\
**Category:** \[P\] Patreon Support\
**Created:** [May 8, 2019, 10:00am UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368 "2019-05-08T10:00:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [May 8, 2019, 10:00am UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/1 "2019-05-08T10:00:33Z")

</div>

Hi  
I am trying to send a filename to SP using OSC and then using the file in the program. The idea is something like the draft below. This does not work. Also any suggestions on how this can be done better alternatively without using global variable? Thanks

```
$filePath=""

live_loop :a do
   use_real_time
  oscValues = sync "/osc/trigger/setname"
  samp = oscValues.join(";")
  $filePath = "C:/AUDIO/" + samp
end

live_loop :c do
  sample $filePath
  sleep 1
end
```

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [May 8, 2019, 12:54pm UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/2 "2019-05-08T12:54:23Z")

</div>

Hi @keys,

perhaps you might be interested in reading about Sonic Pi’s global time state and the functions `get` and `set`: [http://sonic-pi.net/tutorial.html#section-10](http://sonic-pi.net/tutorial.html#section-10)

```ruby
live_loop :a do
  use_real_time
  osc_values = sync "/osc/trigger/setname"
  samp = osc_values.join(";")
  set :file_path, "C:/AUDIO/" + samp
end

live_loop :c do
  sample get(:file_path)
  sleep 1
end

```

In fact, incoming OSC messages are inserted directly into the Time State system, so there’s no need for an explicit `set`:

```ruby
live_loop :c do
  sample "C:/AUDIO/" + get("/osc/trigger/setname").join(";")
  sleep 1
end

```

However, I’m unsure what you’re doing with the values of that OSC message - specifically why your’e joining them with `";"`. Could you explain what you intend this code to do?

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [May 8, 2019, 4:27pm UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/3 "2019-05-08T16:27:52Z")

</div>

Thanks for the advise. I will read up on get/set 🙂

Ok what I am trying to do is simply to send a filename to Sonic Pi using OSC and then playback the file. The join(";") part was something I found on the Internet when trying to convert the array in the osc message to a string. But I think I need to read up on join as well 😉

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [May 8, 2019, 6:06pm UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/4 "2019-05-08T18:06:35Z")

</div>

I have still not made it working. I also tried to use the osc directly but I get an error message on the join function

```
live_loop :d do
  sample "C:/AUDIO/" + get("/osc/trigger/setname").join
  sleep 1
end

```

_Error message_  
Runtime Error: [buffer 0, line 54] - NoMethodError  
Thread death ±-\> :live\_loop\_d  
undefined method `join' for nil:NilClass Did you mean? JSON workspace_zero:54:in`block (3 levels) in \_\_spider\_eval’

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [May 8, 2019, 7:52pm UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/5 "2019-05-08T19:52:04Z")

</div>

Ok I did some more testing and i got it working sending the message into Sonic Pi from another client. Hence the issue seems to be external to Sonic Pi. I will investigate this further.

Thanks for the advise regarding get/set and for the heads up concerning join 🙂

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [May 9, 2019, 9:14am UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/6 "2019-05-09T09:14:53Z")

</div>

With respect to joining, if your OSC data is coming in as a list of separate characters, then you can join without specifying an argument:

```ruby
a = ['h', 'e', 'l', 'l', 'o']
puts a.join #=> "hello"

```

If you specify an argument, then it will place that argument between each individual letter:

```ruby
a = ['h', 'e', 'l', 'l', 'o']
puts a.join(";") #=> "h;e;l;l;o"

```

If you’re controlling the code that’s creating and sending the original OSC message, it might make more sense to send the argument as a string of multiple characters which would mean you wouldn’t need to join anything at all.

It’s also worth noting that `.join` is a Ruby feature and is currently unsupported in any way (as are most Ruby features). This doesn’t mean you shouldn’t use them, but it does mean that they may stop working without notice in a future release. Only the functionality presented in the documentation is officially supported 🙂

---

<div class="post-metadata">

**Author:** ![keys](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@keys](https://in-thread.sonic-pi.net/u/keys)\
**Post date:** [May 10, 2019, 12:12pm UTC](https://in-thread.sonic-pi.net/t/global-variable-alternative/2368/7 "2019-05-10T12:12:27Z")

</div>

Thanks for explaining the .join feature to me 🙂

Also thanks for warning me about the fact that this is a Ruby feature. At the same time I hope these features will not dissappear, because I think it is one of Sonic Pi’s strength that one can find general Ruby code on the Internet and implement it in the Sonic Pi code.
