Random dice roll

Just starting to try coding for the first time so a very simplistic answer would be helpful…
So I am in the middle of creating a confrontation between the playable character and his opponent. for replay value I’m trying to have half a dozen or more different ways in which your opponent may come at you. So what I’m trying to accomplish is a sort of random dice roll which would determined how it began.
How would I code this?

*create variable

*rand variable 0 7 (Will pick a random number between (and including) 0 and 7)

*if variable = 3
something happens
*elseif variable > 3
something else happens
*elseif variable < 3
something else happens

There are many things you could choose here.

Edit: Be sure to place the *create command in the startup.txt file

1 Like

Thank you for the quick response :slight_smile:

No problem, good luck with your project!

I’ve been working on a way to establish a spawn table to for battles, where preferably a number will be randomized, then will load an enemy based on which number is loaded. Any tips on getting this underway?

I’m not sure, you would probably have to do everything manually, having 1 enemy for each number, it would look like this:

*rand var 1 3

*if var = 1
(battle with enemy_1)
*elseif var = 2
(battle with enemy_2)
(battle with enemy_3)

I haven’t really tried coding anything like that, but it shouldn’t be too hard.

You could also add *label and *goto somewhere.

Edit: What Havenstone said. By the way, are you using any variables for the enemies? Or is it just text?

Or if you wanted the battle to play out similarly, but just have a different enemy,

*if var = 1
  *set enemy "orc"
  *set enemy_hit_points 10
  *set enemy_strength 3
*if var = 2
  *set enemy "firbolg"
  *set enemy_hit_points 20
  *set enemy_strength 6

and then let the battle roll:

The horrible ${enemy} charges across the glen.

it’s going to use the variable to plug in the enemies name, then from there upload the respective stats.

Would it be possible to create a file to act as a sort of table that contains the enemies and there stats, then have the data extracted and plugged into the loading battle?

Not exactly, no. What you can do however is use *gosub / *return to jump to (e.g.) the bottom of your current scene file, grab a load of data from there, then return to that exact point in your story and make use of that data. Worth looking into and thinking about the possibilities:

1 Like

Tinkering around a bit, came to the conclusion that gosub would be needed, but this article will help kick start things by skipping the part where I screw around to figure out how a certain feature works by trial and error.

*gosub_scene is also good, if the same table is going to be used in a bunch of scenes. (When I say table, I mean “list sorted by variables” for now.)

Randomizers beware:
If you go to the stat screen and back, the random variable can be reset. The easiest way to work around this is to have the random number immediately set another variable (set enemy variable 112 to ‘enemy=“goblin_knight”’ before the player has a chance to touch anything.) Even if you think that the random variable shouldn’t matter at that point, e.g. you’ve already gone to a different scene determined by the variable, if you haven’t reset it to a non-random variable, it still applies.

I actually recommend invoking the *rand command before the moment where you need it, like before a *page_break or whatever. That avoids the bug you mention as well.


@ballmot in the startup.txt file when I type “*create variable” do I put “false” next to the command or “0”?

It depends on the type of variable you’re creating, string(words)/ numeric(numbers), or boolean(true or false)

Try reading the choicescript wiki. Might help you a lot.

@epic_pants “false” and “true” are for Booleans, you have to use numbers in this case.

I usually just go with “0” (*create var 0)

It’s also worth noting that unless you need to make a check against that value right away you can also create it without a value at all as long as you still include the “” at the end with no value. Like so

*create name ""