Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Access Global Variables in different scene

nrasool
Registered User
Quote
2021-03-26 17:58:10

Hey

So I have a main scene where I created a variable, but if I go to another scene by using the switch scene command, can i access a variable in the main scene

I tried using ccbGetCopperVariable but that doesn't work

Anyone got any idea on access a global variable in different scenes which doesn't involve saving the variables into a file?

Thanks in advance


just_in_case
Moderator
Quote
2021-03-27 05:29:15

first, you need to save that variable in your first scene, the best is to save it right before your scene switches and in your second scene you use the load a variable from disk action to load that variable into your scene, best is to attach the action to before first drawing do something behavior in your second scene. that way you can pass variables from one scene to another scene. There are other methods too which require saving variables to a text file and then loading them again each time the scene starts.

hope that helps


nrasool
Registered User
Quote
2021-03-27 12:33:34

Hey just_in_case

Yeah that how I have it at the moment, doing that, was just wondering if there was any hacks so I didn't need to use the save/load from a file. Poss through javascript but I can't see a way of doing this

Thanks anyway for confirming :)


smnmhmdy
Registered User
Quote
2021-03-27 13:35:21

External script files have the biggest scope access on the whole project, meaning that the variables and/or functions stored inside them can be accessed directly from every scene.

Define your variables inside one and you can easily access them from anywhere in your game :)

To load an external script file make a new javascript file where your ccb file is stored and rename it to your projects name:
AGame.ccb
AGame.js


hadoken
Guest
Quote
2021-03-27 14:22:46

I'm also looking for easy ways to control global game variables across multiple changing active scenes. So thanks for the tip @smnmhmdy.

For getting a better understanding could you perhaps provide a mini CC project with two scenes making use of your concept (please only if your precious time allows)?

THX


smnmhmdy
Registered User
Quote
2021-03-27 14:44:20

@hadoken Of course :)https://www.dropbox.com/s/a1izj4...

As you can see, There's a variable called "test" defined in the js file.
The first scene prints the value of "test" (before first draw) and then changes it to something else.
The second scene then prints the updated value.


nrasool
Registered User
Quote
2021-03-27 14:56:10

Hey smnmhmdy

Aww nice one, This is it!!!. I was declaring my variable in JS within the scenes thinking they would be accessible outside because of it being JS.

This makes more sense now!!, Thanks matey, This is what I needed to know :)


smnmhmdy
Registered User
Quote
2021-03-27 18:34:59

Documentation - Scripting with JavaScript
When publishing your file now as Windows .exe, CopperCube will include this script into the .exe file and use it as main script.

It actually can be useful to prevent "hacking" and messing with the game using -script parameter on the exe (since you can pretty much alter anything with it). By putting your global functions in it, if the user tries to replace their own script with yours using that parameter, the game will most likely don't work as most of the functions required for it to work are missing.


nrasool
Registered User
Quote
2021-03-28 23:03:19

Hey smnmhmdy

Thanks, now working beautifully


josevaldo
Registered User
Quote
2023-10-10 11:53:16

I looked at the example and would like to know how to display this "test" variable in a 2d overley, or handle coppercube behaviors and actions


just_in_case
Moderator
Quote
2023-10-23 06:28:18

@josevaldo, you can use this extension to easily show any variable value on a 2D overlay.
https://vazahat.itch.io/coppercu...


VP
Guest
Quote
2023-10-30 12:35:52

Yes, you can set/access/modify variables between switched scenes without script/coding at all, using the built in "Set variable" actions - you can also use boolean modifiers to multiply/divide/compare variables.

You can set variable conditions and loops using built-in action: "If variable"

You can use the "Update 2D overlay" action to display any variable (from any scene) by enclosing it in-between $$ symbols.

Examples:
On proximity to "Bullet">Set variable "Health" -10
Every 500ms>Update 2D overlay $Health$
Every 1000ms>If Variable "Health" <10, Play Sound "Warning_Beep.ogg"

It doesn't matter which scene you used to create the "Health" variable, all subsequent scenes can access/modify/display it.

If you want separate variables for each scene, just create separate variables such as: Set variable "Score_Scene1" =0 and Set variable "Score_Scene2" =0 etc.

To reset a variable when switching scenes, simply add a "Set variable" action to the "Switch scene" action.

Example:

Switch scene"Start_Game"> Set variable "Health" =100> Set variable "Score" =0

In my bomb-pig game example (free on itch.io), the "Highscore" variable is never reset, this allows it to carry over every scene - but the "Score" variable is reset each time the game_scene starts.

I used the "Before First Drawing" and "Before First Drawing (and also on reload)" to control these events and the "Highscore" value is only updated when the "Score" valjue is larger than the "Highscore" value....

https://veganpete.itch.io/bomb-d...


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internati?nal" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact