Random Encounters in game?

Disthron
Hi everyone,

In the game I'm working on, I want the player to potentially have a few random encounters. In the game, the characters are getting ready to go on a trek through the woods, and on the way to there destination things could happen. Like running into wild animals, injuring themselves ect. And they ether get buy or are hurt based on how well they prepared.

I was going to do this by running a script on a rooms exits that would lead to a random encounter room but I can't seem to find a way to do this. Is there not a random number function? Also, I've had trouble figuring out how to do variables in the web version. Some people on the forum have said that simply setting a variable to a value in the code will create the variable but that doesn't seem to work all the time.

Sorry if this is a noob question. Anyway, I hope you can help.

Pykrete
I can suggest two ways of doing this.

One would be to using Dice Roll. I created an attribute called 'dice' on my player object. if you Set Variable, player.dice (in my example) = DiceRoll("3d6"), that will randomly roll 3 6 sided die and add them together. I haven't played around with it much, but i'm pretty sure you can have d-whatever, so just "1d20", "1d50", etc. From there, you can just make an if check - if player.dice = "1", thing happens. If player.dice = "2", other thing happens; personally, i'd create a function or a timer to be called upon at that point, which can into turn call other functions which contain the events you want, if only for organization's sake.

The other way is by using one of the if check's functions, Random Chance. It allows you to set a percentage based chance check; say you set it to 50%, that thing will happen 50% of the time. If you had 4 events you wanted to be possible, you'd do an if check starting with 25% random chance, then 2 'else ifs' with the same amount, then end it with an 'else if' at 100%. (last one should be 100% because the 75% chance that the other events occur have already passed, if this was 25% as well there'd only be a 1/4 chance of it actually playing; 3/4 of the time your player would end up on a white walled dead end).

Hope that helps!

Anonynn
I would do an "If Script" and set the expression tab to a "Random Chance"

if (RandomChance(50)) {
msg ("You encounter so and so monster!")
move object BlahBlahBeast to object roomname
}
else if (Random Chance(30)) {
msg ("You encounter a Blargh!")
move object Blargh to object roomname
}
else if (RandomChance(30) {
msg ("You encounter a BoogaBooga")
move object BoogaBooga to object roomname
}
else {
move object TeenyWeeny to object roomname
}

I think right?

HegemonKhan
this is not a noob question, it's a bit of advanced coding and if-code logic involved.

------------

there's 4 randomization functions:

http://docs.textadventures.co.uk/quest/ ... eroll.html

http://docs.textadventures.co.uk/quest/ ... hance.html

http://docs.textadventures.co.uk/quest/ ... omint.html
http://docs.textadventures.co.uk/quest/ ... ouble.html

--------------

as to what you want to do exactly in your game, is up to you, which determines which one (or combination of them) you want to use.

------------

as to how to actually implement them for what you want to do in your game, this is the meat (hard part) of the problem.

we need more details on exactly how~what your design idea is...

do you want random events to happen upon moving amongst the rooms? or do you just want random events to be occuring just within a single room?

----------

if you do want the random events based upon changing rooms, a way to do it is to create~add a global Turnscript for your scripting, and I would personally use a combination of two of the randomization functions:

'GetRandomInt' for selecting what event (out of a pool of events)
and then
'RandomChance' for the chance of that selected event actually triggering~occuring or not
// if you want an event to always occur, then don't use this combination, just use one of the randomization functions to select your event from a pool of events

you can be advanced-fancy in your coding by using a dictionary for your pool of events, but this is probably too advanced for you right now.

so, you'd probably just want to use an 'if' scripting structure, for example (sorry for in code, but it's faster for me):

actually... using the special 'changed' Script within the 'player' Player Object, would work better than a global turnscript, just a very lame example below:

<object name="player">
<attr name="changedparent" type="script">
choice = GetRandomInt(1,4)
if (choice = 1 and RandomChance(5)) {
msg ("muwahahaha, HK snaps his fingers and you die!")
msg ("GAME OVER")
finish
} else if (choice = 2 and RandomChance(10)) {
player.level = 99
player.cash = 999999999999999
player.current_life = 9999
player.maximum_life = 9999
player.strength = 1000
player.endurance = 1000
player.dexterity = 1000
// etc etc etc
msg ("god mode event, enjoy!")
} else if (choice = 3 and RandomChance(30)) {
player.condition = "poisoned"
msg ("You've been poisoned.")
} else if (choice = 4 and RandomChance(90)) {
player.fatigue = player.fatigue + 5
msg ("You're a little bit more exhausted from your travels.")
}
</attr>
</object>


----------

to do this in the GUI~Editor:

the 2 SUPER SCRIPTS that epsecially when used together, let's you do 90% of everything that you want to do in your game!:

1. run as script -> add new script -> variables -> 'set a variable or attribute' Script -> set variable (see my code lines above for examples) = [EXPRESSION] (see my code lines above for examples)

2. run as script -> add new script -> scripts -> 'if' Script -> if [EXPRESION] (see my code line examples above, but the syntax used for this, is a bit different from my code lines above)

how to add the special 'changed' Script to your 'player' Player Object:

'player' Player Object -> 'Attributes' Tab -> Attributes -> Add -> (see below)

(Object Name: player)
Attribute Name: changedparent
Attribute Type: script
Attribute Value: (add in the scripts, see my scripting block above)

Disthron
Thanks for the help, unfortunately I've run into another, much worse problem.

viewtopic.php?f=10&t=5610&p=38616#p38616

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

Support

Forums