[SOLVED] Critical rate?

Basically I'm trying to implement the use of critical hits. Their damage is random, lying between critical damage and 2x critical damage. Meanwhile, normal attack lies between half of the normal attack and normal attack

So there are roughly 4 attributes involved: (player attributes, the names aren't exactly these)
- temporary attack = integer, used in battle, total damage player has, affected by both critical rate and normal attack
- critical rate = integer, used in battle, the likelihood of critical hits happening
- normal attack = integer, used in battle, the base damage player has
- critical damage = integer, used in battle

My initial attempt to do this is to use if random chance using expression instead of numbers (prob that's why it doesn't work, but idk of another way)

  this.temporary_attack = GetRandomInt(this.critical damage,this.critical_damage*2)
}
// In (critical rate)% chance, you get a random number that's between critical damage and 2x critical damage

else {
  this.temporary_attack = GetRandomInt(this.normal_attack/2,this.normal_attack)
}
// If not, then you get a random number between 1/2 normal attack and normal attack

My initial attempt to do this is to use if random chance using expression instead of numbers (prob that's why it doesn't work, but idk of another way)

That should work fine.

The if statement is missing from the code you pasted, but what I can see looks like it should be fine. Although I notice you're using both this.critical damage and this.critical_damage; is the space a typo?

If it doesn't work, it would be helpful to share what it does. Are you getting an error message from this code? Does it give an incorrect result?


Are you getting an error message from this code? Does it give an incorrect result?
There isn't any error message as far as I'm concerned. However, whenever I try to print out the temporary attack, I get 0

The if statement is missing from the code you pasted, but what I can see looks like it should be fine. Although I notice you're using both this.critical damage and this.critical_damage; is the space a typo?
Ah, yeah. I forgot the space, but I used a different variable so I think it shouldn't be...


There isn't any error message as far as I'm concerned. However, whenever I try to print out the temporary attack, I get 0

OK.
Neither of those statements should give a zero unless your attack is zero.

So, 3 more things to check:

  1. Is this code actually running? Are you sure there's not some block that prevents it getting here?
  2. Are you sure that this is the right object?
  3. Are you sure that you're printing out the value of the variable after this code is run?

I'd probably try to narrow down the problem by temporarily changing it to something like:

msg ("About to calculate temporary_attack")
foreach (attr, Split("critical_damage;critical_rate;normal_attack;temporary_attack")) {
  msg (attr+" = "+GetAttribute(this, attr))
}
if (RandomChance (this.critical_rate)) {
  msg ("Critical")
  this.temporary_attack = GetRandomInt(this.critical damage,this.critical_damage*2)
}
else {
  msg ("Not critical")
  this.temporary_attack = GetRandomInt(this.normal_attack/2,this.normal_attack)
}
msg ("Temporary attack determined as: " + this.temporary_attack)

Then you have a whole bunch of messages that should give you a better idea of where the problem lies. Once you've fixed it, you can remove all the extra messages.


I tried copying it but as soon as I run it, Quest closes itself. Is it because I used a command to print the temporary attack? And the code I mentioned before was actually from the changedcritical_rate script... I copied the message code to the command script, changed this to the proper object name


I tried copying it but as soon as I run it, Quest closes itself.

That's really strange, and I can't see how it could happen. I'll see if I can figure it out.

And the code I mentioned before was actually from the changedcritical_rate script

Does this mean that the critical_rate changes every time you attack?


Does this mean that the critical_rate changes every time you attack?

My bad. I mean changedtemporary_attack*


Ah… so what else changes the temporary attack?


Only critical_damage, normal_attack, and critical_rate for %
I can give you the complete code too through message if you want. In case the problem actually lies somewhere else


OK, now that makes sense.

I think you misunderstand what a changescript is for.

The script changedtemporary_attack will be automatically run every time temporary_attack is changed. As the attribute is staying at zero, I assume that you're not changing temporary_attack somewhere else, so that script is never run.

At some point when testing, you added a line to some other function or command that changes temporary_attack. Now, it runs that script, which immediately calculates a new value for temporary_attack. This causes the script changedtemporary_attack to be run. Which causes the value of temporary_attack to change again, which causes the script to run again.

It keeps on running that script over and over until it generates the same value twice in a row (so there's no change); or until it runs out of memory and crashes.


Ohh I see...
Yeah, it's working now

Thanks a lot :DD


Personally, I based my combat system off of old D20 rules. A dice roll of 1d20 is applied to the player's hit roll, which is then modified with the player's buffs/penalties. However, after the accuracy roll is calculated against the enemy's dodge (which sets an integer I can sum up as 'did it hit?' with 0 being fail and 1 being hit), I then check that dice roll (and subtract or add to it if the player has any critical/keen buffs).

If it's 1 or below, I change the hit check to a fail. If it's 2-19 no change is made. If it's 20 or above, I change the hit check to a 2 instead. Finally, an if script checks it; 0 misses, 1 hits, and 2 hits with some different more exciting flavour text and multiplies the damage (either by 2 or 1.5, in the latter case of which I round the resulting damage.)

Quest offers so many ways of handling random chance that there's no wrong way to do it, imo! If it works for you, it works. :)


Log in to post a reply.

Support

Forums