Tying Integers to Strings?

JonouchiKotaro
21 Jan 2020, 19:41

Hey there! So I've been trying to deal with this particular problem for about 2 hours now and I was wondering if anyone here had any insight.

What I am trying to accomplish is to tie integer values to strings for use in Status Attributes.

How I've been trying to get it done is by initializing an integer attribute called "colorvalue" and a string attribute called "colorname". I have then set up a Status Attribute with the "colorname" key that reads "Color = !"

I feel like I have set this up properly, but I am not sure how to proceed from here. What I want to do is make it so "if colorvalue = 0, then colorname = "Black" ", "if colorvalue = 1, then colorname = "Red" " and so on.

I have attempted this with conditional branches and lists, but I have no idea what I'm doing and how to accomplish what I ultimately want to do. Any help would be appreciated as this is a pretty big roadblock for me right now. XD


Io
21 Jan 2020, 20:27

What you want is a changescript.

Select an object. Go to its attributes. Give it the TestVar attribute, and then select that. See that boxed-plus sign, next to the + and - that let you add and delete attributes? Click it to create a changescript for TestVar.

The new changescript will fire whenever TestVar changes. So if you have a TestVarString attribute, you can have the changescript be:

switch (this.TestVar){
case(1){
this.TestVarString="Black"
}
case(2){
this.TestVarString="Red"
}
etc
}

Hope this helps!


mrangel
21 Jan 2020, 20:53

A changescript is the best way to tie two attributes together.

Switch/case is a pretty efficient way to select a value based on a number. But it might work out easier to use a list of colour names to look up.

If you have an attribute colornameslist which is a stringlist, then your changedcolorvalue script would look like:

this.colorname = ListItem (this.colornameslist, this.colorvalue)

hegemonkhan
22 Jan 2020, 08:16

if you only got a few integer-color options, then using an 'if-else if' or 'switch-case' Scripts/Functions are fine, but if you got a lot of integer-color options, then you'll want to use a list or a dictionary

here's an example of using a list and a dictionary:

a list's 'keys' are hidden and automatic, the first item's 'key' is the index number of '0', and each following item's 'key' is contiguous (0, 1, 2, 3, etc). The list's index number 'keys' are Strings (NOT Integers), but I believe that quest is able to automatically handle/do conversions between Integers and Strings for you, hopefully

a dictionary's 'keys' are also Strings, even if you use numbers, they're Strings, NOT Integers

<game name="NAME_OF_GAME">

  <attr name="colorvalue" type="int">0</attr>

  <attr name="colorname" type="string">unknown</attr>

  <attr name="changedcolorvalue" type="script">

    // using a String List:

    this.colorname = StringListItem (this.colorlist, this.colorvalue) // if quest doesn't automatically convert the integer to string, then use this code line instead: this.colorname = StringListItem (this.colorlist, ToString (this.colorvalue))

    // or, using a String Dictionary:

    this.colorname = StringDictionaryItem (this.colordictionary, this.colorvalue) // if quest doesn't automatically convert the integer to string, then use this code line instead: this.colorname = StringDictionaryItem (this.colordictionary, ToString (this.colorvalue))

  </attr>

  <colorlist type="stringlist">

    <value>red</value>
    <value>blue</value>
    <value>yellow</value>
    <value>green</value>
    <value>purple</value>
    <value>orange</value>
    <value>black</value>
    <value>white</value>
    <value>grey</value>
    <value>brown</value>
    <value>pink</value>

  </colorlist>

<colordictionary type="stringlist">

    <item>
      <key>0</value>
      <value>red</value>
    </item>

    <item>
      <key>1</value>
      <value>blue</value>
    </item>

    <item>
      <key>2</value>
      <value>yellow</value>
    </item>

    <item>
      <key>3</value>
      <value>green</value>
    </item>

    <item>
      <key>4</value>
      <value>purple</value>
    </item>

    <item>
      <key>5</value>
      <value>orange</value>
    </item>

    <item>
      <key>6</value>
      <value>black</value>
    </item>

    <item>
      <key>7</value>
      <value>white</value>
    </item>

    <item>
      <key>8</value>
      <value>grey</value>
    </item>

    <item>
      <key>9</value>
      <value>brown</value>
    </item>

    <item>
      <key>10</value>
      <value>pink</value>
    </item>

  </colordictionary>

  <statusattributes type="stringdictionary">

    <item>
      <key>colorname</key>
      <value>Color: !</value>
    </item>

  </statusattributes>

  <attr name="start" type="script">

    // testing for using a list:

    this.colorvalue = GetRandomInt (0, ListCount (this.colorlist) - 1)

    // or, testing for using a dictionary:

    // (this example requires that your lowest 'key' be '0' and each other 'key' must be contiguous (0, 1, 2, 3, etc etc etc)

    this.colorvalue = GetRandomInt (0, DictionaryCount (this.colordictionary) - 1)

  </attr>

</game>

JonouchiKotaro
22 Jan 2020, 13:33

Super appreciate all the help, folks! I'll go ahead and give these a try when it comes time to work on my game again! Thank you so much!