Game cannot process expression "this"
jmnevil54
06 May 2019, 01:17I am trying to get monsters, Pokemon, to spawn in my game. I keep getting errors.
Error running script: Error compiling expression 'this': Unknown object or variable 'this'
This is my code.
https://i.imgur.com/hUSbgRP.png
https://i.imgur.com/GWOSWcH.png
Game link.
http://textadventures.co.uk/games/view/5jllte-m4e2e2whw4gf5jq/pokemon-type-harley-johto-and-sinnoh
The Pixie
06 May 2019, 10:05Could you click on the code view button, and copy-and-paste that? Even if it means nothing to you, it is easier for us (well me) to see what is going on.
jmnevil54
06 May 2019, 17:06Okay.
roll = GetRandomInt(1,100)
firsttime {
if (roll > 0 and roll < 26) {
SpawnSentret (this)
}
else if (roll > 25 and roll < 51) {
SpawnHoothoot (this)
}
else if (roll > 50 and roll < 76) {
SpawnSpinarak (this)
}
}
otherwise {
if (roll > 0 and roll < 21) {
SpawnSentret (this)
}
else if (roll > 20 and roll < 41) {
SpawnHoothoot (this)
}
else if (roll > 40 and roll < 61) {
SpawnSpinarak (this)
}
}
The Pixie
06 May 2019, 18:45That looks fine. Could it be in one of the three functions SpawnSentret, etc.?
mrangel
06 May 2019, 20:08In the code shown in your first two screenshots, and in the downloaded game, you use the variable object
without defining it.
In the code in your second post, you change object
to this
, which should work fine.
Can you confirm which one you're testing on?
jmnevil54
06 May 2019, 22:28Can you confirm which one you're testing on?
Both.
jmnevil54
07 May 2019, 08:31Can I use something other than "this"?
mrangel
07 May 2019, 08:49So, if you use the variable name this
in the code, you get an error that Quest doesn't know what you mean by this
.
If you use the variable name object
in the code, you get an error that Quest doesn't know what you mean by this
.
That tells you right away that you're looking in the wrong place for the error. If it says it doesn't know what this
is, then the error must be somewhere you're using this
.
Let's look at your spawn functions.
obj.hitpoints = 20
obj.max = 20
obj.damage = 2
obj.maxdamage = 2
yep, that all looks fine. You've got a variable named obj
which contains the new Pokemon you're creating, and you're setting its attributes.
And then after a few lines more of that:
this.firearmdamage = 4
this.firearmattack = 2
There is no variable this
inside that function. That's your error; it should be obj
like all the lines above it.
jmnevil54
10 May 2019, 03:38"this" doesn't work in my other functions either.
I have a function called "instantkill." It looks like this:
this.dead = true
msg ("The Pokemon whited out!")
AddToInventory (this)
mrangel's code doesn't work either. I don't know what's wrong.
I think my game is just broken somehow.
hegemonkhan
10 May 2019, 05:20'this' is a built-in Object Reference/Pointer Variable VARIABLE to the parent Object of the scripting that you use 'this' within
<object name="orc">
<attr name="attack" type="script">
// this = orc
this.life = this.life - player.damage
</attr>
</object>
<object name="ogre">
<attr name="attack" type="script">
// this = ogre
this.life = this.life - player.damage
</attr>
</object>
you can also set 'this' to whatever you want manually as well:
<object name="orc">
<attr name="attack" type="script">
this = ogre
this.life = this.life - player.damage
</attr>
</object>
<object name="ogre">
<attr name="attack" type="script">
this = orc
this.life = this.life - player.damage
</attr>
</object>
'this' is a Variable VARIABLE, meaning it's a local Variable, so it has a limited and confusing scope
http://docs.textadventures.co.uk/quest/blocks_and_scripts.html (this might have some relevance on helping understanding its scope)
hopefully, others can explain where/when/what it works vs doesn't work, as I have trouble myself with understanding scope still
mrangel
10 May 2019, 12:07"this" doesn't work in my other functions either.
In a verb this
means the object the verb is being used on.
In a command this
means the command
In an room's enter/exit script this
means the room
In a function this
is meaningless. You need to specify which object you want to use.
jmnevil54
10 May 2019, 17:48I'm just going to try the room and see what happens.

