Setting function parameters from a variable

I can call the function below to revive Bob. What if I want to revive Cindy. Although she can be a total "B" at times, I don't want her to die...yet. I don't want to create a revive function for every NPC in a game, so how does one create this function to accept variables? Also, can I set variables from the parameters in a function? ;)

<function name="revive Bob">
    if (GetBoolean(Bob, "alive")) {
      msg ("Bob is already alive.")
    }
    else {
      msg ("Miraculously, the defibrillator lived up to its promise, and Bob is now alive again. He says his head feels kind of fuzzy.")
      SetObjectFlagOn (Bob, "alive")
    }
  </function>

What kind of functions, and what kind of variables are we talking about?


Using a Function:

<game name="example_game">
  <attr name="start" type="script">
    revive_function (john)
    revive_function (jane)
    revive_function (robot)
    revive_function (jeff)
    revive_function (jill)
  </attr>
</game>

<object name="john">
  <inherit name="character_type" />
  <inherit name="male_type" />
  <attr name="alive" type="boolean">false</attr> 
</object>

<object name="jane">
  <inherit name="character_type" />
  <inherit name="female_type" />
  <attr name="alive" type="boolean">false</attr> 
</object>

<object name="jeff">
  <inherit name="character_type" />
  <inherit name="male_type" />
</object>

<object name="jill">
  <inherit name="character_type" />
  <inherit name="female_type" />
</object>

<object name="robot">
  <inherit name="character_type" />
  <inherit name="asexual_type" />
  <attr name="alive" type="boolean">false</attr> 
</object>

<type name="character_type">
  <attr name="alive" type="boolean">true</attr> 
</type>

<type name="male_type">
  <inherit name="sex_type" />
  <attr name="sex_string_attribute" type="string">male</attr>
  <attr name="sex_possessive_string_attribute" type="string">his</attr>
  <attr name="sex_article" type="string">him</attr>
  <attr name="sex_gender" type="string">he</attr>
</type>

<type name="female_type">
  <inherit name="sex_type" />
  <attr name="sex_string_attribute" type="string">female</attr>
  <attr name="sex_possessive_string_attribute" type="string">hers</attr>
  <attr name="sex_article" type="string">her</attr>
  <attr name="sex_gender" type="string">she</attr>
</type>

<type name="asexual_type">
  <inherit name="sex_type" />
  <attr name="sex_string_attribute" type="string">asexual</attr>
  <attr name="sex_possessive_string_attribute" type="string">its</attr>
  <attr name="sex_article" type="string">it</attr>
  <attr name="sex_gender" type="string">it</attr>
</type>

<type name="sex_type">
  <attr name="sex_string_attribute" type="string">unknown</attr>
  <attr name="sex_possessive_string_attribute" type="string">unknown</attr>
  <attr name="sex_gender_string_attribute" type="string">unknown</attr>
  <attr name="sex_article_string_attribute" type="string">unknown</attr>
  <attr name="changedsex_string_attribute" type="script">
    if (this.sex_string_attribute = "male") {
      this.sex_possessive_string_attribute = "his"
      this.sex_gender_string_attribute = "he"
      this.sex_article_string_attribute = "him"
    } else if (this.sex_string_attribute = "female") {
      this.sex_possessive_string_attribute = "hers"
      this.sex_gender_string_attribute = "she"
      this.sex_article_string_attribute = "her"
    } else if (this.sex_string_attribute = "asexual") {
      this.sex_possessive_string_attribute = "its"
      this.sex_gender_string_attribute = "it"
      this.sex_article_string_attribute = "it"
    }
  </attr>
</type>

<function name="revive_function" parameters="object_parameter">
  if (GetBoolean(object_parameter, "alive")) {
    msg (object_parameter.name + " is already alive.")
  } else {
    msg ("Miraculously, the defibrillator lived up to its promise, and " + object_parameter.name + " is now alive again. " + object_parameter.sex_gender_string_attribute + " says " + object_parameter.sex_possessive_string_attribute + " head feels kind of fuzzy.")
    SetObjectFlagOn (object_parameter, "alive")
  }
</function>

Using an Object+Script_Attribute+Delegate (alternative to using a Function):

<delegate name="revive_delegate" parameters="object_parameter" />

