Pick random object to keep
SmasherSir
29 Jul 2023, 18:57I'm having the PC rumage through a gift bag and pick something they can't see. But I can't figure out how to move or use the item they pick.
Here's what I've got:
list = GetDirectChildren(this)
s = PickOneString (list)
MoveObject (s, player)
msg ("You pulled out "s", how nice.")
When I try it I get this error
Error running script: Error compiling expression 'object': RootExpressionElement: Cannot convert type 'Object' to expression result of 'Element'
Error running script: Error compiling expression '"You pulled out "s", how nice."': SyntaxError: Unexpected token "s" <IDENTIFIER>; expected one of <EOF>Line: 1, Column: 18
By putting 's' into an attribute and using the debugger I found that the value is including "Object:" in front of it. So the value is something like Object: funny teeth
I think MoveObject doesn't like to have "Object:" in front of funny teeth, so I think that's what causes the first error.
I think the 'Object:' in front of it is making it not look like a string, and that gives the second error.
I can't find what I need to do to remove 'Object:' from the front of the value, or what I should be doing instead. Can anyone help me out?
mrangel
30 Jul 2023, 10:57
list = GetDirectChildren(this)
That looks right. The variable list
is now an objectlist containing all the objects inside this
(which I assume is the bag here)
s = PickOneString (list)
The function PickOneString
takes a single argument, assumes it is a list, and returns one element from it. However, this function is of type string. Your list here is a list of objects, so you should be using PickOneObject
.
MoveObject (s, player)
This should work fine, so long as s
is actually an object.
msg ("You pulled out "s", how nice.")
You can't just put a string and a variable next to each other. You need to tell Quest how to combine them.
If s
was a string, you could do:
msg ("You pulled out " + s + ", how nice.")
But as s
is an object, you need to convert it to a string first. The usual way of doing this is one of:
msg ("You pulled out " + GetDisplayAlias(s) + ", how nice.")
(shows the object's name or alias. For example "You pulled out ball")msg ("You pulled out " + GetDisplayName(s) + ", how nice.")
(this version includes the prefix. For example "You pulled out a ball")msg ("You pulled out " + GetDisplayNameLink(s, "object") + ", how nice.")
(this version makes the name a clickable link, if your game has links enabled)msg ("You pulled out {object:" + s.name + "}, how nice.")
(actually the same as the previous one)
SmasherSir
02 Aug 2023, 05:32That is so helpful, thank you!!
I am still pretty new to this, and I was scouring the guide but couldn't find it.