Difficult Programming Error [Area Transitions]

Hello.

I am a new member to the Vault, I have come here to ask a question concerning programming.

See the Following Link:

Manast:
You could also use logical OR (||):
if(GetItemPosessedBy(oPC, “cat”)!=OBJECT_INVALID||GetItemPosessedBy(oPC, “dog”)!=OBJECT_INVALID||GetItemPosessedBy(oPC, “mouse”)!=OBJECT_INVALID)
{
return TRUE;
}
return FALSE;

I wish to construct a module where if a character has a Tome A or a Tome B they may leave.

I wanted to share this because it was an interesting, but albeit short example of an idea that was actually inspired by the “Monty Hall” Problem. If anyone is a fan of Thought Experiments.

Let me Show you what I am looking at…

Imgur
Imgur
Imgur
Imgur

As I am not a Professional Developer, but only a Amateur Avocational Gamer – I only wanted to build this module to test a simple puzzle out with the Aurora Toolset Once.

Thank you Kindly.

@Mannast’s code can be simplified to

return (GetItemPosessedBy(oPC, “cat”) != OBJECT_INVALID || GetItemPosessedBy(oPC, “dog”)!= OBJECT_INVALID);

for just 2 items. Other than that, what code have you got so far? Any?

BTW, See this pinned thread on how these forums work including how to put code into code boxes (read to the end as there have been updates since that thread started).

TR

It is not a good idea to “simplify” the code for beginners who barely understands it.

And if you really have to do it, then drop the parenthesis encapsulating the return statement :wink:

1 Like

@Shadooow I fundamentally disagree with both sentences. The simplified code is no more complex than the @Mannast code the op posted. The brackets ensure that the code they contain is evaluated before the return itself fires. Whether or not it is entirely necessary is neither here nor there. It makes me more comfortable.

Without any code of their own to examine it makes it difficult to assess exactly what level of help they require, hence why I asked. Had they asked for help with learning about scripting I could have pointed them towards various tutorials (and yes including my own).

TR

Is it really?

You used term simplified. But that depends on who is the recepient of that simplification.

If for the human user then your change makes it much more complex to understand. Especially for beginner.

If for the computer then ok, your code is simpler. But that was my point - simpler code for computer is not always simpler for human reader.

What do you mean? The extra parenthesis around the condition are absolutely useless and doesn’t change the behavior of the code. In fact it makes it only harder to read the code for human reader imo.

Yes I assumed the OP is beginner. But look at it this way. If the OP isn’t beginner then your simplification of code is hardly useful for him because he obviously knows that.

I never saw anyone who would be angry for others to send him “inefficient” code when he wanted help with something, experienced users will rewrite their code as they like. All you are achieving with your correction is confusing the beginners. Please either stop doing that or add explanation as to why you can write it like that, why it works and why it is better to write it like that.

@Rogue-Novice-1-1 Due to what is needed not being absolutely clear, I have assumed that you want code for a door that will only open if one or another book (or both) is in the PC’s possession. I have also assumed that you are more interested in having something that works (but see later) than in understanding how it actually works. What follows is the simplest (maybe over-simplified) script just in case I am wrong about that. In fact there are actually 2 scripts. Any questions about this don’t be afraid to ask. So you can see it in action I have created a small simple module (in NwN EE) to demonstrate it -

Rogue-Novice-1-1.7z (6.4 KB)

There are 8 jail cells and wide hall (where the PC starts). Each cell has a custom door with the scripts already in place. The 4 Northern cells each have a copy of “All About Cats” which is a custom book placed on the floor. The 4 Southern cells each have a copy of “All About Dogs” which is another custom book placed on the floor. All the books can be picked up by the PC.

The first script goes in the OnOpened event slot under scripts. What it does is first automatically close the door 7 seconds after it has been opened. Half a second later the door locks. This code is taken from the Newbie Scripting FAQ almost as is (I just shortened the time taken).

/////////////////////////////////////////////////////////
// Auto-Close Door / Lock Door
/////////////////////////////////////////////////////////
void main()
{
    DelayCommand(7.0f, ActionCloseDoor(OBJECT_SELF));
    DelayCommand(7.5f, SetLocked(OBJECT_SELF, TRUE));
}