<game name="example_game">
  <attr name="start" type="script">
    rundelegate (revive_object, "revive_script_attribute", john)
    rundelegate (revive_object, "revive_script_attribute", jane)
    rundelegate (revive_object, "revive_script_attribute", robot)
    rundelegate (revive_object, "revive_script_attribute", jeff)
    rundelegate (revive_object, "revive_script_attribute", jill)
  </attr>
</game>

<object name="revive_object">
  <attr name="revive_script_attribute" type="revive_delegate">
    if (GetBoolean(object_parameter, "alive")) {
      msg (object_parameter.name + " is already alive.")
    } else {
      msg ("Miraculously, the defibrillator lived up to its promise, and " + object_parameter.name + " is now alive again. " + object_parameter.sex_gender_string_attribute + " says " + object_parameter.sex_possessive_string_attribute + " head feels kind of fuzzy.")
      SetObjectFlagOn (object_parameter, "alive")
    }
  </attr>
</object>

<object name="john">
  <inherit name="character_type" />
  <inherit name="male_type" />
  <attr name="alive" type="boolean">false</attr> 
</object>

<object name="jane">
  <inherit name="character_type" />
  <inherit name="female_type" />
  <attr name="alive" type="boolean">false</attr> 
</object>

<object name="jeff">
  <inherit name="character_type" />
  <inherit name="male_type" />
</object>

<object name="jill">
  <inherit name="character_type" />
  <inherit name="female_type" />
</object>

<object name="robot">
  <inherit name="character_type" />
  <inherit name="asexual_type" />
  <attr name="alive" type="boolean">false</attr> 
</object>

<type name="character_type">
  <attr name="alive" type="boolean">true</attr> 
</type>

<type name="male_type">
  <inherit name="sex_type" />
  <attr name="sex_string_attribute" type="string">male</attr>
  <attr name="sex_possessive_string_attribute" type="string">his</attr>
  <attr name="sex_article" type="string">him</attr>
  <attr name="sex_gender" type="string">he</attr>
</type>

<type name="female_type">
  <inherit name="sex_type" />
  <attr name="sex_string_attribute" type="string">female</attr>
  <attr name="sex_possessive_string_attribute" type="string">hers</attr>
  <attr name="sex_article" type="string">her</attr>
  <attr name="sex_gender" type="string">she</attr>
</type>

<type name="asexual_type">
  <inherit name="sex_type" />
  <attr name="sex_string_attribute" type="string">asexual</attr>
  <attr name="sex_possessive_string_attribute" type="string">its</attr>
  <attr name="sex_article" type="string">it</attr>
  <attr name="sex_gender" type="string">it</attr>
</type>

<type name="sex_type">
  <attr name="sex_string_attribute" type="string">unknown</attr>
  <attr name="sex_possessive_string_attribute" type="string">unknown</attr>
  <attr name="sex_gender_string_attribute" type="string">unknown</attr>
  <attr name="sex_article_string_attribute" type="string">unknown</attr>
  <attr name="changedsex_string_attribute" type="script">
    if (this.sex_string_attribute = "male") {
      this.sex_possessive_string_attribute = "his"
      this.sex_gender_string_attribute = "he"
      this.sex_article_string_attribute = "him"
    } else if (this.sex_string_attribute = "female") {
      this.sex_possessive_string_attribute = "hers"
      this.sex_gender_string_attribute = "she"
      this.sex_article_string_attribute = "her"
    } else if (this.sex_string_attribute = "asexual") {
      this.sex_possessive_string_attribute = "its"
      this.sex_gender_string_attribute = "it"
      this.sex_article_string_attribute = "it"
    }
  </attr>
</type>

You've been wanting an excuse to flaunt a delegate in the forums, haven't you, HK? this is just what I was looking for! Thank you.


I also show how to do it with a Function too (see my post above my 'delegate usage' post). I personally like Delegates (good organization, encapsulation design) better, now that I understand them, compared to Functions (global/universal, non-encapsulation design)


let me know if you need help with anything, my two posts' code above, were just examples, on using Functions and Delegates, but there's a bit more with Functions and Delegates that I didn't include (such as using/having return values)


Delegates are one way, but t is easy to do with functions too, just add a parameter:

