"this" Not Always Working
Shadecerule
20 Oct 2021, 14:03I have some objects with randomly generated attributes that I am cloning. However, when I use "this" to refer to the object in the Description ("A {this.flavor} popsicle.}"
) and Drop (RemoveObject(this)
) sections of the parent object's interface, the client declares that "this" is an unknown variable or object.
Is there a way to work around this?
mrangel
20 Oct 2021, 16:43As far as the "Drop" script goes, that looks like it should work fine. Can you show some more code from that example (or share a link to a game that demonstrates the problem) so that we can try to work out the problem?
It's a little different for using this within text. Within the text processor ({this.flavor}
and similar), this
refers to the object in the variable game.text_processor_this
. For some reason this isn't set if you have a text description. So you would either need to modify the lookat
command from the core library:
<command name="lookat" template="lookat">
<![CDATA[
if (GetBoolean(object, "hidechildren")) {
object.hidechildren = false
}
if (TypeOf(object, "look") = "script") {
if (not HasAttribute(object,"timesexamined")){
object.timesexamined = 0
}
do (object, "look")
object.timesexamined = object.timesexamined + 1
}
else {
lookdesc = ""
if (HasString(object, "look")) {
game.text_processor_this = object
lookdesc = object.look
}
if (LengthOf(lookdesc) = 0) {
lookdesc = Template("DefaultObjectDescription")
}
if (GetBoolean(object, "switchedon")) {
if (HasString(object, "switchedondesc")) {
lookdesc = lookdesc + " " + object.switchedondesc
}
}
else {
if (HasString(object, "switchedoffdesc")) {
lookdesc = lookdesc + " " + object.switchedoffdesc
}
}
isDark = CheckDarkness()
if (isDark and not GetBoolean(object, "lightsource")) {
lookdesc = DynamicTemplate("LookAtDarkness", object)
}
else {
if (not HasAttribute(object,"timesexamined")){
object.timesexamined = 0
}
object.timesexamined = object.timesexamined + 1
}
msg (lookdesc)
}
ListObjectContents (object)
]]>
</command>
Or you could make the object's description a script:
msg ("A " + this.flavor + " popsicle.")
Or you could process those attributes when you create the clone:
newpopsicle = CloneObject(popsicle)
// whatever code you're doing to determine the random attributes
game.text_processor_this = newpopsicle
newpopsicle.look = ProcessText (newpopsicle.look)
Shadecerule
21 Oct 2021, 09:44In the Inventory tab under Drop, I have "Run script" selected and then there is the following code:
msg ("You drop the popsicle and it immediately melts.")
RemoveObject (this)
When I drop the popsicle, I get this error:
Error running script: Error compiling expression 'this': Unknown object or variable 'this'
Using your second solution for this.flavor
worked though, but only after I restarted the program. Not sure why that was but that part is fine now.
mrangel
21 Oct 2021, 11:21When I drop the popsicle, I get this error:
That's really strange; I can't work it out. Does it print the message first?
There must be something else affecting it. Can you share the game so I can try it out?
Edit: Created a new game with just a popsicle, which gets cloned a few times by the start script. Gave it the drop script exactly as you said, and it works as expected. So I have to assume there's something different elsewhere that's affecting it; but I wouldn't know where to look except for combing through the whole code.
Shadecerule
21 Oct 2021, 12:46I tried reinstalling Quest and it suddenly works. That was silly but thanks!