NPC Proximity Check

I'm using the NpcLib.

At some point in my game, I want the adventurer to evade someone/something from either being captured/eaten.

I created a simple turnscript (I say simple, I was expecting it to be more complicated, but it does the job).

foreach (s, ScopeExitsForRoom(game.pov.parent)) {
  if (Contains (s.to,fred)) {
    msg ("You hear footsteps from the " + s.alias + ".")
  }
}

The code works, but the message 'You hear footsteps from the [direction].', appears when the character 'Fred', is in the same room as the player.

I then opened up the 'NpcType' object, and then added a similar code below, into the 'takeaturn' attribute.

foreach (s, ScopeExitsForRoom(game.pov.parent)) {
  if (Contains (s.to,this)) {
    msg ("You hear footsteps from the " + s.alias + ".")
  }
}

This time the code works as expected, with the message 'You hear footsteps from the [direction].', being displayed when the Npc character is in an adjacent room to the player.

I thought all turnscripts happened at the same time, the 'takeaturn' attribute of the Npc character is controlled by the 'NpcTurnScript'.

Can anyone shed any light on to why I'm getting two different results.


If you look at the core function RunTurnScripts, you'll see that all turnscripts are run in alphabetical order.

I'm assuming that the turnscript you mentioned first had a name which comes before NpcTurnScript in the alphabet; so it will run your script, display the message to let you know Fred is in the next room, and then NpcTurnScript runs and moves him into the current room.

You could get around this the way SysV-style init scripts do it - by naming your turnscripts something like 01_NpcTurnScripts and 02_HearFootsteps - which guarantees that they'll run in the order you expect while still having explanatory names. Or you could put all the code related to moving NPCs in one place; as you found.


Initially, the turn script I created didn't have a name. I've now given it a name, that comes alphabetically before 'NpcTurnScript' and it is still not displaying correctly. I've even tried moving the turnscript higher up the 'game tree', but that doesn't seem to work either.


a name, that comes alphabetically before 'NpcTurnScript'

As I said, you need to give it a name that comes after NpcTurnScript.


Sorry mrangel, I missed that bit.

Yes, working a lot better now thanks.

Good to know as well that turnscripts run in alphabetical order. Will have to watch that one again in the future.

Thanks again for the help and advice.


Good info.


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

Support

Forums