Richard Headkid
10 May 2019, 22:05mrangel's code doesn't work either. I don't know what's wrong.
It looks like you're calling SpawnSentret
with an undefined (or non-existent) variable/parameter (object
).
Like mrangel says: In [a] room's enter/exit script this
means the room.
There is no object
variable in that script, so the function you call throws an error due to a lack of parameter.
So, first off, change every instance of object
back to this
in your "After entering" script.
Then, well, I don't know how the function you are calling (SpawnSentret
) works, but I assume its parameter needs to be a room.
So, like . . .
Time out.
1 minute later:
I just downloaded your game, unzipped it, and opened the aslx file in a text editor.
Here is the SpawnSentret
function:
<function name="SpawnSentret" parameters="room"><![CDATA[
if (HasInt(game, "crittercount")) {
game.crittercount = game.crittercount + 1
}
else {
game.crittercount = 1
}
create ("crittercount" + game.crittercount)
obj = GetObject("crittercount" + game.crittercount)
obj.parent = room
obj.displayverbs = Split("Look at;Attack;Direct Hit;Stat;Status", ";")
obj.dead = false
obj.hero = false
obj.changedhitpoints => {
if (this.hitpoints < 1) {
this.dead = true
msg ("It whited out!")
player.exp = player.exp + 20
player.pokedollar = player.pokedollar + 5
roll2 = GetRandomInt(1,20)
pokeroll = player.catchrate + roll2
if (pokeroll > 16) {
if (player.team > 7) {
MoveObject (this, PC)
this.displayverbs = Split("Look at;Equip;Reload", ";")
msg ("object.alias was moved to the PC.")
}
else {
this.displayverbs = Split("Look at;Equip;Reload", ";")
AddToInventory (this)
}
}
else {
RemoveObject (this)
}
}
}
obj.element = "Normal"
obj.alias = "Sentret"
obj.listalias = CapFirst(obj.alias)
obj.look = ProcessText("A " + obj.alias + ". A normal type. A very cautious Pokémon, it raises itself up using its tail to get a better view of its surroundings.")
obj.critdesc = "A well-placed blow by #attacker# sends you reeling (#hits# hits)."
obj.attackdesc = "#Attacker# hits you (#hits# hits)."
obj.missdesc = "#Attacker# misses you."
obj.hitpoints = 20
obj.max = 20
obj.damage = 3
obj.maxdamage = 3
obj.attack = 1
obj.maxattack = 1
obj.defence = 0
obj.maxdefence = 0
obj.armour = 0
obj.maxarmour = 0
obj.speed = 10
obj.maxspeed = 10
obj.contestpoints = 100
obj.maxcontestpoints = 100
obj.tough = 5
obj.maxtough = 5
obj.beauty = 6
obj.maxbeauty = 6
obj.cool = 4
obj.cool = 4
obj.smart = 2
obj.smart = 2
obj.cute = 4
obj.cute = 4
obj.trainerpoke = false
obj.faint = false
obj.element1 = "normal"
obj.selectweapon => {
this.weapon = GetObject(PickOneString("Struggle;Quick Attack;Scratch"))
}
obj.selectattack => {
this.weapon = GetObject(PickOneString("Struggle;Quick Attack;Scratch"))
}
this.firearmdamage = 4
this.firearmattack = 2
this.ammo = 20
this.ammomax = 20
// ProcessText("A " + obj.alias + ",t}.")
]]></function>
I see the same thing mrangel and HK and Pixie see. These are your last 12 lines of code in that function:
obj.element1 = "normal"
obj.selectweapon => {
this.weapon = GetObject(PickOneString("Struggle;Quick Attack;Scratch"))
}
obj.selectattack => {
this.weapon = GetObject(PickOneString("Struggle;Quick Attack;Scratch"))
}
this.firearmdamage = 4
this.firearmattack = 2
this.ammo = 20
this.ammomax = 20
// ProcessText("A " + obj.alias + ",t}.")
As others have pointed out, you define the variable obj
early in the code, but the variable this
does not exist in this script.
I see how someone could easily get confused, though (because it seems like this
exists in the "After entering" script):
obj.selectattack => {
this.weapon = GetObject(PickOneString("Struggle;Quick Attack;Scratch"))
}
That bit of code is actually creating a selectattack
script for whatever obj
points to. In that selectattack
script, this
will work because that script is an attribute on an object. As others have said, when a script is an attribute on an object (like a verb script), this
will point to the object to which the script attribute belongs.
But this
is not defined in your "After entering room" script. So, like others said, those last 4 lines of code (not counting the last line -- which is commented out) should use the obj
variable (which you definitely do define in this script), like this:
obj.firearmdamage = 4
obj.firearmattack = 2
obj.ammo = 20
obj.ammomax = 20
// ProcessText("A " + obj.alias + ",t}.")
I think my game is just broken somehow.
Well...
If you weren't playing Solitaire and watching two YouTube videos while you were editing the game, it would probably work better! (Just kidding!!!)
jmnevil54
12 May 2019, 07:52I just set it to "Route 24", the room name, and it worked.
Thank you all.