Problem with the random function.

Started by anime2019master, Wed 30/06/2010 00:21:48

Previous topic - Next topic

anime2019master

I am making a battle system for my game and i plan to use the random function to decided weather of not stepping on a hotspot will trigger a battle and transport to another room. I also want to use in a GUI I made so if you press the attack button there is a random chance that you will score a hit; likewise with defense. My problem is, all I can find in the manual and what little I can find in the forums say to use scripts like

int ran=Random(2);
if (ran==0) cEgo.ChangeRoom(1);
else if (ran==1) cEgo.ChangeRoom(2);
else cEgo.ChangeRoom(3);

Problem with that is the scripting "(ran==0) errors out. "ran" is not acceptable, nor will the statement work with out it. Can anyone help? I'm using AGS version 3.1.2

Khris

I assume you put this directly into a script, as opposed to inside a function.

Declarations, like the first line of code, will work outside; the rest however must be inside a function's body.

For events to be triggered if the player steps on an area, use regions rather than hotspots. In the events tab of the region properties, select the event and create the function by clicking on the ellipses button.
This adds something like this to the room script:

Code: ags
function region1_WalkOn() {

  // code goes here

}

anime2019master

I did place it inside a function. this is what I have im my globalscript.

function Defend1_OnClick(GUIControl *control, MouseButton button)
{
  Random(2);
}

I can't add the "int" before the word random nore anything after it. If I do when I hit F5 it calls it an error. Each time I try to type the word "Run" in the line "(run==0)" the game gives me the option to use the line Random. If I just ignore it an click the = sign, it completes the word anyway. If i simply paste the code on there it errors out.

Gilbert

Hmmm. Have you tried using a variable with a different name? Maybe ran is defined elsewhere?
I'm not quite sure as the codes seem fine. It's worth checking the above problems though.

anime2019master

Does that mean that I need to create the outcome for each possibility? I would like for it to say something like "you missed" on all but 1 or 2 outcomes, and both of those would score a hit, would notify you, and would deal the damage using global int.
I haven't named anything even remotely close to "run" if thats what u meant. I name things Like oBed1, oBed2, and so on, but nothing is close to one of the command lines like Random.

If I can't use random, is there anyway to use Global int to acheive the same function?

Gilbert

I just meant you try to change ran to some other name and try again, i.e. something like:
Code: ags

int blah=Random(2);
if (blah==0) cEgo.ChangeRoom(1);
else if (blah==1) cEgo.ChangeRoom(2);
else cEgo.ChangeRoom(3);


anime2019master

That worked! I changed it to the word test and is went through. Now i just nedd to ask if I can put multiple Else if statments.

If //something
else if //do this
else if //do this
else if //do this
else //do this

Is this how i'd plan what would happen when i use higher than random(2)?

Gilbert

Yes. Just chain else ifs if you have many conditions. Like:
Code: ags

if (a==0) do this;
else if (a==1) do that;
else if (a==2) do more;
else if (a==3) do less;
...


Khris

Quote from: anime2019master on Wed 30/06/2010 01:38:15Each time I try to type the word "Run" in the line "(run==0)" the game gives me the option to use the line Random. If I just ignore it an click the = sign, it completes the word anyway. If i simply paste the code on there it errors out.

If the auto-complete window offers something you don't want, just press Escape, move the cursor using the arrow keys or press space; int ran = Random(2); will work, too.
Pasting the code worked fine for me (except my test game only has one room, so triggering the event made the game crash).

Btw, if you want outcomes that aren't equally likely, you can do this:

Code: ags
  int rn = Random(99);

  if (rn < 10) {
    // 10% chance
  }
  else if (rn < 40) {
    // 30% chance
  }
  else {
    // 60% chance
  }

SMF spam blocked by CleanTalk