0 votes
by (250 points)

Hi everyone,

 

I'm coming back to Twine after a while, and I've tried to set a variable with the following code:

<<nobr>>You attack the $target.name<br>
<<set $hit to random(99)>>
<<if $hit > $target.evadechance>>
<<set $calcdmg to $player.attack + $playerweapon.attack - $target.level * ($target.defense / 5)>>
<<print $calcdmg>> damage!
<<else>>
You missed!
<</if>>
<</nobr>>

However, it always gives me the error "Error: <<set>>: bad evaluation: Cannot read property 'attack' of undefined"

 

I have made sure I've used the correct syntax when setting up the objects used (Or so I believe), and I've never seen this error before when setting up other variables. Can anyone see the problem?

 

Thanks!

2 Answers

+1 vote
by (2.4k points)
Hello,

Could you send the syntax used for setting up the objects ?

Thanks !
by (250 points)

Thanks for the response. A few passages earlier, I use these (in separate passages) to set up most of the relevant objects. I am attempting to reate an RPG-like combat system.

<<set $calcdmg to 0>>

<<set $enemy1 ={
name: "none",
level: 1,
maxHP: 0,
hp: $maxHP,
attack: 0,
critrate: 0,
critbonus: 0,
defense: 0,
speed: 0,
magic: 0,
evadechance: 0,
}>>

<<set $enemy2 ={
name: "none",
level: 1,
maxHP: 0,
hp: $maxHP,
attack: 0,
critrate: 0,
critbonus: 0,
defense: 0,
speed: 0,
magic: 0,
evadechance: 0,
}>>

<<set $enemy3 ={
name: "none",
level: 1,
maxHP: 0,
hp: $maxHP,
attack: 0,
critrate: 0,
critbonus: 0,
defense: 0,
speed: 0,
magic: 0,
evadechance: 0,
}>>

<<set $target ={
name: "none",
level: 1,
maxHP: 0,
hp: $maxHP,
attack: 0,
critrate: 0,
critbonus: 0,
defense: 0,
speed: 0,
magic: 0,
evadechance: 0,
}>>

<<set $bat = {
name: "bat",
maxHP: 3,
hp: $maxHP,
attack: 1,
critrate: 1,
critbonus: 1,
defense: 1,
speed: 1,
magic: 1,
evadechance: 2,
}>>

target is made to equal enemy1 in a setter link, which has previously been made to equal bat:

<<link '$enemy1.name' "Attack2.2">><<set $target = $enemy1>><</link>><br>
<<set $enemynumber = 2>>
<<set $enemy1 = $bat>>
<<set $enemy1.level = 1>>
<<set $enemy2 = $bat>>
<<set $enemy2.level = 1>>

What do you think could be wrong with it?

+1 vote
by (68.6k points)

If you're absolutely sure that the shown code is where the error is coming from, then there are only two possibilities, since you only attempt to access a property named "attack" in two places, either $player or $playerweapon are undefined at that point.

by (250 points)

Thanks for the response. If it helps, I'm setting up the variables like so:

 

<<set $player = {
level: 1,
attack: 1,
defense: 1,
speed: 1,
magic: 1,
strategy: 0,
bravery: 0,
skill: 0,
}>>

<<set $playerweapon = {
attack: 1,
critrate: 1,
critbonus: 1,
defense: 0,
magic: 0,
accuracy: 0,
}>>

I do this in one of the first passages of the story.

by (68.6k points)

That really doesn't say much about the situation.  I will note, however, that if that's supposed to be initialization code, then you're probably better off placing it within the StoryInit special passage.

Try placing something like following just before the <<set $calcdmg ...>> line:

($$player is <<= typeof $player>>, $$playerweapon is <<= typeof $playerweapon>>)

That will tell you which one is undefined.  If both come back as object, then your problem lies elsewhere and we'll probably need to see your project up close to be able to pinpoint the source of your troubles.

 

PS: Unless you're okay with your project not working in IE, and older versions of other browsers, I would recommend not use a trailing comma after the final member within both array and object literals.  That being allowed is a somewhat recent addition.

by (250 points)
Thanks a ton for the whole testing which one is undefined thing. Really helped! Turns out I made a stupid mistake where I set $playerweapon to equal $longsword before I defined longsword as an object. I'm stupid :P. Thanks again!

 

PS: Didn't know about the trailing comma thing. Thanks for that too!
...