A Careless Mistake
Richard Headkid
14 Dec 2020, 23:54EDIT
I made a careless mistake in my code. There is nothing wrong with this
in either of these examples.
I forgot to make the item in example 1 a container. Then, I made a mountain (or at least a small hill) out of a mole hill.
Pixie explains here
If you'd like to possibly learn from my mistakes, read on. Just ignore what I'm saying about this
in this post!
(OLD POST TITLE: Quest 6: Using the JS this Keyword to Target the Item in Function Attributes of Clone Prototype Items)
Bad Example
this
being used in this code will cause an error when examining a clone:
createItem("grocery_sack", {
loc:"cloneRoom",
examine: function(){
msg("A paper grocery sack.")
let stuff = this.listContents()
msg("The sack currently contains: ")
msg(stuff)
}
})
Gameplay
When I examine a clone of this object, this happens:
Good Example
this
will work fine when examining a clone if we code it like this:
createItem("grocery_sack", CONTAINER(false), {
loc:"cloneRoom",
})
w.grocery_sack.examine = function() {
msg("A paper grocery sack. ")
let stuff = this.listContents()
msg("The sack currently contains: ")
msg(stuff)
}
Gameplay
I thought I knew the reason it throws an error when attempting to examine a clone of the object in the first example, but I quickly realized that I have not a clue whilst attempting to type up an explanation.
Note that I'm not saying this is a bug (or even an issue). I'm just sharing the method I found that works.
Also note that you shouldn't call cloneObject
until the game has loaded. (Calling it from settings.setup
works perfectly for me.)
Summary
So:
- Watch out when using
this
! - Calling
createItem
after the game has loaded is bad. - Calling
cloneObject
before the game has loaded is bad.
REFERENCE
https://github.com/ThePix/QuestJS/wiki/Creating-objects-on-the-fly
The Pixie
15 Dec 2020, 07:48This will be a kicking yourself one, I am afraid. The problem with the first one is it is not a container. this
exists, but has no listContents
function. This works fine.
createItem("grocery_sack1", CONTAINER(false), {
loc:"lounge",
examine: function(){
msg("A paper grocery sack.")
let stuff = this.listContents()
msg("The sack currently contains: ")
msg(stuff)
}
})
The Pixie
15 Dec 2020, 07:50A good way to diagnose issues like this is to add a console.log
that tells you what this
actually is.
createItem("grocery_sack", {
loc:"cloneRoom",
examine: function(){
msg("A paper grocery sack.")
console.log(this)
let stuff = this.listContents()
msg("The sack currently contains: ")
msg(stuff)
}
})
You can then see if it is undefined
, and if not, check its attributes. What is its name? What is listContents?
Richard Headkid
15 Dec 2020, 13:20Ha ha!
I should have realized it was probably operator error when I couldn't explain it. I woke up this morning wondering what the difference really was between the two examples, and it was a careless mistake. (I make those frequently.)
Huh. This is the second time this week I've posted something here when I should have done more research first. Life lesson learned: slow my happy ass down!