Creating An Attribute That's a String Based On Another Attribute

For example, say your protagonist has a "Strength" Attribute, which is an integer value that ranges from 1-10. Various actions taken in the game can cause this Attribute to increase or decrease.

But say you want to reference the protagonist's Strength in a Status Pane, or some other context, not using a number, but with a descriptive word. So, for example, if their Strength Attribute is 1, their Strengthword Attribute is "Weak", but if they raise their Strength to 7, their Strengthword becomes "Burly" or something.

I'm super new to Quest, and to coding in general. I've tried taking a few lessons on codeacademy! But it doesn't seem to be helping. Meh. I thought setting the value of a "Strengthword" attribute to, say,

if (player.strength = 1) {
return (Weak)
}

And then have a Status Attribute keyed from the Strengthword that displays as "Strength: !". But that does not seem to work. I didn't think it would. But hopefully someone here can point me in the right direction. I've definitely got a story I want to tell and I'm eager to learn how to do it!


No, because a status attribute displays the attribute; it doesn't run it if it's a script.

For something like that, you more likely want to give the player a script attribute named changedstrength - this will automatically be run every time the player's strength changes, so you can use it to set the strengthword attribute to a sensible value. For example, your changedstrength script could be:

if (this.strength = 1) {
  this.strengthword = "Weak"
}

and so on for the other options.

(within a script attribute, you can use this to save time typing the name of the object)

In fact, it might be quicker for the script to be something like:

this.strengthword = ListItem (Split ("Zero;Weak;Puny;Pathetic;Scrawny;Below average;Average;Burly;Muscular;Ripped;Titanic"), this.strength)

Then you only need one line of code :)


A few ways it can be done:

  1. A split variable:
    (I think) game.strength=split("very weak; weak; weak; kinda weak; below average; average; kinda strong; strong; stout; burly; hulk")
    That gives you words from 0 to 10...
    or:
  2. as a function, which kinda looks like you were trying with the return(Weak) bit...
    But if you make it a function, drop the player.strength bit, and it will work for any time you need a strength word...
    Make a function and add "strength" as what you pass to it, and have it return a string.
    IE: strength2word
    Then use a switch for a list of words you want returned:
    case 1: strengthword="weak"
    case 4:strengthword="below average"
    Then:
    return (strengthword)
    So, to use it:
    msg ("You see the man stand up from the table and he looks " + strength2word(npc.strength) + "to you.")

Thank you both for your replies! The ListItem and Split functions look really useful together.

I feel like they might make creating a self-description page that is generated based on the player's current attributes a whole lot easier. I thought I would have to do a huge nest of if/then statements to make their description, for example, give a different description specific to their current outfit or level of fitness.

This changes everything! =D

I will take this lesson and push onward! Thanks again!


here's some links:

http://textadventures.co.uk/forum/general/topic/ljjm32av4e2t9ot49k478g/help#710be61e-eae1-4af1-8363-520cc718ba1c

http://docs.textadventures.co.uk/quest/using_lists.html
http://docs.textadventures.co.uk/quest/using_dictionaries.html

http://docs.textadventures.co.uk/quest/text_processor.html


if you want to do ranges (STR: 0 to 33 = "weak", 34 to 66 = "average", 67 to 100 = "strong"), you can either use 'if' scripting, or you use possibly some fancy math conversion from the ranges into 1 to 5 values (I'm not good at math so can't yet figure out the formula/equation, if its possible to keep it this simplistic) for the list/dict usage, for example:

create ("test")

test.score = GetRandomInt (0,100)

// high to low checking:

if (test.score > 89) {
  test.grade = "A"
} else if (test.score > 79) {
  test.grade = "B"
} else if (test.score > 69) {
  test.grade = "C"
} else if (test.score > 59) {
  test.grade = "D"
} else {
  test.grade = "F"
}

// or, if you don't like using the '-1' values:
// (high to low checking)

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

// or, low to high checking:

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

// ---------------------------------------------

// or, if you want to use a list:

// low to high checking:

grade_list = Split ("F;D;C;B;A", ";")

// ----------
// or:
grade_list = NewStringList ()
list add (grade_list, "F")
list add (grade_list, "D")
list add (grade_list, "C")
list add (grade_list, "B")
list add (grade_list, "A")
// ------------

input = ToString (((test.score + 10) / 20) - 1) // can't figure this formula/equation out (what I got doesn't work): I want to be able to convert the ranges (A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: 0-59) to 1-5 (and then minus 1 to 0-4) or directly to 0-4, as for the list's index number needs (F:0, D:1, C:2, B:3, A:4)

test.grade = StringListItem (grade_list, input)

// ---------------------------

or, if you want to use a dictionary:

grade_dict = NewStringDictionary ()
dictionary add (grade_dict, "1", "F")
dictionary add (grade_dict, "2", "D")
dictionary add (grade_dict, "3", "C")
dictionary add (grade_dict, "4", "B")
dictionary add (grade_dict, "5", "A")

// -------
// or:
grade_dict = NewStringDictionary ()
dictionary add (grade_dict, "0", "F")
dictionary add (grade_dict, "1", "D")
dictionary add (grade_dict, "2", "C")
dictionary add (grade_dict, "3", "B")
dictionary add (grade_dict, "4", "A")
// -------

input = ToString (((test.score + 10) / 20) - 1) // can't figure this formula/equation out (what I got doesn't work): I want to be able to convert the ranges (A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: 0-59) to 1-5 or 0-4, as for the dict's key string needs (F:0, D:1, C:2, B:3, A:4 or F:1, D:2, C:3, B:4, A:5)

test.grade = StringDictionaryItem (grade_dict, input)

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

Support

Forums