Ambiera ForumDiscussions, Help and Support. |
|
|
|||||
|
i made so that when a scene begins it ccbGetCopperCubeVariable("orgs") and when clicked on the thing it ccbSetCopperCubeVariable("orgs", +1) and repeted that 4 times for all the itens i nedded (it needes to be orgs = 4 to do actilon) but it doesnt work pls help |
||||
|
try ccbSetCopperCubeVariable("orgs", ccbGetCopperCubeVariable("orgs") + 1); |
||||
|
wrote: try ccbSetCopperCubeVariable("orgs", ccbGetCopperCubeVariable("orgs") + 1); You need to cast to number first. Those variables are always stored as strings and you will get '1' + 1 which is '11' not 2. |
||||
|
@okeoke is right my answer was too quick, use something like: ccbSetCopperCubeVariable("orgs", Number(ccbGetCopperCubeVariable("orgs")) + 1); or for strict integer value useccbSetCopperCubeVariable("orgs", parseInt(ccbGetCopperCubeVariable("orgs")) + 1);
|
||||
|
Background / context information: The type (number, string) that ccbGetCopperCubeVariable() returns appears to depend on the content previously set by the user. - If you use ccbGetCopperCubeVariable() BEFORE using ccbSetCopperCubeVariable() on a new variable, the variable appears to default to an empty string, which triggers the issue mentioned by okeoke. hadoken's updated approach should cleanly counter this issue. - If you initialize the variable somewhere via ccbSetCopperCubeVariable(), you can afterwards safely use hadoken's first chosen approach (without the type conversion via "Number" or "parseInt"). Example: 1) Add a behavior "Before first drawing do something" to the root node of the scene (optionally check AlsoOnReload to re-run this every time the scene is reloaded/restarted). 2) On this behavior, add an "Execute javascript" action to set your variable: ccbSetCopperCubeVariable("orgs", 0); For variables affected by the "Set or change a Variable" editor action: - When you use "Set", the variable seems to be initialized as a string, even if you specify a number as a value. This always leads to the issue described by okeoke. - However, when you use "Add" on the variable or initialize the variable via "Add", it seems to be treated as a number afterwards. Relevant code excerpts from CopperLicht to show that ccbGetCopperCubeVariable() supports "type aware" output and maps internally stored numbers to the correct format:
|
||||
|
- If you initialize the variable somewhere via ccbSetCopperCubeVariable(), you can afterwards safely use hadoken's first chosen approach (without the type conversion via "Number" or "parseInt"). Worth mentioning that if you set variable using coppercube action, not through JS api call it will always be string. I would explicitly cast anyway though:) |
||||
|
Warning: Be careful with parseInt() if the variable hasn't been initialized yet. If ccbGetCopperCubeVariable("orgs") returns an empty string "", parseInt("") results in NaN (Not a Number) (tested on the webGL build). It is safer to use Number(), if you want to use that approach. |
||||
|
|