Quiz Game - Counter Function


#1

Hello ZapForum.
I wonder if anyone could help with the following:
I have a quiz game with 10 questions… I don’t normally code so it is a pain really…
I was sent an example of a counter by the lovely support team at Zappar, thank you but I wonder how I can use one script (one function ) and call this function in every question? as support says:
So how to create a a function in a script, and then call it from other scripts in your hierarchy.
if anyone has an example of how to do this please let me know .

Milenne


#2

To make a function, or a variable , accessible from other scripts, you have to "export " it.
Let’s say, you have a blank script named " code " in your hierarchy.
Inside " code " you declare your function myTimer() with the keyword export:

export function myTimer(){ ... }

Then to invoke myTimer() from another script, you just have to give the whole path to the function:

symbol.nodes.code.myTimer();

Of course, you will have also to take care of the timer value.

So, add the variable in " code ":

export var timer;
export function myTimer(){ ... }

And in your other script:

symbol.nodes.code.timer = 10;
Z.every(1000,symbol.nodes.code.myTimer);

Happy coding! :smile:


#3

Hey jvouillon. Thank you. The timer example you sent me works perfectly. :wink:
Now I am looking for a counter. I have it working by putting a code in every of the 10 questions if answer correctly then it adds a point and at the end it has to show how many questions were right. I just don’t know where I need to put things and one script only. So I understand the above. I will try that and instead of timer use the code that i am using in each screen to add a point. I will let you know if that works .
Thanks Again
Milenne


#4

Oh, my mistake! I thought to wanted to call the timer from differents scripts… :slight_smile:

A counter is nothing more than a variable that you increment on each good answer… no need for a function.


#5

Thanks. Yes, but I still want to increment only one variable and not having to use the same script where I declare a new variable every time.
The support team has sent me an email at the same time with some instructions very similar to yours. So I decided to have a go and it worked :wink:
So thank you both of you. :slight_smile:
incrementScore3_function.zpp (2.7 MB)