More problems with creating object
jmnevil54
17 Oct 2018, 02:16Hey, I'm having problems creating pokeballs again.
- Should I use a name or expression for the "destory" function? Do I use obj, this, or object?
- I am unable to take the object, but I can drop it.
My code.
player.pokedollar = player.pokedollar - 5000
if (HasInt(game, "pokeballcount")) {
game.pokeballcount = game.pokeballcount + 1
}
else {
game.pokeballcount = 1
}
create ("pokeballcount" + game.pokeballcount)
obj = GetObject("pokeballcount" + game.pokeballcount)
obj.parent = player
obj.displayverbs = Split("Look at;Use;Take", ";")
obj.inventoryverbs = Split("Look at;Drop;Use", ";")
obj.alias = "Pokeball"
obj.look = "A " + obj.alias + ", a device that aids in catching Pokemon. Use it."
obj.use => {
player.catchrate = player.catchrate + 1
destroy ("this")
}
hegemonkhan
17 Oct 2018, 02:56the 'destroy (XXX)' Function takes/uses the Object's string name:
http://docs.textadventures.co.uk/quest/scripts/destroy.html
// create ("NAME_OF_OBJECT")
destroy ("NAME_OF_OBJECT")
// or
destroy (NAME_OF_OBJECT.name)
// or
object_variable = NAME_OF_OBJECT
destroy (object_variable.name)
// or
string_variable = NAME_OF_OBJECT.name
destroy (string_variable)
player.pokedollar = player.pokedollar - 5000
if (HasInt(game, "pokeballcount")) {
game.pokeballcount = game.pokeballcount + 1
}
else {
game.pokeballcount = 1
}
// --------------------------------------------
// just to be safe that it works:
string_variable_1 = ToString (game.pokeballcount)
string_variable_2 = "pokeballcount" + string_variable_1
object_variable = create (string_variable_2)
// but this (what you had --- with slight edit: in storing it into a Variable) probably works fine:
object_variable = create ("pokeballcount" + game.pokeballcount)
// if not, then try this:
object_variable = create ("pokeballcount" + ToString (game.pokeballcount))
// if still not, then use the 'just to be safe that it works' code above
// -------------------------------------------
object_variable.parent = player
object_variable.take = true
object_variable.displayverbs = Split("Look at;Use;Take", ";")
object_variable.inventoryverbs = Split("Look at;Drop;Use", ";")
object_variable.alias = "Pokeball"
object_variable.look = "A " + object_variable.alias + ", a device that aids in catching Pokemon. Use it."
object_variable.use => {
player.catchrate = player.catchrate + 1
destroy (object_variable.name)
}
if you want your code to stay (mostly) the same as you had it, then you can use this code:
player.pokedollar = player.pokedollar - 5000
if (HasInt(game, "pokeballcount")) {
game.pokeballcount = game.pokeballcount + 1
}
else {
game.pokeballcount = 1
}
obj = create ("pokeballcount" + game.pokeballcount) // or: see above on alternative scripting
obj.parent = player
obj.take = true
obj.displayverbs = Split("Look at;Use;Take", ";")
obj.inventoryverbs = Split("Look at;Drop;Use", ";")
obj.alias = "Pokeball"
obj.look = "A " + obj.alias + ", a device that aids in catching Pokemon. Use it."
obj.use => {
player.catchrate = player.catchrate + 1
destroy (obj.name)
}
jmnevil54
17 Oct 2018, 04:12I tried the second thing that you said, and I got:
Error running script: Element not found: 'object.name'
I still need to figure out what to do for "take."
mrangel
17 Oct 2018, 09:04-
You need the name of the object. You can't use
obj.name
like HK suggested, because the variableobj
is only in scope as long as the creation function is running.What you want is:
destroy (this.name)
-
To let the player take an object, you need to set
obj.take = true
. I think I pointed this out on the last version of this code you shared.
jmnevil54
17 Oct 2018, 10:50To let the player take an object, you need to set obj.take = true. I think I pointed this out on the last version of this code you shared.
Sorry, I missed it. Anyways, thank you!
hegemonkhan
17 Oct 2018, 22:36(filler for getting my edited post, updated/posted)
oops my bad, sorry jmnevil! (thank you for correcting and explaining my mistake, mrangel!)
I wasn't paying attention to this part of code of mine:
obj.use => {
player.catchrate = player.catchrate + 1
destroy (obj.name)
}
the 'obj' isn't defined (its not storing the Object reference/pointer) for within its nested scripting... so that's why the error
outside of the 'obj.use' Script Attribute: obj = YOUR_OBJECT: no error
inside of the 'obj.use' Script Attribute: obj = ??? (null): ERROR!!!!
a quick-easy fix for it:
game.stored_object = obj
obj.use => {
player.catchrate = player.catchrate + 1
destroy (game.stored_object.name)
}
and the full fixed code:
player.pokedollar = player.pokedollar - 5000
if (HasInt(game, "pokeballcount")) {
game.pokeballcount = game.pokeballcount + 1
} else {
game.pokeballcount = 1
}
obj = create ("pokeballcount" + game.pokeballcount) // or: in my previous post, see above on alternative scripting
obj.parent = player
obj.take = true // sorry, I had this line of code between the other code lines in my previous post, so easy to miss it, but now I've separated it out in this post, so you can see it this time
obj.displayverbs = Split("Look at;Use;Take", ";")
obj.inventoryverbs = Split("Look at;Drop;Use", ";")
obj.alias = "Pokeball"
obj.look = "A " + obj.alias + ", a device that aids in catching Pokemon. Use it."
game.stored_object = obj
obj.use => {
player.catchrate = player.catchrate + 1
destroy (game.stored_object.name)
}
mrangel
17 Oct 2018, 23:45@HK:
That makes no sense. You've got a script to create consumable objects, but you're using a global attribute to store a reference to the last one created?
The only sensible way to handle that change is to use this
within the script attribute. As in:
obj.use => {
player.catchrate = player.catchrate + 1
destroy (this.name)
}
... which is the answer I already gave.
hegemonkhan
18 Oct 2018, 02:24...smacks head...
...I need to pay more attention...
(we've had too many posts needing help with the issue of using nesting 'this' scope issue, wanting/needing/using a non-parent Object)
... I need to pay more attention
it's a Script Attribute and the Object we want is its parent for this case, so indeed, we can use 'this'
I was just using an Attribute, which would be the quick-fix, if we weren't using the parent Object of a Script Attribute (weren't able to use 'this')...
as I wasn't paying attention...
... smacks head...

CheeseMyBaby
19 Oct 2018, 08:26/me puts a band-aid on HK's head.
hegemonkhan
19 Oct 2018, 12:33I got a very hard dense head (and am very hard headed), so don't worry, my head is quite fine... now the desk or hand or wall that I smacked my head against is... broken...
jmnevil54
20 Oct 2018, 02:19Now I'm getting:
Error running script: Element not found: 'this.name'
Maybe my game just doesn't like me...
mrangel
20 Oct 2018, 08:43That sounds like you've put:
destroy ("this.name")
instead of
destroy (this.name)
jmnevil54
21 Oct 2018, 06:21Strange. When I looked at it it said destroy-name-this.name, and the code was destroy ("this.name")
, then when I changed it to destroy (this.name)
it became destory-expression-this.name. I'll play it when I have time. Thank you.