need help with Luck system (random numbers + conditions)

I've spent half my day trying to get over this and reading for solutions on the docs and the forum, but nothing works, so I'm asking for your help.

I want to have a system in my game, where the player can try his luck. His luck is a value between 0 and 10: 0 meaning he will never have luck, 10 meaning he will always have 100% luck (the latter will be unreachable). The player can try his luck in a battle and if he has luck, he doesn't take any damage from the battle.

Logically I'm doing it with generating a random number between 1 and 10 during the battle and then checking: If Luck - Random Number <= 0: no luck. Else - congrats. Here is my code (obviously can be shortened, but I did it in many steps to trace my error - unsuccessfully :D ):

For generating the random number:

var randomnumber = function rng(min, max) {
 return Math.floor(Math.random() * (max - min) + min);

set ("thisFight", randomnumber(1,10));

Now "thisFight" becomes the random number to compare with the luck value. It works so far, but then I have a problem:

set("winLose", get("thisFight") - get("luck"));
@set winLose = @thisFight - @luck

This line is supposed to set the number, determining the outcome. Something here doesn't work, because it gives out "null" and nothing else. I've tried those two and everything inbetween, unsuccessfully.

I also have a problem with presenting the outcome:

{if winLose<0:{You fail!, health-=10}}
{else:{Hooray! You win without a scratch!}}

These lines also don't work for some reason, even when I try them with a preset value!

Please help :D

I didn't really debug it, but you might try this (which I didn't really test, so I hope it works). Note that I'm using winLose as a Boolean, rather than an integer.

...okay, thisFight = 1-10 already...

@set not winLose
{if luck<[email protected]:{@winLose}}

{if winLose: You win without a scratch}
{else:{You fail! {@health-=10}}

I think this will work. Someone can correct me - I don't have a squiffy editor where I'm at.

Ok, the winLose problem seems to be solved (Thank you!), but the if-else thing continues to not work. Add one } to the end of the last line and the win outcome works just fine (if you don't it just shows you the code as text in both outcomes). However I can't make the fail outcome to work, it gives null and doesn't change the health. I tried the code as it shoult be according to the documentation - {else:{You fail!, @health-=10}} but it also gives null. I can't find the error in the synthax!

Shame on me for the buggy code. Okay, now that I'm home, I reworked this. Now you can set the game variables and test it too.

@set luck=6
@set thisFight=5
@set health=15

@set not winLose
{if luck<[email protected]:{@winLose}}

{if winLose: You win without a scratch}
{else:You fail! {@health-=10}}


That should work for you now. At least it worked here. Let me know. And good luck writing games!

Thank you so much! It's all fine now! You're the best :)

Also, I liked your game in the London underground :P I hope to reach your skill some day.

Many thanks. I need to chase the moderators to review and approve it.

This topic is now closed. Topics are closed after 60 days of inactivity.