ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator Help please, my attack/defend script...
person icon
veganpete.
Registered User
Quote
2024-06-06 18:25:33

Hi, I've managed to create a small (non-working) script, to check each turn if the player is attacking or defending, then apply harm according to a dice-throw total. I was hoping it would work but the issue I think I'm having is related to defining variables - I'm not sure if they're meant to be in quotations, brackets, square-brackets etc. I've tried various combinations with no luck. Pretty sure this will be obvious to anyone who codes regularly. Hopefully I'm on the right tracks - any tips to point me in the right direction please? Syntax validator just goes nuts - probably because the whole thing is a mess.

//attack and defend mechanics

if (Total) > (defensescore) //player attacks enemy
{
ccbSetCopperCubeVariable("enemyharm", "Total" - "defensescore"); //calculate harm to enemy
ccbSetCopperCubeVariable("enemyHP", "enemyHP" - "enemyharm"); //apply harm to enemy
};

if (Total) < (defensescore) //enemy attacks player
{
ccbSetCopperCubeVariable("playerharm", "attackscore" - "Total"); //calculate enemy attack strength
If (playerharm) > 0 //check if enemy attack strength is greater than player defense
{
ccbSetCopperCubeVariable("playerHP", "playerHP" - "playerharm"); //apply player harm
};
};


Thank you!

person icon
o139
Registered User
Quote
2024-06-06 19:23:32

hi VP here is the correct code not tested though

//attack and defend mechanics

if (Total > defensescore) //player attacks enemy
{
ccbSetCopperCubeVariable("enemyharm", Total - defensescore); //calculate harm to enemy
ccbSetCopperCubeVariable("enemyHP", enemyHP - enemyharm); //apply harm to enemy
};

if (Total < defensescore) //enemy attacks player
{
ccbSetCopperCubeVariable("playerharm", attackscore - Total); //calculate enemy attack strength
};

If (playerharm > 0) //check if enemy attack strength is greater than player defense
{
ccbSetCopperCubeVariable("playerHP", playerHP - playerharm); //apply player harm
};



person icon
VP
Guest
Quote
2024-06-06 19:52:23

Thank you o139! Much appreciated. I'll give it a test and hopefully it will work now.

person icon
Guest
Guest
Quote
2024-06-07 00:33:01

It would likely be better if you avoid doing arithmetic directly when setting the variable like this. It's easier to see what is happening if you maintain a separation of concerns for each line. Lines cost nothing in the end. The line that sets the variable should do just that: set the variable. Here's an example:
if (Total > defensescore) {  //player attacks enemy
var enemyharm = Total - defensescore; //calculate harm to enemy
ccbSetCopperCubeVariable("enemyharm", enemyharm);
var newEnemyHP = enemyHP - enemyharm; //apply harm to enemy
ccbSetCopperCubeVariable("enemyHP", newEnemyHP);
}

if (Total < defensescore) { //enemy attacks player
var playerharm = attackscore - Total; //calculate enemy attack strength
ccbSetCopperCubeVariable("playerharm", playerharm);
}

if (playerharm > 0) { //check if enemy attack strength is greater than player defense
var newPlayerHP = playerHP - playerharm; //apply player harm
ccbSetCopperCubeVariable("playerHP", newPlayerHP);
}
This approach is more readable, easier to debug, and easier to change if needed in the future. This is mainly style-related but is still good practice.


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 |