Trying to set a random property to an object
Brian5757
01 Mar 2020, 01:28I'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?
Brian5757
01 Mar 2020, 07:40It 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?
mrangel
01 Mar 2020, 09:30ProcessText
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.
Brian5757
01 Mar 2020, 11:18Thanks mrangel. Good to know that there is an alternative.
Brian5757
02 Mar 2020, 04:15Hi 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?
mrangel
02 Mar 2020, 09:29This 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)
Brian5757
03 Mar 2020, 00:59Hi 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)
mrangel
03 Mar 2020, 08:47That'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.