Trying to set a random property to an object

I'm trying to set a random size for the paper with this command:
Paper.size = {random:small:medium:large}
msg(Paper.size)

But I get this error:
Error running script: Error compiling expression 'random:small:medium:large': SyntaxError: Unexpected character: :Line: 1, Column: 7

Is there a different format for doing this?


It looks like I need:
Paper.size = ProcessText("{random:small:medium:large}")
unless there is a better way.

I'm thinking that ProcessText must be used when the conditional text is not in the description.

Would the paper size that was created during the game be saved when the players saves the game he is playing?


ProcessText is called automatically when text is sent to the player (using msg or similar commands).

So if you do:

Paper.size = "{random:small:medium:large}"

the size attribute will be a string with the value {random:small:medium:large} - which will be replaced by an option chosen at random every time it is displayed (it will be different each time)

but if you do:

Paper.size = ProcessText ("{random:small:medium:large}")

then the ext processor will run and replace the command with a randomly chosen string, so size will be a string attribute whose value is either small, medium, or large. It will have the same value every time you refer to it, and will continue to have that value when the game is saved.


I would note, however, that the text processor is quite inefficient, and you probably shouldn't use it without a good reason. In this case, you would probably be better doing:

Paper.size = PickOneString (Split ("small;medium;large"))

(Split turns a string into a stringlist; PickOneString picks one string at random out of a stringlist)

The text processor is a useful feature if you have strings that might contain a lot of different text processor commands (or if you want an object's description to be slightly different each time it is looked at), but if you're calling ProcessText on a string with just a single text processor command in it, there might be a faster way to do it.


Thanks mrangel. Good to know that there is an alternative.


Hi mrangel.

There must be something missing in this code you suggested:
Paper.size = PickOneString (Split ("small:medium:large"))
msg ("The paper is "+Paper.size)

As the result is "The paper size is small:medium:large"
Maybe it needs the word random somewhere?


This is the hazard of typing on mobile; the keyboard is supposed to be "smart" at guessing which symbols I meant. Or I accidentally had caps-lock on.

In the argument to Split, the list elements should separated by a semicolon (;). Not a colon (:). - unless you explicitly specify the separator.

Split turns a string into a list, but in this case it didn't see the separator, so it was returning a list with the single element "small:medium:large". Then PickOneString picks a random element from that list.

You could use PickOneString (Split ("small:medium:large")), or PickOneString (Split ("small:medium:large", ":")), or PickOneString (Split ("small,medium,large", ",")) if you prefer.

(have edited the code snippet in the previous post)


Hi mrangel.

It looks like the command 'Split' is not needed as this code will still work
Paper.size = PickOneString ("small;medium;large")
msg ("The paper is "+Paper.size)


That's convenient :)

I hadn't noticed that; but it looks like PickOneString automatically splits its input if necessary. I guess that's a common enough case, so someone decided to make it easier.


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

Support

Forums