Getting a variable value from a different object?


#1

Hello there!

I have two buttons, a plane that displays a circle of 10 numerals, and a plane that displays an icon-picture.

The first button, when clicked, rotates a circle of numerals 36 degrees. This button also keeps track, in a variable, of which number is at the top of the circle. Let’s call it topnumber.

The second button, when clicked, will change the icon-picture texture, dependent on which numeral is at the top of circle.

What I want to do is …

When the second button is clicked, it grabs the value of topnumber (from the first button’s script), and then changes the texture of the plane that displays the symbol.

**So a question I have is …

How would I refer to a variable, in the second button’s scripting, that exists in the first button’s scripting?

**And another question I have is …

How would change the texture of an object from within the scripting of a different object? I was able to change the rotation of an object within the scripting of a different objects by using this:

numbercircle.rotation([0,0,-36])

…but I’m not sure how to do this same thing, that will change the texture …

Thanks!

THE ANSWERS:

Thanks to Stevesanerd and George, I found the answers:

how to grab a variable into a script that exists in a different script

When you identify the variable in a script, you must write:

export let topnumber = 0

And then when you grab that variable from within a different script:

-you must first add the script-with-the-variable is, as a constant at the top of the script-you-want-to-grab-the-variable.

-then you refer to that script like this: symbol.nodes.scriptwithvariablename.variablename

"how to change a plane’s material property:

icon_picture.skin(symbol.mediaFiles.num01b_png);

I found reference to this here:
[How to change plane 'materials' property (texture) from my script]


#2

Your going to need to use export to send the data. Take a look at Sab’ s code at the end of this post.

https://forum.zap.works/t/changing-a-3d-objects-skin-material-with-button-press/1427


#3

Hi Steve, there was a link to another post in that thread that answered my question about how to change a texture by using this:

icon_picture.skin(symbol.mediaFiles.num01b_png);

Thanks for that!

I looked through to the end of the thread as you suggested and read through the example given by Sab. I wasn’t able to glean an answer to my first question from it though … perhaps I need to comprehend a little more.

I was hoping that you could import a variable somehow by referring to the script the variable comes from within the script you want to use it … something like scriptname.variablename … Forgive me for my clumsy lack of comprehension. I’ll keep looking at it.


#4

Hi @shawnjoh,

Well done for working out the first part!

What you need for the second is subsymbol communication, where you can export a variable from one script, for it to then be accessed from another.

So you export from the first script -

export let myVariable;

and import within the second, creating a reference to the variable ^ -

symbol.nodes.myScript.myVariable;

You can also get the reference of an object/script/plane etc. by dragging and dropping it into a script. Once you have the reference, you can initiate changing the material/texture/skin.

An example of this can be seen below -

You can find out more information on subsymbol communication on our documentations pages at: Subsymbol communication.

Hope this helps,

George


#5

Hi George, and thank you!

Is “let” absolutely necessary? I started using the word export and the debugger accepted that.

I fear I’m also having trouble with if checks on one of my scripts, but working fine on the others, so I’m missing something here. I wonder if you might give it a check for me?

note: the green button advances the inner ring, the blue button advances the outer ring. And the red button is supposed to transfer the number at the top of the inner ring to replace the symbol at the top of the outer ring.

Here is the file, if you have a moment:

Magic Circle Test 2a.zpp (1.7 MB)


#6

Hi @shawnjoh,

I think I understand what you are trying to do and you have a clever idea, but the execution of the code is not quite there.

You seem to have a lot of functions within your scripts, but they are never being invoked (called) meaning that they and the code within them will never run.

When looking at your code, I have come to presume that you want -

When the red button is touched
Get the bottom number
Change the question mark holder to that bottom number

If this is correct, you could hold all of your media files (images) within an array and iterate through them using a sequential for loop. This is better practice than having multiple if statements running and will drastically reduce the amount of code.

Also within the blue and green pointer down scripts, resetting the number back to 0 once it is over 10 can use less than 10 condition.

For example -

if (outrot < 10) {
outrot=outrot+1
}
else{
outrot=0
}

If you are still struggling to understand this you should check out our documentation on ’Typescript Primer’ as this gives a general overview of typescript with some helpful links at the end!

W3Schools and Codecademy also have some great Javascript resources!

Hope this helps and look forward to seeing the experience!

George


#7

Thanks George, I’ll try that. I admit being naive about function use. I will try what you are suggesting, but I’m curious, is nesting if loops (without the function part) possible? I tried them without encasing them in a function, and it didn’t work.