Most efficient way to create a guarded entrance?

NecroDeath
02 Feb 2017, 18:59

Not something I've done before, NPC guards exit from one room to another, if give NPC object x, then player can use exit, otherwise return printed message.

Sure this has been done umpteen times before, can someone point me to an efficient way of doing it?


The Pixie
02 Feb 2017, 19:17

Just do it as a locked exit, with a custom message.


TinFoilMkIV
03 Feb 2017, 21:13

In a more detailed explanation of what The Pixie said. The NPC itself doesn't have to actually guard the exit, with a custom message on a locked exit you can make it appear as if it is though. Have the exit itself give a message as though the NPC is either saying or physically doing something to stop you from using it, then when given the item have the exit become unlocked.

It's a common technique in games. The code does not need a system for the NPC to block the exit as long as everything given to the player says that's what's happening and it functions correctly.


hegemonkhan
03 Feb 2017, 21:29

It's all about the illusion it ;)

for example, new people to game making and coding think you have to physically transfer 'gold coin' Objects around, but what's better, is to instead use 'gold' Integer Attributes and adjust them, to give the illusion of such transactions (buying/selling/depoting/withdrawing) or whatever action (stealing/planting, etc) that results in a change in the gold coins quantity.


crystalwizard
03 Feb 2017, 23:14

About gold: You don't even need to have gold coin objects in the players inventory, for it to look like they do. you just state how much gold the player has if they check it, and if they drop their gold, you just load a gold pile object into the room (or gold coin if all they had was 1 coin), and assign it a value equal to what they had, then assign them the value of 0 for the gold in their inventory.

However - a problem just creating the illusion that an NPC is guarding an exit is this:

If you allow the NPC to be killed, and you are simply doing slight of hand to make it look like they were guarding the exit, you can have this:

Player - attacks and kills guard NPC without getting it to unlock the exit
Player - tries to go through the exit
Game - sends player the message that the now-deceased NPC steps in their way.

Bad.

It's much better to have the game check to see if the NPC is in the room or not, then if it is, lock the exit and tell them that the NPC is in the way. But if it is NOT, then enable the exit and allow them to go through.

This requires that the exit be in an unlocked state at all times, and only be locked by the game, when the player is in the room AND the NPC is in the room, until such time as the player either does what the NPC wants, kills the NPC, or accomplishes some other victory condition. At which point, the game unlocks the exit.

It's a tad more complex than just creating an illusion.


hegemonkhan
04 Feb 2017, 05:04

oh definately, it depends on a case bs case basis as what is the situation/scenerio/design at hand. If you have a bunch of stuff to handle, of course you'll handle it, so you don't have such a bug where the person locks themself out from being able to complete the game.

generally you want illusions of stuff than more directly/"physically" doing something, though I should've noted previously, for more complex/larger games, Object usage, is much more useful (OOP/OOD), than mere Attributes (as Objects are able to be referenced and hold multiple Attributes), but for simple individual scripting, using Attributes for an illusion of in-game "work"/action/event is better than a design that uses Objects like in the real world "physical objects" of moving them around, which is not how good programming works.


Anonynn
04 Feb 2017, 05:20

You could do something like this.

count = 0
foreach (obj, ScopeVisibleNotHeld ()) {
  if (DoesInherit(obj, "monster")) {
    if (obj.health > 0) {
      count = count + 1
    }
  }
}
  if (count >= 1) {
    msg ("<br/><font color=\"dedede\">You can't get away!</font color><br/>")
  }
  else {
    player.parent = this.to
  }

Essentially, if your enemies have "health" as an attribute, this script looks for that health and if the enemy/mob's health is above 0 --- they prevent you from leaving.
if (count >= 1) {
}
The number of the count is how many enemies you'd like the script to check.

This code is provided by Pixie! ^_^

Anonynn.