0 votes
by (270 points)
Hi guys,

So I'm trying to make random forest locations. The name of the passage will be "forest"+random number ... so it can be forest2, forest 14, forest 5....  depending on the random number result using the random function. Problem is, I can't add the integer result to the string because it tells me those are two different data types. Question is:

How to convert data type?

BTW, if anyone has a better idea how to go about it, I'm all ears...

 

Regards,

-Val

2 Answers

0 votes
by (1.3k points)

Do you mean that the forest locations already exist as passages?

In that case, I'd suggest something like this.

Forest1, Forest2, and Forest3 are names of passages that already exist.

(if: (random: 1,3) is 1)[(goto: "Forest1")]
(else-if: (random: 1,3) is 2)[(goto: "Forest2")]
(else:)[(goto: "Forest3")]

I suppose that gets clunky if you have a lot of different locations.

If on the other hand you're trying to generate new passages/locations, I'm not sure how you'd do that.

by (159k points)

@hkfto

An issue with your solution is that it potentially needs to generate two random numbers is the first condition isn't true, it would be better if your solution stored the first number it generated within a temporary variable and then used that for all the relevant conditions.

(set: _random to (random: 1,3))\
(if: _random is 1)[(goto: "Forest1")]
(else-if: _random is 2)[(goto: "Forest2")]
(else:)[(goto: "Forest3")]

 

0 votes
by (320 points)
edited by

You can use the (text:) macro to convert a numeric value into a string.

(set: $forestNumber to (random: 1,15))

(link-goto: "Forest " + (text: $forestNumber))

 

 

by (159k points)

@Chillbit

Unless you are planing on using that same random number later within other Passages I suggest using a temporary variable instead, that way the random number won't be tracked/stored by the History system from that point on-wards.

(set: _forestNumber to (random: 1,15))

(link-goto: "Forest " + (text: _forestNumber))

 

by (270 points)
Excellent answer, greyelf! And Chillbit, thank you both!

@hkfto - yes that's a bit clunky, so I'll use what was suggested below your comment.

I was also thinking to use "array" and "shuffle" as a last resort, but that seems less comfortable frankly.
...