Skip to content

05. Setting and Getting Chuck variables via ActorComponent

Eito Murakami edited this page Nov 28, 2023 · 5 revisions

Compiling ChucK code

A ChucK code can be written as a string and can be passed to the Audio Component that uses the MetaSound source as follows:

  • ChuckCode1 is the string input that we routed to Code argument of ChuckMain / ChuckSub.
  • TriggerChuck is the trigger input that we routed to RunCode argument of ChuckMain / ChuckSub.
  • (Optional) ChuckID1 is the string input that we routed to the ID argument of ChuckMain.
image

Alternatively, we prepared a ReadFile Blueprint function for loading a .ck file using an absolute path.

image

Try running the following ChucK code by following one of the methods above! Make sure to click on Compile button from the top toolbar, and place the actor component in your level. In this part of the tutorial, you can either use a ChuckMain node or a ChuckSub node. If you are using the latter, please make sure to add a MetaSound source that contains a ChuckParent node to the same level. (Simply drag and drop Chunreal/ExampleMetaSounds/Chunreal_Parent_MetaSound to the level.)

//Create an oscillator
SinOsc osc => dac;    
0.75 => osc.gain;

100 => global int basefreq;
10 => int basefreq_inc;
0 => global float freq;
25 => float freq_inc;

while(true)
{
    //Increment and wrap base frequency
    basefreq + basefreq_inc => basefreq;  
    if(basefreq>200)                      
    {
	    basefreq/2 => basefreq;
    }
        
    basefreq => freq;
		    
    for(0=>int i; i<8; i++)
    {
        //Increment frequency
        freq + freq_inc => freq;    
        osc.freq(freq);
        150::ms => now;
    }
}

Getting & Setting ChucK Global Variables Using ID

A specific ChucK instance can be accessed using its assigned unique ID.

Global variables are shared across all instances of ChuckSub nodes. They can be accessed using the ID "ChuckParent"

Let's try getting global int basefreq and global float freq back from the ChucK code above. The following Blueprint is taken from the Chunreal/ExampleBlueprints/Chunreal_Simple_BP.

image

The image below is a list of currently available ChucK global getters + setters.

image

Setting ChucK sample rate

You can manually set and get the sample rate for all instances of ChucK via the following functions:

image