Noob looking for help with functions

Hello,
I'm attempting to learn how to use functions to save time. I want to create a function that activates any time you use my "attack" verb on a mob. This way I can just add the verb "attack" to a mob instead of re-scripting the attack script every time I add a new one.

I would like to avoid using a command so that players can simply tap the "attack" verb connected to the mob in the object list.

I'm also hoping there's a way to have the function take the #object# you are using the verb on so I can adjust attributes. I.E. instead of using "squirel.hp = squirel.hp - player.dmg" I would like to use "object.hp = object.hp - player.dmg"

Any help would be greatly appreciated!


The attack verb script should call the function as so:

MobFunction (whatever the name of the object) <---What object you want to be sent to the function.

Then add a parameter to the function script called object (Are you using the desktop version of Quest)? Then you will see the box asking for the parameter... And whatever you send in the verb function call will be called object in the function script
The function will then expect the parameter to be sent to it, which is (whatever the name of the object) and you can then refer to it as you did in the script using object.
Use only the name of the object in the calling of the function in the verb script not it's alias.

I hope this is clear and also what you needed.


That kind of worked. Although the function did not recognize "object" in msg ("You punch the " + object + ".") as anything.

if (player.haswep = false) {
  Attacks = NewStringList ()
  list add (Attacks, "Punch")
  list add (Attacks, "Heavy Punch")
  list add (Attacks, "Recover")
  ShowMenu ("Fight Menu", Attacks, false) {
    if (result = "Punch") {
      if (player.en >= 5) {
        msg ("You punch the " + object + ".")
        if (object.hp - ((player.str + player.dmg) - object.def) > 0) {
          player.en = player.en - 5
          object.hp = object.hp - ((player.str + player.dmg) - object.def)
          msg ("You dealt " + ((player.str + player.dmg) - object.def) + " damage.")
          if (object.hp <= 0) {
            MakeObjectInvisible (object)

I added the call function with parameter squirel <--(the object I'm testing with) to the verb.
I added the parameter object to the function parameters.


Check that the spelling is the same for squirrel (matches object name) it has to be exact and it is spelled wrong in your example.
Also you lose the effect in a show menu. do this

object = game.temp before the show menu line
and
game.temp = object  after the show menu line
it carries it through for you.

I was misspelling it in both places, just to be sure though I renamed the object and and verb parameter to squirrel. I added the temp lines but now it cannot find object in the object = game.temp expression. I added the full code for the function this time in case there's something else I don't know about.

I will mention that if i create an object called object and give it the appropriate attributes, then the function does work, just not for the squirrel.

if (player.haswep = false) {
  Attacks = NewStringList ()
  list add (Attacks, "Punch")
  list add (Attacks, "Heavy Punch")
  list add (Attacks, "Recover")
  object = game.temp
  ShowMenu ("Fight Menu", Attacks, false) {
    game.temp = object
    if (result = "Punch") {
      if (player.en >= 5) {
        msg ("You punch the " + object + ".")
        if (object.hp - ((player.str + player.dmg) - object.def) > 0) {
          player.en = player.en - 5
          object.hp = object.hp - ((player.str + player.dmg) - object.def)
          msg ("You dealt " + ((player.str + player.dmg) - object.def) + " damage.")
          if (object.hp <= 0) {
            MakeObjectInvisible (object)
          }
        }
        else {
          player.en = player.en - 5
          msg ("Your punch dealt no damage to the " + object + ".")
        }
      }
      else {
        msg ("Your too tired to punch the " + object + ".")
      }
    }
    else if (result = "Heavy Punch") {
      if (player.en >= 15) {
        msg ("You punch the " + object + " hard.")
        if (object.hp - (((player.str + player.dmg) * 2) - object.def) > 0) {
          player.en = player.en - 15
          object.hp = object.hp - (((player.str + player.dmg) * 2) - object.def)
          msg ("You dealt " + (((player.str + player.dmg) * 2) - object.def) + " damage.")
          if (object.hp <= 0) {
            MakeObjectInvisible (Object)
          }
        }
        else {
          player.en = player.en - 15
          msg ("Your punch dealt no damage to the " + object + ".")
        }
      }
      else {
        msg ("Your too tired to punch the " + object + " this hard.")
      }
    }
    else if (result = "Recover") {
      player.en = player.en + 25
      if (object.en >= 5) {
        if (object.dmg > object.mag) {
          if (object.dmg > player.def) {
            object.en = object.en - 5
            player.hp = player.hp - (object.dmg - player.def)
            msg ("The " + object + " got an extra attack in while you were resting and dealt " + ((object.dmg + object.mag) - player.def) + " damage.")
          }
          else {
            msg ("The " + object + " tried to get in an extra attack, but it dealt no damage.")
          }
        }
        else {
          if (object.mag > player.res) {
            object.en = object.en - 5
            player.hp = player.hp - (object.mag - player.res)
            msg ("The " + object + " got an extra attack in while you were resting and dealt " + (object.mag - player.res) + " magic damage.")
          }
          else {
            msg ("The " + object + " tried to get in an extra attack, but it dealt no damage.")
          }
        }
      }
      else {
        msg ("The " + object + " looked very tired and did not attack.")
        object.en = object.en + 25
      }
    }
  }
}
else {
  ShowMenu ("Fight Menu", wepAttacks, true) {
    list add (wepAttacks, "Slash")
    list add (wepAttacks, "Shoot")
    list add (wepAttacks, "Blast")
    list add (wepAttacks, "Recover")
    if (result = "Slash") {
      if (player.en >= 5) {
        msg ("You slash the " + object + ".")
        if (object.hp - ((player.str + player.dmg) - object.def) > 0) {
          player.en = player.en - 5
          object.hp = object.hp - ((player.str + player.dmg) - object.def)
          msg ("You dealt " + ((player.str + player.dmg) - object.def) + " damage.")
          if (object.hp <= 0) {
            MakeObjectInvisible (Object)
          }
        }
        else {
          player.en = player.en - 5
          msg ("Your slash dealt no damage to the " + object + ".")
        }
      }
      else {
        msg ("Your too tired to slash the " + object + ".")
      }
    }
    else if (result = "Shoot") {
      if (player.en >= 5) {
        msg ("You shoot the " + object + ".")
        if (object.hp - ((player.agi + player.dmg) - object.def) > 0) {
          player.en = player.en - 5
          object.hp = object.hp - ((player.agi + player.dmg) - object.def)
          msg ("You dealt " + ((player.agi + player.dmg) - object.def) + " damage.")
          if (object.hp <= 0) {
            MakeObjectInvisible (Object)
          }
        }
        else {
          player.en = player.en - 5
          msg ("Your shot dealt no damage to the " + object + ".")
        }
      }
      else {
        msg ("Your too tired to shoot the " + object + ".")
      }
    }
    else if (result = "Blast") {
      if (object.en >= 5) {
        msg ("You blast the " + object + ".")
        if (object.hp - ((player.int + player.mag) - object.res) > 0) {
          player.en = player.en - 5
          object.hp = object.hp - ((player.int + player.mag) - object.res)
          msg ("You dealt " + ((player.int + player.mag) - object.res) + " magic damage.")
          if (object.hp <= 0) {
            MakeObjectInvisible (Object)
          }
        }
        else {
          player.en = player.en - 5
          msg ("Your blast dealt no magic damage to the " + object + ".")
        }
      }
      else {
        msg ("Your too tired to blast the " + object + ".")
      }
    }
    else if (result = "Recover") {
      player.en = player.en + 25
      if (object.en >= 5) {
        if (object.dmg > object.mag) {
          if (object.dmg > player.def) {
            object.en = object.en - 5
            player.hp = player.hp - (object.dmg - player.def)
            msg ("The " + object + " got an extra attack in while you were resting and dealt " + ((object.dmg + object.mag) - player.def) + " damage.")
          }
          else {
            msg ("The " + object + " tried to get in an extra attack, but it dealt no damage.")
          }
        }
        else {
          if (object.mag > player.res) {
            object.en = object.en - 5
            player.hp = player.hp - (object.mag - player.res)
            msg ("The " + object + " got an extra attack in while you were resting and dealt " + (object.mag - player.res) + " magic damage.")
          }
          else {
            msg ("The " + object + " tried to get in an extra attack, but it dealt no damage.")
          }
        }
      }
      else {
        msg ("The " + object + " looked very tired and did not attack.")
        object.en = object.en + 25
      }
    }
  }
  if () {
    if (object.en >= 5) {
      if (object.dmg > object.mag) {
        if (object.dmg > player.def) {
          object.en = object.en - 5
          player.hp = player.hp - (object.dmg - player.def)
          msg ("The " + object + " attacked and dealt " + (object.dmg - player.def) + " damage.")
        }
        else {
          msg ("The " + object + " tried to attack, but it dealt no damage.")
        }
      }
      else {
        if (object.mag > player.res) {
          object.en = object.en - 5
          player.hp = player.hp - (object.mag - player.res)
          msg ("The " + object + " attacked and dealt " + (object.mag - player.res) + " magic damage.")
        }
        else {
          msg ("The " + object + " tried to attack, but it dealt no damage.")
        }
      }
    }
    else {
      msg ("The " + object + " looked very tired and did not attack.")
      object.en = object.en + 25
    }
  }
}

I'm attempting to learn how to use functions to save time. I want to create a function that activates any time you use my "attack" verb on a mob. This way I can just add the verb "attack" to a mob instead of re-scripting the attack script every time I add a new one.

Verbs are for commands that do different things for each object you can use them on.
For something like this, you really want a command.

I would like to avoid using a command so that players can simply tap the "attack" verb connected to the mob in the object list.

On the 'Object' tab, there is a list called "displayverbs" which controls the options in the popup menu and verb buttons. You can add a command to it, and that command appears in the menu. (It's confusing that they're called "displayverbs" when they're not verbs; but important to realise that you can use them)


With regard to Forgewright's suggestion:

object = game.temp before the show menu line
and
game.temp = object  after the show menu line
it carries it through for you.

That's the wrong way around.
You want

  game.temp = object
  ShowMenu ("Fight Menu", Attacks, false) {
    object = game.temp

(I also suggest giving the attribute a sensible name rather than temp)


Awesome! I was tearing my hair out over that function for a bit, tried 99 fixes but swapping game.temp/object wasn't one. xD

Thank you both for the help it has been extremely educating!

On the 'Object' tab, there is a list called "displayverbs" which controls the options in the popup menu and verb buttons. You can add a command to it, and that command appears in the menu. (It's confusing that they're called "displayverbs" when they're not verbs; but important to realise that you can use them)    

So in regards to this because I feel like this will be vitally important to what I'm working on. This means that if I have a command like add str and link this to an object like a stats book "displayverbs" then the add str command would become a button that displays like a verb on the book in game?!? I will check it out for sure.

Last thing for this thread, can I edit the default sleep command so it displays a custom message?


Last thing for this thread, can I edit the default sleep command so it displays a custom message?

If you're in the desktop editor, you can modify any of the core commands. They're included in your game, just hidden in the editor, so it's possible to show the hidden commands and then edit them.

If you're using the web editor, it's not quite so easy. The web editor doesn't allow you to modify core commands, or to create a new one with the same name.
But, a command's name is only used internally. It doesn't have to be the same as the command's pattern (what the player has to type to activate it). So you could create your own command with the pattern sleep;rest, and then in the game's start script do something like destroy ("sleep") to remove the built-in command.

Or the way I do it if I want to change a core command in the web editor: in the start script you can put something like:

sleep.script => {
  msg ("It's too loud to sleep around here.")
}

This just replaces the command's script with something else.


Thank you! Now I can properly inform the player they did not give an amount of time for how long to sleep!

Consider this thread resolved :)


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

Support

Forums