Trouble with Variables

H-Segher-G
Greetings! I'm afraid my first post on the forums is to be a request for help, as I have what is likely a very small problem. In the opening scene of my text adventure, the player character encounters a sleeping character. Given that this particular encounter only happens once, at the start of the game, I have tried to include a variable which allows me to use an 'if' command to result in different dialogue next time the player engages in conversation with said character. As there are numerous ways of waking said character and engaging in the initial dialogue which is partly unique and not to be repeated, once the character has been woken using one method, a variable which marks the character as being awake is changed to true, and an 'if' command then checks this variable should the player attempt to initiate dialogue again, and decides on the appropriate course of action. So, in the 'firstenter' script which is also responsible for displaying a unique one-time message, I've added this variable 'triedtowakeleslie = false'. When dialogue is initiated, an 'if' command checks this variable to determine whether the character is awake or not, and then present the appropriate dialogue. The problem is, when I try speaking to the character, Leslie... I get this error;

Error running script: Error compiling expression 'triedtowakeleslie = false': Unknown object or variable 'triedtowakeleslie'

What's happening here?

I have attached the relevant section of code, I do apologise, it's quite a wall of the stuff. To avoid spoilers, I've replaced descriptions and messages with placeholders, please excuse them. Any help would be appreciated, I have no programming experience so I'm probably making a silly mistake.

    
<firstenter type="script">
triedtowakeleslie = false
msg ("She's still asleep!")
</firstenter>
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
<object name="The Manager">
<inherit name="editor_object" />
<inherit name="namedfemale" />
<gender>She</gender>
<takemsg>Eh? What do you plan on doing? proposing to her?</takemsg>
<take />
<drop type="boolean">false</drop>
<article>Female</article>
<alias>Leslie</alias>
<look><![CDATA[This is where the look message was.]]></look>
<alt type="stringlist">
<value>leslie</value>
<value>Leslie</value>
</alt>
<listalias>Manager Leslie</listalias>
<displayverbs type="stringlist">
<value>Look at</value>
<value>Speak to</value>
</displayverbs>
<speak type="script">
if (triedtowakeleslie = false) {
topics = Split ("Approach Leslie", ";")
ShowMenu ("Speaking with Leslie", topics, false) {
switch (result) {
case ("Approach Leslie") {
msg ("\"H... Hello?\" Stuff.")
triedtowakeleslie = true
if (triedtowakeleslie = true) {
leslietopics2 = Split ("Placeholder;Boo!", ";")
ShowMenu ("", leslietopics2, false) {
switch (result) {
case ("Placeholder!") {
msg ("placeholder.")
}
case ("Boo!") {
msg ("\"Ugh.\"")
}
}
}
}
else {
msg ("You shouldn't be here, and you KNOW it.")
}
}
}
}
}
else {
msg ("Hold it, Stranger. Placeholder for 'triedtowakeleslie = true' dialogue tree.")
}

lightwriter
Variables can only be used locally, as in the same script, where as attributes can be used globally (within any script)

XanMag
If I understand you correctly, you simply want the conversation with a sleepy Leslie different than a conversation with a fully alert Leslie.

Here is my fix. Instead of messing with variables and attributes and booleans/flags (which can lead to problems like the one you are seeing), just create a second Leslie and store that Leslie in a "dead room" or make them invisible. When whatever happens to fully wake Leslie up just move Leslie1 to "dead room" (or make invisible) and move Leslie2 to current room (or make visible).

Let me know if that solves your problem.

Good luck!

HegemonKhan
there's also the 'firsttime~otherwise' Function~Script: http://docs.textadventures.co.uk/quest/ ... ttime.html ~~~ but for additional conditional branchings, you're going to need 'if' Scripts and Attributes.

------

as to keeping your design, to further what Lightwriter mentioned:

in quest, there's this terminology:

VARIABLES
-> Variables
-> Attributes
-> Parameters

Variables are local, which means that they're limited to the script block that you've created (and are using them) within. This means that once the script block is done, those Variables that you created are destroyed, which is why you're getting the error that your 'triedtowakeleslie' Variable doesn't exist.

examples of commonly used Variable names~labels (notice that there's no: Object_name.Variable_name syntax):

// triedtowakeleslie: this is an exception, yours is not common, lol.
handled
result (built-in Variable used automatically by quest for the 'get input' and 'show menu' Scripts~Functions)
value
etc etc etc

WHEREAS, Attributes are global, you can use them anywhere, so long as the Object that they're contained within (or attached to) exists, of course (notice that Attributes are 'attached', the use of the dot~period, to the Object that contains them, in code, that is):

game.triedtowakeleslie = true
game.triedtowakeleslie = false
player.strength = 50
monster.strength = 25
HK.strength = 100
HK.favorite_color = "black"
orc.dead = false
orc.dead = true
player.right_hand = sword
HK.favorite_colors = split ("black;red", ";")
etc etc etc

if (student.score > 90) {
student.grade = "A"
} else if (student.score > 80) {
student.grade = "B"
} else if (student.score > 70) {
student.grade = "C"
} else if (student.score > 60) {
student.grade = "D"
} else {
student.grade = "F"
}

if (game.state = 0) {
orc.life = 10
} else if (game.state = 100) {
orc.life = 10000000000000000
}

etc etc etc etc


------

Parameters deal with Commands and Functions, if you know coding basics, these shouldn't need explanation. If you don't know coding basics, Parameters, Functions, and Commands, take some time to understand, not easy stuff to get when this stuff is new to someone.

--------------

in general, using Attributes makes everything easy, but if you know some coding~programming, then you can save resources by using Variables, as you already understand how they work, and can build Functions~Commands that use local variables when they can, as opposed to creating new Attributes.

jaynabonne
Short answer: if you change your usages of "triedtowakeleslie" to "player.triedtowakeleslie", then it should work, provided you either create the attribute on the player to begin with or initialize it in some startup script (so that "if (player.triedtowakeleslie = false)" will have an attribute to check).

If you don't want to actually initialize the attribute, then just change your "if" to be "if (GetBoolean(player, "triedtowakeleslie") = false). GetBoolean will return false in the case the attribute has not yet been defined.

And you can replace "if (player.triedtowakeleslie = false)" with "if (not player.triedtowakeleslie)", if you prefer the way that reads. :)

H-Segher-G
Ok! Thank you all very much for the help, I wasn't aware that a variable was local... So, I'm now using the attribute 'TheManager.triedtowake' and it's working perfectly. Thank you.

Although I haven't used it here, XanMag, thanks for the alternate method, I'll bear it in mind as it might come in handy later on.

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums