Scripting problem - 3 warnings chest

Hello, newbie here. I am trying to learn to script and have been watching this video and got stuck on this particular script. Can anyone give me some insight, please? Thank you.

I’ve been watching a youtube video and I’m currently stuck at 21:17. I think my code is correct, it matches but something is wrong because the goblin, just says the first line over and over.

RPG Design Workshop using NWN Aurora Toolset: Part 9 - Tags, Actions & Complex Conditionals (youtube.com)

"void main()
{

object oChest = OBJECT_SELF;
object oPC = GetLastUsedBy();
object oChieftain = GetNearestObjectByTag(“ChiefSnaggleTooth”);

if(GetLocalInt(oChest,“nUsedTries”) == 0) {
AssignCommand(oChieftain,ActionSpeakString(“Hey! Get away from my chest!”,TALKVOLUME_SHOUT));
SetLocalInt(oChest,“nUseTries”,GetLocalInt(oChest,“nUseTries”)+1);
}

if(GetLocalInt(oChest,“nUsedTries”) == 1) {
AssignCommand(oChieftain,ActionSpeakString(“Back away, This is your last warning!”,TALKVOLUME_SHOUT));
SetLocalInt(oChest,“nUseTries”,GetLocalInt(oChest,“nUseTries”)+1);
}

if(GetLocalInt(oChest,“nUsedTries”) == 2) {
AssignCommand(oChieftain,ActionSpeakString(“Alright, you asked for it!”,TALKVOLUME_SHOUT));

}

}"

1 Like

if(GetLocalInt(oChest,“nUsedTries”) == 0) {

should have been

if(GetLocalInt(oChest,“nUseTries”) == 0) {

Figured ill leave this comment as a lesson. :grin: :clown_face:

Try this maybe? I have to go to bed now. Just made a quick new version of your script. Untested.

void main()
{

object oChest = OBJECT_SELF;
object oPC = GetLastUsedBy();
object oChieftain = GetNearestObjectByTag("ChiefSnaggleTooth");

	if(GetLocalInt(oChest,"nUseTries") == 2)
	{
		AssignCommand(oChieftain,ActionSpeakString("Alright, you asked for it!",TALKVOLUME_SHOUT));

	}

	else if(GetLocalInt(oChest,"nUseTries") == 1)
	{
		AssignCommand(oChieftain,ActionSpeakString("Back away, This is your last warning!",TALKVOLUME_SHOUT));
		SetLocalInt(oChest,"nUseTries",GetLocalInt(oChest,"nUseTries")+1);
	}

	else if(GetLocalInt(oChest,"nUseTries") == 0) 
	{
		AssignCommand(oChieftain,ActionSpeakString("Hey! Get away from my chest!",TALKVOLUME_SHOUT));
		SetLocalInt(oChest,"nUseTries",GetLocalInt(oChest,"nUseTries")+1);
	}


}
2 Likes

A good way to keep from having variable spelling issues is to use constants.

Constants are a predefined variable that is checked in the code and will give you an error if it is spelled incorrectly.

example:

constant string USE_TRIES = "nUseTries";
void main()
{
    object oChest = OBJECT_SELF;
    object oPC = GetLastUsedBy();
    object oChieftain = GetNearestObjectByTag(“ChiefSnaggleTooth”);
    if(GetLocalInt(oChest,USE_TRIES) == 0) {
        AssignCommand(oChieftain,ActionSpeakString(“Hey! Get away from my chest!”,TALKVOLUME_SHOUT));
        SetLocalInt(oChest,USE_TRIES,GetLocalInt(oChest,USE_TRIES)+1);
    }
    if(GetLocalInt(oChest,USE_TRIES) == 1) {
        AssignCommand(oChieftain,ActionSpeakString(“Back away, This is your last warning!”,TALKVOLUME_SHOUT));
        SetLocalInt(oChest,USE_TRIES,GetLocalInt(oChest,USE_TRIES)+1);
    }
    if(GetLocalInt(oChest,USE_TRIES) == 2) {
        AssignCommand(oChieftain,ActionSpeakString(“Alright, you asked for it!”,TALKVOLUME_SHOUT));
    }
}
3 Likes

@Aurelius I think I realized now that your script actually worked when you had fixed the variable name, right? So my own script was quite unnecessary, I guess. I read your post too fast last night, so sorry about that. In any case, @Philos way of doing it is the best. I know @kevL_s has pointed this out many times to me, to use constants at the beginning of the script to avoid spelling mistakes.

2 Likes

@andgalf

Actually, I think your script was fine. i.e. It required “else if”, otherwise, it kept firing the next “if” because it was always ready to fire due to the addition.

1 Like

@Lance_Botelle - I didn’t mean mean my didn’t work. I think it would have worked just as fine as Philos’ and Aurelius’ own script, just that it was unnecessary since Aurelius figured out what was wrong in the script.

@andgalf

I think the script requires the “else if”. Otherwise, the var sets and allows the next condition to fire too. I.e. If 0 (zero) is passed, the first condition is fired, which adds 1 to the var, now 1, which passes at the next check. So that also fires, adds one to the var, now 2, which fires the last condition too. Your script is the only one that does not fire all conditions if 0 is passed.

2 Likes

@Lance_Botelle - Hmm, didn’t think about that.

1 Like

Yea, I should have checked the logic, I was just helping with the misspelling issue. Definitely needs if else.

2 Likes

you might find this take a bit interesting …

const string USES = "nUsedTries";

void main()
{
	object oChieftain = GetNearestObjectByTag("ChiefSnaggleTooth");
	if (GetIsObjectValid(oChieftain))
	{
		string bark;

		switch (GetLocalInt(OBJECT_SELF, USES))
		{
			default:
				bark = "Alright, you asked for it!";
				// todo: If chieftan is not already in combat
				// - set to hostile faction
				// - call DetermineCombatRound() vs GetLastUsedBy()
				break;

			case 0:
				SetLocalInt(OBJECT_SELF, USES, 1);
				bark = "Hey! Get away from my chest!";
				// todo: turn chieftan to face GetLastUsedBy()
				break;

			case 1:
				SetLocalInt(OBJECT_SELF, USES, 2);
				bark = "Back away, This is your last warning!";
				// todo: turn chieftan to face GetLastUsedBy() and emote vigorously
				break;
		}

		AssignCommand(oChieftain, ActionSpeakString(bark, TALKVOLUME_SHOUT));
	}
}
1 Like