Ambiera ForumDiscussions, Help and Support. |
|
|
|||||
|
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 |
||||
|
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 |
||||
|
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 :) |
||||
|
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 |
||||
|
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 |
||||
|
@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. |
||||
|
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 :) |
||||
|
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. |
||||
|
Hey smnmhmdy Thanks, now working beautifully |
||||
|
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 |
||||
|
@josevaldo, you can use this extension to easily show any variable value on a 2D overlay. https://vazahat.itch.io/coppercu... |
||||
|
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... |
|