How to set up Random call

Started by Icey, Tue 31/05/2011 00:12:17

Previous topic - Next topic

Icey

In my game I would like to have a damage dealt to an enemy at random. I searched through the forum but everything else has a different purpose making it hard for me to understand it.

I have this in the attack call.

Code: ags


  
if(EnemyST -= 5){
Log.AddItem("Dave attacked Behemoth.");
Log.AddItem("Behemoth lose [?] HP."); 
}
else if(EnemyST -= 0){
Log.AddItem("Attack missed .");
Log.AddItem("Behemoth lose 0 HP."); 
}



  • Attacks range from 5,6,7,8,9,10,0 [This is used if missed]
  • How do I add the random # to the game so that they can be called?
  • And how do I add the random number to the Log to replace the [?].

    Code: ags
    // new module script
    
    struct Random(6){
     int Random[0] = EnemyST -= 5;
      int Random[1] = EnemyST -= 6;
      int Random[2] = EnemyST -= 7;
      int Random[3] = EnemyST -= 8;
      int Random[4] = EnemyST -= 9;
      int Random[5] = EnemyST -= 10;
      
    };



Khris

First you calculate if the attacker missed. If you want to use a percentage, you can do this:

Code: ags
  if (Random(99) < 70)    // 70% chance of being true


Random(99) returns a random number ranging from 0 to 99, so Random(99) is less than 70 in 70 out of a hundred cases.

To get a random attack value within a certain range, calculate how many different results there are going to be to get to the parameter, then add to that.

5 - 10 are six values. Random() includes 0 and the parameter so Random(5) will give a random result in the range of 0 - 5, again six values. Now add 5 and you get from 0 - 5 to 5 - 10.

You need to store the result in a variable to reuse it because every time you call it again you'll get a different result.

Code: ags
  int attack_value = Random(5) + 5;   // 5 - 10
  bool hit = (Random(99) < 70);  // 70% chance of the hit connecting

  if (hit) {
    Log.AddItem("Dave attacked Behemoth.");
    Log.AddItem(String.Format("Behemoth lost %d HP.", attack_value));
    EnemyHP -= attack_value;
  }
  else {
    Log.AddItem("Attack missed.");
  }


Why the hell do you use "EnemyST -= 5" as CONDITION for an if statement?

Icey

I just noticed I could do that in a if statement. And I had to tweak it a bit because I messed up something before.
Code: ags
   int attack_value = Random(5) + 5;   // 5 - 10
  bool hit = (Random(99) < 70);  // 70% chance of the hit connecting

  if (hit) {
    Log.AddItem("Dave attacked Behemoth.");
    Log.AddItem(String.Format("Behemoth lost %d HP.", attack_value));
    EnemyST -= attack_value; //< Here is were I made the small tweak but I had fix this other part in a script to get this to work.
  }
  else {
    Log.AddItem("Attack missed.");
  }


Also I have one last question. How do I build up the ATB bar? You know, The wait bar? What should I use? Gui buttons but if so how do I get the bar to fill up or vanish? I plan to make it were when the bar fills up the the play can use the mouse to select a action. But do you think I should keep it active or wait mode even when the bar is full?


Khris

I tested it and you can't. I get a parse error if I try that.

What I did is use a button for the bar and change its width. You can set up the button so it'll clip the image if it's bigger than the button.
In repeatedly_execute, the width of the button is increased until the bar is full.

Active or Wait is a design decision and thus up to your preference. I recommend against doing a timed battle for now though until you have figured out the basics.
The code you appended to your original post is - frankly speaking - an atrocity, so until you have implemented a bug free round-based combat system, don't do things you can't without help.

I also told you in the PM to write this code so it can be universally used, but you are using "Dave" and "Behemoth" in there.
You are supposed to store these names in struct instances holding players' and enemies' data, then reference those.

Icey

I don't know what to say. I mean I got it to work.

I will try that for thee ATB bar.

I think I will stick with wait then.

Well you see I was thinking that it would be easier to make a separate
topic rather then blow up you inbox with RPG question. I made a script with this stuff but I had questions and nobody answered so I moved back to this. However if i get help with my RPG module then I will switch back. Trust me, I rather use my module then this way.

SMF spam blocked by CleanTalk