Page 1 of 1

[EOS] creating a subroutine

Posted: Sun Jul 23, 2023 7:31 am
by ritewriter
Hey, does anyone know if there’s a way to create a callable subroutine that will run a series of commands. Something I can create in init then call with an eval?

I’m trying to have something that will run this when triggered:

Eval
rand=Math.floor(Math.random() * (100 - 1 + 1)) + 1;

If rand<11

>Then (Eval willcheck=) (Say “Critical Success!”)

>Else

>>If rand>90

>>>Then (Eval willcheck=0) (Say “Critical Failure!”)

>>>Else

>>>>If rand>will

>>>>>Then (Eval willcheck=0)

>>>>>Else (Eval willcheck=1)


I apologize for the terrible formatting but it was hard to represent the nested if/thens in the forum formatting.

Re: [EOS] creating a subroutine

Posted: Sun Jul 23, 2023 8:40 am
by Thamrill
ritewriter wrote: Sun Jul 23, 2023 7:31 am Hey, does anyone know if there’s a way to create a callable subroutine that will run a series of commands. Something I can create in init then call with an eval?

I’m trying to have something that will run this when triggered:

Eval
rand=Math.floor(Math.random() * (100 - 1 + 1)) + 1;

If rand<11

>Then (Eval willcheck=) (Say “Critical Success!”)

>Else

>>If rand>90

>>>Then (Eval willcheck=0) (Say “Critical Failure!”)

>>>Else

>>>>If rand>will

>>>>>Then (Eval willcheck=0)

>>>>>Else (Eval willcheck=1)


I apologize for the terrible formatting but it was hard to represent the nested if/thens in the forum formatting.
If you need to post code, you can use the code display (</> button between quotes and list). In the initialization script write:

Code: Select all

text="";
willCheck=0;

function willCheckFunction(){
  rand=Math.floor(Math.random() * (100 - 1 + 1)) + 1;
  text="";
  willcheck=0;
  if(rand<11){
    text="Critical Success!";
    willcheck=1; //I guess, it's missing in you code
  }else if(rand>90){
    text="Critical Failure!";
    willcheck=0; 
  }else if(rand>will){
    willcheck=0;
    // text="Failure!"; Read comment below
  }else{
    willcheck=1;
    // text="Success!"; Read comment below
  }
}
To use this, in an Eval action set the script to willCheckFunction(); follow it with a Say action with the eval tag wrapping the text variable, if you want to say the text. This will be played even if there is no text, so it may be better to place a text for all4 possible outcomes (remove the "//" in front of the two comments I added above, which set the text)

Re: [EOS] creating a subroutine

Posted: Sun Jul 23, 2023 5:00 pm
by ritewriter
Thank you! You're my hero.

I'm rethinking the dialogue inside the function and I think I'll replace it with a variable. Does this look right:

Code: Select all

critfail=0;
critwin=0;
willCheck=0;
rand=0;

function willCheckFunction(){
  rand=0;
  rand=Math.floor(Math.random() * (100 - 1 + 1)) + 1;
  critfail=0;
  critwin=0;
  willcheck=0;
  if(rand<11){
    critwin=1;
    willcheck=1; 
  }else if(rand>90){
    critfail=1;
    willcheck=0; 
  }else if(rand>will){
    willcheck=0;
  }else{
    willcheck=1;
  }
}
UPDATE: Tested and it, had an issue where rand seemed to carry over from previous checks (but might've been another issue) added the rand=0 initializer to the function (but also fiddled with the followup if/then which might've been the problem.

Now it works like a charm.

Thanks again and please let me know if anything looks wonky.

Re: [EOS] creating a subroutine

Posted: Sun Jul 23, 2023 7:46 pm
by ritewriter
I also tried to create a function for will loss and it tested properly. Does this look right?

Code: Select all

function willLoss(){
  rand=0;
  rand=Math.floor(Math.random() * (6 - 1 + 1)) + 1;
  willmin=0;
  if(will>10){
    will=will-rand; 
  }else{
    willmin=1;
  }
}
Thanks again!

Re: [EOS] creating a subroutine

Posted: Mon Jul 24, 2023 9:59 am
by Thamrill
ritewriter wrote: Sun Jul 23, 2023 7:46 pm I also tried to create a function for will loss and it tested properly. Does this look right?

Code: Select all

function willLoss(){
  rand=0;
  rand=Math.floor(Math.random() * (6 - 1 + 1)) + 1;
  willmin=0;
  if(will>10){
    will=will-rand; 
  }else{
    willmin=1;
  }
}
Thanks again!
I don't know what it's supposed to do, so it's difficult to say if it's correct or not.

Right now what it does is:
- if the will is above ten, it reduces it by a number between 1 and 6; the minimum is 0;
- if it is 10 or less, it sets the minimum to 1 but leaves the will at the same value

If this is what it's supposed to do, then it is correct, otherwise, no

Re: [EOS] creating a subroutine

Posted: Mon Jul 24, 2023 7:25 pm
by ritewriter
Thamrill wrote: Mon Jul 24, 2023 9:59 am
Right now what it does is:
- if the will is above ten, it reduces it by a number between 1 and 6; the minimum is 0;
- if it is 10 or less, it sets the minimum to 1 but leaves the will at the same value

If this is what it's supposed to do, then it is correct, otherwise, no
willmin is a flag that will is at or below minimum, so yep, looks like I got it right. By which I mean I just copied and pasted your code, then, by some miracle, modified it without managing to screw it up. It definitely seems to be working as intended.

Thank you again for everything!