Random Function

I am new to Squiffy here, and I am currently working with the online browser. I am creating a battle simulator, but I'm not sure how to do the random, to see if the character will hit or miss the object it is aiming for. Any help?


I think that for something like that you will probably want to use javascript. There's a lot of different ways to handle it, depending on how you want the random numbers to be used. Can you provide a little more detail about what you want Squiffy to do?


Well the point is to show a health and attack and defense, but to see if when you attack, that it will either hit or miss the enemy.


IFforClassroom
29 Jan 2021, 01:07
    var coin = Math.ceil(Math.random() *2 );
        squiffy.set("coin", coin);
    var dice = Math.ceil(Math.random() *6 );
        squiffy.set("dice", dice);

{if coin=1:{@coinside=heads}}{if coin=2:{@coinside=tails}}

Your coinflip resulted in {coinside}, and your die rolled a {dice}.

IFforClassroom
05 Feb 2021, 04:26

Of course, what Squiffy SHOULD be able to do is

{random coin:heads:tails}

Maybe some day.


    var coin = Math.ceil(Math.random() *2 );

Better to put:

     squiffy.set("coin", (Math.random() > 0.5) ? 'heads' : 'tails');

then you don't need all the if line.


Of course, what Squiffy SHOULD be able to do is

{random coin:heads:tails}

That doesn't seem intuitive. Maybe something like…

@set coin = {{random:heads:tails:}}

it would be easy enough to make that work… like 4 lines added to the Squiffy source.
Unfortunately, not so easy to implement within a game.


IFforClassroom
08 Feb 2021, 12:59

Squiffy already uses the format I presented with rotate.

For example,

{rotate meats:beef:pork:chicken:mutton: venison}

So it would be consistent.


Good point.

I think I can see how to do it now, but doing it without modifying the Squiffy source will be awkward because of some functions that user code can't access directly.