<function name="revive" parameters="npc">
    if (GetBoolean(npc, "alive")) {
      msg (npc.name + " is already alive.")
    }
    else {
      msg ("Miraculously, the defibrillator lived up to its promise, and " + npc.name + " is now alive again. " + CapFirst(npc.gender) + " says " + npc.possessive + " head feels kind of fuzzy.")
      SetObjectFlagOn (npc, "alive")
    }
  </function>

Then you can do:

revive(Bob)
revive(Cindy)

Or do it as a script on a type if you want the encapsulation HK mentioned, and give the type to Bob and Cindy.

<type name="revivable">
  <revive type="script">
    if (GetBoolean(this, "alive")) {
      msg (this.name + " is already alive.")
    }
    else {
      msg ("Miraculously, the defibrillator lived up to its promise, and " + this.name + " is now alive again. " + CapFirst(this.gender) + " says " + this.possessive + " head feels kind of fuzzy.")
      SetObjectFlagOn (this, "alive")
    }
  </revive>
</type>

Then you can do:

do (Bob, "revive")
do (Cindy, "revive")


@ Pixie (or whoever using, doesn't realize it on their own): missed changing a variable in your code

change the 'npc' to 'this' in this line: if (GetBoolean(npc, "alive")) {

so, it should look like this:

<type name="revivable">
  <revive type="script">
    if (GetBoolean(this, "alive")) {
      msg (this.name + " is already alive.")
    }
    else {
      msg ("Miraculously, the defibrillator lived up to its promise, and " + this.name + " is now alive again. " + CapFirst(this.gender) + " says " + this.possessive + " head feels kind of fuzzy.")
      SetObjectFlagOn (this, "alive")
    }
  </revive>
</type>

Can a 'type' have/use a delegate script attribute?

<delegate name="revive_delegate" parameters="npc" />

<type name="revivable">
  <revive type="revive_delegate">
    if (GetBoolean(npc, "alive")) {
      msg (npc.name + " is already alive.")
    }
    else {
      msg ("Miraculously, the defibrillator lived up to its promise, and " + this.name + " is now alive again. " + CapFirst(this.gender) + " says " + this.possessive + " head feels kind of fuzzy.")
      SetObjectFlagOn (this, "alive")
    }
  </revive>
</type>

also, can a 'changedXXX' Script Attribute use/handle a delegate?

<delegate name="example_delegate" parameters="example_string_parameter" />

<object name="example_object">
  <attr name="example_string_attribute" type="string">example_string_value</attr>
  <attr name="changedexample_string_attribute" type="example_delegate">
    msg (example_string_parameter)
  </attr>
<object>

Thanks for the catch HK.

Yes to the first.

I would guess no to the second, as Quest will assume the script takes no parameters, so will not supply anything. What are you hoping you could pass to the changed script? Obviously a changed script has access to this and the new value. Did you know it has the old value too in a local variable called oldvalue?


ya, I was just pointing it out, so easy to miss some things/parts/variables/etc in code when you're writing code on the fly (I do it all the time... part of why I edit my posts so much, laughs), it's not a big deal for those who know coding, but for people who just copy and paste... they won't recognize that the 'npc' needs to be changed to 'this', saves us from having to address stupid simple mistakes in our code if we do it before people start seeing and trying to use the code. Helps to have as error free code as possible for all the people who don't know coding.


ah, cool, I was just wondering if delegates would work with them (too lazy to test/see myself, lol), and thanks for pointing out that the 'changed' does store the old value before updating with the new value... now if I can remember that... lol.

I had nothing in particular in mind, just wondering if the underlying code would recognize a delegate when using the 'changedXXX' script. But, just realizing, you'd still need a way of passing variables/data to it... though maybe it could work for just a delegate that returns a value, if the underlying code can use the 'type=DELEGATE' to implement the delegate with the 'changedXXX' script, just wondering how the underlying code is set up to work. Always interested in learning more about the underlying code, even if it is on/about stuff that is still beyond my ability.


Is the storage of the 'oldvalue' an older quest version feature, or is it something you've added with your quest updates? (as I'm still using an old version of quest, hadn't updated to your current version of quest yet, as I'd have to learn all the new stuff you have been implementing/adding/changing into it)


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

Support

Forums