ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator Variables and If conditional/staement
person icon
veganpete
Registered User
Quote
2023-06-21 22:36:02

How would I achieve something like this in Script/Coppercube/Java please?

I know I can do it without coding - but it will take forever to do - I've done it twice before, for all 195 countries, and then accidentally lost the work emoji icon_cry.....

var a = ccbGetCopperCubeVariable($Country$);
var s = ccbGetSceneNodeFromName("Country(Stats)");

if (a == "1"), ccbSetSceneNodeProperty(s, "Text", AFGHANISTAN);
if (a == "2"), ccbSetSceneNodeProperty(s, "Text", ALBANIA);
if (a == "3"), ccbSetSceneNodeProperty(s, "Text", ALGERIA);
if (a == "4"), ccbSetSceneNodeProperty(s, "Text", ANDORRA);
if (a == "5"), ccbSetSceneNodeProperty(s, "Text", ABGOLA);
if (a == "6"), ccbSetSceneNodeProperty(s, "Text", ANTIGUA & BARBUDA);
if (a == "7"), ccbSetSceneNodeProperty(s, "Text", ARGENTINA);
if (a == "8"), ccbSetSceneNodeProperty(s, "Text", ARMENIA);
if (a == "9"), ccbSetSceneNodeProperty(s, "Text", AUSTRALI);
if (a == "10") ,ccbSetSceneNodeProperty(s, "Text", AUSTRIA);

Obviously it doesn't work - it just says "Syntax Error". I checked W3chool -but I can't see how to convert it to something that will work with Coppercube's API.....

Their example is...
<h2>JavaScript if</h2>
<p>Display "Good day!" if the hour is less than 18:00:</p>
<p id="demo">Good Evening!</p>
<script>
if (new Date().getHours() < 18) {
document.getElementById("demo").innerHTML = "Good day!";
}
</script>

I kinda know what I want to do, just don't know how to do it or how to learn how to do it. If it's not possible using Coppercube's API, I'll just do it again with non-coding using every few seconds, if variable = 1,change 2D overlay

Thanks.

person icon
sven
Registered User
Quote
2023-06-21 23:09:41

i would use ARRAY..

store countries list into array:

//On before draw (execute javascript)
//List of countries in array.
country=[];
country[1]="Germany";
country[2]="Canada";
country[3]="India";
country[4]="USA";
etc..

Then i would execute javascript on each 100ms (or whatever interval you need)

var a = ccbGetCopperCubeVariable("CountryID"); //number
var nodeptr = ccbGetSceneNodeFromName("2dOverlay");
ccbSetSceneNodeProperty(nodeptr, 'Text', country[a] );

There is no need for IF statement at all.



if you want to use IF statement then:

var a = ccbGetCopperCubeVariable("CountryID"); //number
var nodeptr = ccbGetSceneNodeFromName("2dOverlay");

if(a==1)
{
ccbSetSceneNodeProperty(nodeptr, 'Text', "Germany" );
}

etc..

person icon
VP
Guest
Quote
2023-06-21 23:18:22

Nice one Sven! I'll give it a try and use the ARRAYs.

Appreciate your help! Thank you.

person icon
sven
Registered User
Quote
2023-06-21 23:24:19

You get all countries list here:
https://www.html-code-generator.com/javascript/array/country-names

it generates you the array of countries :D

person icon
VP
Guest
Quote
2023-06-22 00:47:45

Thanks again Sven.

Not sure how to get Arrays the working but will try using IF statements instead.

person icon
VP
Guest
Quote
2023-06-22 01:03:33

Same error with the If statements.

I'll just do it the long way with individual actions.
F****ing dyslexia. emoji icon_cry

person icon
sven
Registered User
Quote
2023-06-22 01:17:47

send me your script so i can see where you are doing error..

i can make script for you if you want (its simple script so doesnt take long to make)


Here:
https://drive.google.com/file/d/...

person icon
VP
Guest
Quote
2023-06-22 12:31:42

Thank you for doing that Sven!

Yours works perfectly. When I tried to do it myself, it doesn't work - it says "syntax error:syntax error: Script 3: Reference Error: Country is not defined."

Would you mind trying to find what I'm doing wrong please? Although they're just a few lines of code, I've obviously managed to make a mismatch/typo error between the 2 scripts.

Here's my ccb file...

https://drive.google.com/file/d/...

Thank you.

person icon
sven
Registered User
Quote
2023-06-22 12:42:26

Error @line 108:

Country107[]="Maldives (Male)";

should be Country[107]=

i recommend you to use this site:
https://esprima.org/demo/validat...

Put your javascript in box and it tells where error is.. error you made is so tiny but hard to discover without proper methods :D

Also i recommend you to use Notpad++ to write code.. if file you write has .js extension then code will be colored and its simple to detect errors visually.. later just copy code from it to coppercube editor.

person icon
arikus
Registered User
Quote
2023-06-22 16:24:16

How would this work??? To move a player from one location to another, how would I write that, ccbAICommand moveto code: NM?" as an example.
Would I write that right after the code was declared
{name: "Colombia",code: "CO"},
//ccbAICommand? moveto code:CO??
// between each line???

or after the whole array???

{name: "Comoros",code: "KM"},
{name: "Congo - Brazzaville",code: "CG"},
//moveto code "KM"
//moveto code "CG"

person icon
VP
Guest
Quote
2023-06-22 16:50:19

Thank you Sven! Nice one. Appreciate your help.

person icon
VP
Guest
Quote
2023-06-22 16:53:46

Yay! Works like a charm. Thank you for teaching me how to use arrays - makes sense - that will save me a lot of time in the future.

person icon
just_in_case
Moderator
Quote
2023-06-23 13:02:01

@VP, I am glad that you started in scripting, it will ease out the work for you a lot. and you will be able to create amazing stuff with scripting.

person icon
VP
Guest
Quote
2023-06-24 19:29:51

Thanks, yes - I think scripting is the best way forward for me. I'll take the time to get used to it and explore the API.


Create reply:










 

  

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


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






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