Jump to content

Scripting Question - Using Reference Variables "Globally"


Recommended Posts

Hello all, I have a question about scripting that I can't quite wrap my head around. In a mod I am working on, I have a "daily schedule" scripted out for 3 NPC's. Due to the way I have created it, each NPC has a script attached that can sometimes make a call for a different NPC (using their persistent reference ID I set up in the CS) to perform an action (I.E. something like NPCRef1.addscriptpackage TravelToSpecificLocation). Using this as an example, all of these scripts currently refer to specific NPC's (again, using their persistent reference ID) - I'd like to make it more dynamic if possible and switch out NPCs as needed.

 

For context - I set it up this janky way (instead of using something like a single quest script) because each script uses a number of "OnPackageDone" blocks that (as I understand it) have to be run on the NPC performing the package.

 

What I'm trying to do now is come up with a scripted spell that, when cast on an NPC, would pass that reference (preferably only if they are set as a persistent ref, not sure if that's possible or not) to a "global" reference variable which in turn could be used in place of the specific reference IDs to each NPC I am currently using. For the scripted spell, here's a VERY rough draft:

scn MyScript

ref MyRef1
ref MyRef2
ref MyRef3
short counter

begin ScriptEffectStart

if counter == 0 && isActor == 1
   set MyRef1 to getself
   set counter to 1
elseif counter == 1 && isActor == 1 && getself == MyRef1
   Message "This NPC has already been selected. Please choose another", 5
elseif counter == 1 && isActor == 1 && getself != MyRef1
   set MyRef2 to getself
   set counter to 2
elseif counter == 2 && isActor == 1 && (getself == MyRef1 || getself == MyRef2)
   Message "This NPC has already been selected. Please choose another", 5
elseif counter == 2 && isActor == 1 && getself != MyRef1 && getself != MyRef2
   set MyRef3 to getself
   set counter to 0
endif

end

I know the code above is missing some reset logic, but this just gives an idea of what I have in mind.

 

Where I am struggling with though is how do I (using the example above) use "MyRef1", "MyRef2" & "MyRef3" in other scripts? Or is this even possible? I'd prefer to not use OBSE if possible, but I'd make a consideration if there's no other way with vanilla commands.

 

 

 

Also, if you have any comments/tweaks/suggestions to my code above - I would highly appreciate them!

Edited by kingtitan
Link to comment
Share on other sites

You can make content of variable to be shared through quest variable. This technique is often used by dialog quests - quest script iself doesn't contain any active begin-end blocks, only variables themselves and so quest is sometimes used as shared variables databank to be accessed by other scripts.

Edited by RomanR
Link to comment
Share on other sites

You can't use variables in the script-effect-script that way, because the that kind of script doesn't continue to live after it's done. So, every time the spell is cast it creates a new instance of that script and the local vars are all set to zero.

 

You need a quest or object script for that. Then you refer to the variables via the quest or the ref-id of the object for object scripts (like those on an NPC). To access the vars of an object script, the object must be persistent.

 

If you don't want to make an NPC persistent, so that your script can work on any in the game, you need to store the data you want to save about them in a quest script.

Edited by GamerRick
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...