The other script is the one that unlocks the door only if the PC has at least one of the books in their inventory. Unfortunately, there is a minor problem in that it is not seamless. Because there is no OnClicked event for doors I had to choose the next best event - OnFailToOpen. This means that this code only fires after the PC unsuccessfully tries to open the door. In other words, the PC needs to try a second time in order to open it. There may well be other ways around this but they will be much more complex. To cover this I make the PC speak a short “lock needs oiling” phrase.

/////////////////////////////////////////////////////////
// Auto-Unlock Door Only if they possess one (or both) of 2 books
/////////////////////////////////////////////////////////
void main()
{
    object oPC = GetClickingObject(); //who opened the door?
    object BookCat;
    object BookDog;
    int bValidBook;
    
    if(GetIsPC(oPC))
    {
        BookCat = GetItemPossessedBy(oPC, "Cat"); // has the PC got this book?
        BookDog = GetItemPossessedBy(oPC, "Dog"); // has the PC got this book?
        bGotValidBook = ((BookCat != OBJECT_INVALID) || (BookDog != OBJECT_INVALID)); // has the PC got either book?
        
        if(bGotValidBook) // see if the PC has gotten either in their possession
        {
            SetLocked(OBJECT_SELF, FALSE); // if so, unlock the door
            AssignCommand(oPC, SpeakString("Oh, I heard the door unlock! The lock must be old and need oiling"); // as this simple code doesn't fire until PC fails to open door first
        }
    }
}

I repeat, if you have any questions just ask.

BTW, there is a tool that you may well find useful -

LS-TK Script Generator (Lilac Soul’s Script Generator, updated for NWN 1.69)

TR

@Rogue-Novice-1-1

Welcome to the boards! :slight_smile:

If none of the books are required to be distinguished in any other way later in their usage (other than they have at least one/either of these books), then (arguably), you can give as many books with different names as you like, but give them all the same tag.

That way, you only need to determine if the PC has at least one of the books with that tag. The same tag can be used later if any version of that item (with the same tag) is required later too. If, however, you needed to distinguish between the “dog” and “cat” versions that the PC has escaped with, then you need the simple book name possession checks.

Other than that, I also ask what is the main thrust of your question?

EXAMPLE:

Cat Book (TAG: “QUEST_BOOK”)
Dog Book (TAG: “QUEST_BOOK”)

if(GetItemPosessedBy(oPC, “QUEST_BOOK”)!=OBJECT_INVALID)
{
return TRUE;
}
else
{
return FALSE;
}
1 Like

@Tarot_Redhand

Thank you for the quick reply – I would think that returning is in the correct direction to solving the problem

It is that only in certain cases this may not work because there are two items here: oPc [Player] and oItem [Items]

One is a subject, the other an object – that is subjectivity and objectivity.

The Scripting Framework only uses Objects
And all of them are inherited…

@Tarot_Redhand

Once again thank you for constructing a module

If anyone is able to download it, then it shall provide the best example of such mechanic

There is:

  • A Delay Command

  • A Reflexive Declaration (who opened the door?) – for example

  • However,

is it that BookCat or BookDog will return the correct object?
As that these objects are items – that is oItem

@Lance_Botelle

Welcome!

Yes Arguable that is sound and maybe true!

But I wonder are there any consequences to just…

if(GetItemPosessedBy(oPC, “QUEST_BOOK”) == oItem)
{
return FALSE;
}
else
{
return TRUE;
}

For example

[Once again I am only an Amateur Avocation Gamer]

@Rogue-Novice-1-1

I’m not sure what it is that you are asking or trying to say. :thinking:

I mean if you have defined what oItem is in your example, which you have to for the script to compile, then the script will pass or fail the condition subject to the tag of oItem.

Whether the condition returns true or false subject to whether you have the book item or not is up to you to interpret its intentions.

Yes/no?

There are either drugs involved or it’s an AI. That post was … odd but somewhat entertaining and almost poetic in a through-the-looking-glass sort of way :slight_smile:

4 Likes