Jump to content

Issue using variable inside an in-line script


Recommended Posts

I'm having trouble building an in-line script that will be attached to all the canned NPCs.  My variable substitution isn't working.  I'm getting a series of files named $npc_list(0) $npc_list(1), etc and the variable inside the code block isn't getting it's value inserted, either.  What am I doing wrong?

My code:

Spoiler

// Prep canned BG1 NPCs for leaving/staying after Korlasz quest and at the beginning of SoD and BG2
// NOTE: Jaheira, Khalid, Minsc, and Dynaheir don't get BG2 exit dialogues, they'll stick with the PC
// Imoen is dealt with separately
ACTION_DEFINE_ARRAY npc_list BEGIN
   Ajantis
   Jaheira
   Khalid
   Dorn
   Edwin
   Neera
   Baeloth
   Garrick
   Xan
   Minsc
   Dynaheir
   Alora
   Branwen
   Coran
   Eldoth
   Montaron
   Xzar
   Faldorn
   Kagain
   Kivan
   Quayle
   Rasaad
   Safana
   Sharteel
   Skie
   Tiax
   Viconia
   Yeslick
END

// Additions to all NPCs (except Imoen)
OUTER_FOR (npc = 0; npc < 28; ++npc) BEGIN

   <<<<<<<< .../npc_add.baf
   // Pre-SoD
   IF
      GlobalGT("#L_StartCaelarAttack","GLOBAL",0)
      Global("EndOfBG1","GLOBAL",1)
      Global("#L_$npc_list(%npc%)Modded","GLOBAL",0)      // <<=== variable substitution doesn't happen like it should...should replace it with the NPC name in the array above
      IsValidForPartyDialogue(MYSELF)
      OR(2)
         Global("#L_NPCSoD","GLOBAL",0)
         GlobalTimerExpired("#L_NPCSoD","GLOBAL")
      !AreaType(DUNGEON)
      CombatCounter(0)
      OR(2)
         GlobalGT("#L_StartCaelarAttack","GLOBAL",2)     // Imoen staying in the group
         !InPartyAllowDead("IMOEN2")
   THEN
      RESPONSE #100
         ClearAllActions()
         Dialogue(Player1)
   END
   >>>>>>>>
   EXTEND_TOP ~$npc_list(%npc%).bcs~ ~.../npc_add.baf~
END

 

EDIT: The answer = you can't get there from here.  If it's possible to use a variable (npc) to specify the array member to use ($npc_list(%npc%)) it has eluded me.  I had to revert to using ACTION_PHP_EACH instead.  I'll have to just use a variable inside that loop for the other things I needed.

Edited by Lauriel
Link to comment

The simplest way around is to use an intermediate variable (perhaps that's what you mean by 'variable inside that loop'). Put

OUTER_SPRINT this_npc $npc_list("%npc%")

and then use this_npc inside your inlined script.

It is possible to do it more directly, though. $npc_list("foo") can also be referred to as %npc_list_foo%. So you can put %npc_list_%npc%% into your inlined file. But it has to be done with care. You need two EVALUATE_BUFFERS (or one if you've got AUTO_EVAL_STRINGS, probably). If "npc" is "foo", the first EVALUATE_BUFFER replaces %npc_list_%npc%% with %npc_list_foo% and the second replaces it with its value. Most of the time it's easier to use an intermediate variable though.

Link to comment

I swear I tried some of that, but I guess not.  I'll try again.  I hate it when a simple thing like syntax throws me for a loop.  heh..for a loop...for loop.  Ok, it's early, I haven't had my coffee and I'm easily amused.  :)

Link to comment
5 hours ago, DavidW said:

The simplest way around is to use an intermediate variable (perhaps that's what you mean by 'variable inside that loop'). Put


OUTER_SPRINT this_npc $npc_list("%npc%")

and then use this_npc inside your inlined script.

So it was the quotes around the variable indicating the number (in this case npc) that I was missing.  Thank you so much!  This will make my life SO much simpler.  I just can't put into words how happy this makes me!

Link to comment
5 minutes ago, qwerty1234567 said:

I would still recommend to stick to ACTION_PHP_EACH, because "OUTER_FOR (npc = 0; npc < 28; ++npc)" is prone to mistakes if you later add or remove NPCs.

And I would for most instances, just in this one, it's better to limit how many I'm doing until I have them all done.  I'm building dialogues for each NPC and I'm not ready for all of them.  I suppose I can have a variable incremented inside the loop and have it do nothing if it goes above a number, but it's just easier to have the loop take care of that for me.

Link to comment
27 minutes ago, argent77 said:

If you want to stick with a classic for-loop then I'd recommend this expression instead:


OUTER_FOR (npc = 0; VARIABLE_IS_SET $npc_list(~%npc%~); ++npc) BEGIN

It works fine even if you later add or remove NPCs.

Good idea!  Thank you! :)

Link to comment

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...