Jump to content

Access variable from other script


Recommended Posts

There is a quite a bit online about how to do this but I just can't make it work. Apparently I have to turn a variable into a property?

 

So please, some example code for the following:

 

script_1 - has the variables in it that other scripts will use

 

script_2 - will use the variables from script_1

 

i currently have something like

 

in script_2:

dz_mcm_undressing_script property dz_mcm auto

 

and in script_1 (dz_mcm_undressing_script):

bool property disrobes auto

 

according to what I've read this should mean that in script_2 I can use:

dz_mcm.disrobes

to access the disrobes variable in script1

 

for info script_1 is intended to be a mcm menu and there will more than one mod that will access the same menu variables.

 

Now using this format I can compile my actual scripts, but the second script never acknowledges the value of 'disrobes' as being anything other than false, even if I assign it true in script_1.

The form dz_mcm.disrobes is accepted when compiling script_2 but when the mcm menu changes (script_1) the value of 'disrobes'; dz_mcm.disrobes doesn't change.

 

I've read that I need to attach both scripts to the same object, if so what about the fact that one script is already attached to a trigger box and the mcm script (script_1) is attached to a quest (as per mcm menu instructions)?

 

Thanks in advance

 

diziet

Link to comment
Share on other sites

The scripts do not need to be attached to the same object. If they did then my Random Mining MCM mod would not work. I access the MCM script to get the player selected values in order to modify the ore vein script's behavior.

 

The way you have described your set up, it should work. Perhaps posting script 2's code will help shed some light. There could be some logic issues preventing the script from executing as desired even if the value is being properly updated.

Link to comment
Share on other sites

Okay, I'll post the two scripts here, they are my first attampts at writing scripts and one borrows a lot from someone else, first the mcm menu script (alll mine), note it's full of commented debugs and attempts to get the value of 'disrobes' displayed by the mcm menu - which in fact worked and gave the expected values!

 

 

 

Scriptname dz_mcm_undressing_script extends SKI_ConfigBase

 

bool property disrobes auto

 

int allowPlayerSetting
int thirdPersonSetting

;bool _toggleValue = false
bool _toggle_State1 ;= false
bool _toggle_State2 ;= false


event OnPageReset(string page)
{Called when a new page is selected, including the initial empty page}
SetCursorFillMode(TOP_TO_BOTTOM)

AddHeaderOption("Player Disrobing Options")

AddEmptyOption()

allowPlayerSetting = AddToggleOption("Player Undresses", _toggle_State1)

AddEmptyOption()

thirdPersonSetting = AddToggleOption("Force Third Person", _toggle_State2)


endEvent

event OnOptionSelect(int a_option)

if (a_option == allowPlayerSetting)
_toggle_State1 = !_toggle_State1
SetToggleOptionValue(a_option, _toggle_State1)
;debug.messagebox("toggle1 happened")
;if (_toggle_State1 == true)
; Disrobes = true
;elseIf (_toggle_State1 == false)
; Disrobes = false
;endIf
elseIf (a_option == thirdPersonSetting)
_toggle_State2 = !_toggle_State2
SetToggleOptionValue(a_option, _toggle_State2)
endif
;debug.messagebox("disrobes =" +Disrobes)
endEvent

event OnOptionDefault(int a_option)
if (a_option == allowPlayerSetting)
_toggle_State1 = true
SetToggleOptionValue(a_option, _toggle_State1)

elseif (a_option == thirdPersonSetting)
_toggle_State2 = true
SetToggleOptionValue(a_option, _toggle_State2)
endIf
endEvent

 

 

 

then the script that needs to use variables, note all I've tried to do so far is get the variable recognised by the script (line 53), the logic using the variable after that has yet to be written:

 

 

 

Scriptname dz_vh_nakedactivator extends ObjectReference

;dz_mcm_undressing_script property disrobes auto

spell property dz_vh_NakedSpell auto

dz_mcm_undressing_script property mcm auto


Actor Property PlayerRef Auto

;define variables from mcm menu
;bool property PlayerDisrobes auto
;bool property ForcedThirdPerson auto

;bool disrobes

;define variable for camera state
int iCameraState

;define variables for weapons in each hand
Weapon equippedleft
Weapon equippedright

;define varialbe for player shield
Armor player_shield

;define variables for the follwing slots,30,31,32,33,34,35,36,37,38,39,40,41,42,43
Armor slot32
Armor slot30
Armor slot31
Armor slot33
Armor slot34
Armor slot35
Armor slot36
Armor slot37
Armor slot38
Armor slot39
Armor slot40
Armor slot41
Armor slot42
Armor slot43

;define variables for possible spells
Spell spell0
Spell spell1
Spell spell2
Spell spell3

Event OnTriggerEnter(ObjectReference triggerRef)
;debug.notification("found something!")

debug.messagebox("Disrobes variable from MCM script equals " +mcm.disrobes)
Actor akactionRef = triggerRef as Actor
if (akActionRef != game.getplayer() && !akActionRef.isincombat())
;debug.notification("effect applied")
dz_vh_NakedSpell.cast(akactionref,akactionref)
else

;find camera state
iCameraState = Game.GetCameraState() ; this is an skse function
;change to third person if not already
If(iCameraState == 0)
Game.ForceThirdPerson()
Endif

;get weapons
equippedleft = PlayerRef.GetEquippedWeapon(true)
equippedright = PlayerRef.GetEquippedWeapon()

;get shield
player_shield = PlayerRef.GetEquippedShield()

;get spells
spell0 = PlayerRef.GetEquippedSpell(0)
spell1 = PlayerRef.GetEquippedSpell(1)
spell2 = PlayerRef.GetEquippedSpell(2)
spell3 = PlayerRef.GetEquippedSpell(3)

;get armor slots
slot32 = PlayerRef.GetEquippedArmorInSlot(32)
slot30 = PlayerRef.GetEquippedArmorInSlot(30)
slot31 = PlayerRef.GetEquippedArmorInSlot(31)
slot33 = PlayerRef.GetEquippedArmorInSlot(33)
slot34 = PlayerRef.GetEquippedArmorInSlot(34)
slot35 = PlayerRef.GetEquippedArmorInSlot(35)
slot36 = PlayerRef.GetEquippedArmorInSlot(36)
slot37 = PlayerRef.GetEquippedArmorInSlot(37)
slot38 = PlayerRef.GetEquippedArmorInSlot(38)
slot39 = PlayerRef.GetEquippedArmorInSlot(39)
slot40 = PlayerRef.GetEquippedArmorInSlot(40)
slot41 = PlayerRef.GetEquippedArmorInSlot(41)
slot42 = PlayerRef.GetEquippedArmorInSlot(42)
slot43 = PlayerRef.GetEquippedArmorInSlot(43)

;now check if trigger is the player and the player is not in combat
if (akActionRef == game.getplayer() && !akActionRef.isincombat())
akActionRef.unequipall()
endif
endif
endEvent

Event OnTriggerLeave(ObjectReference triggerRef)
Actor akactionRef = triggerRef as Actor
if (akActionRef != PlayerRef)
;debug.notification("effect removed")
akActionRef.dispelspell(dz_vh_NakedSpell)
else
;return to first person if necessary
if (iCameraState == 0)
Game.ForceFirstPerson()
Endif

;if the trigger is the player then equip all weapons, spells and armor

;equip weapons
PlayerRef.EquipItemEx(equippedleft,2,true) ;this is an skse function
PlayerRef.EquipItemEx(equippedright,1) ;this is an skse function
;PlayerRef.EquipItem(equippedright)
;PlayerRef.EquipItem(equippedleft)

;equip shield
PlayerRef.EquipItem(player_shield)

;equip spells
PlayerRef.EquipSpell(spell0,0)
PlayerRef.EquipSpell(spell1,1)
PlayerRef.EquipSpell(spell2,2)
PlayerRef.EquipSpell(spell3,3)

;equip armor
PlayerRef.EquipItem(slot32,false,true)
PlayerRef.EquipItem(slot30,false,true)
PlayerRef.EquipItem(slot31,false,true)
PlayerRef.EquipItem(slot33,false,true)
PlayerRef.EquipItem(slot34,false,true)
PlayerRef.EquipItem(slot35,false,true)
PlayerRef.EquipItem(slot36,false,true)
PlayerRef.EquipItem(slot37,false,true)
PlayerRef.EquipItem(slot38,false,true)
PlayerRef.EquipItem(slot39,false,true)
PlayerRef.EquipItem(slot40,false,true)
PlayerRef.EquipItem(slot41,false,true)
PlayerRef.EquipItem(slot42,false,true)
PlayerRef.EquipItem(slot43,false,true)

;debug.messagebox("Leave Triggered")
endif
endEvent

 

 

 

this script also full of commented fragments:)

 

the scond script is attached to a trigger box and the first toa quest as described in the mcm instructions

 

diziet

Link to comment
Share on other sites

Change that line 53 in your second script to this.

	If mcm.disrobes == True
		debug.MessageBox("Disrobes variable from MCM script is true")
	Else
		debug.messagebox("Disrobes variable from MCM script is false")
	EndIf

Basically, forcing papyrus to get the variable value and compare it to a known quantity.

 

Did you fill the property for the script with the quest that has the target script attached? If not, that would explain why the variable fails to update.

Link to comment
Share on other sites

 

Did you fill the property for the script with the quest that has the target script attached? If not, that would explain why the variable fails to update.

Is this what you mean?

 

the quest name is dz_mcm for the mcm menu.

I've done this and altered the script as suggested, but still no joy, the message box still says the variable is false,

 

diziet

Link to comment
Share on other sites

Are you testing on a new game? I can see some scenarios where it might not work if your testing is not done on a new game or a save that has not had any part of the mod in question loaded yet.

 

Otherwise, I am at a loss as to the reason why it is not working for you.

I'm testing on a save that has never has this mcm in it, or the mod that the menu is intended for either; that mod I already wrote, I just want to be clever and add an mcm menu to it!

I'll mention that I'm creating a new esp for the mcm stuff, though the scripts from the exisitng mod are being edited to recognise the mcm variables. Is this wrong? Should the mcm menu be in the main mod? Currently my mod has most of its functionality without skse and I was hoping to make the mcm menu a simple add-on.

 

The save is one that I downloaded from nexus especially for the purpose of modding:)

Link to comment
Share on other sites

That would be the issue. By having two separate plugins, you have to make the one being referenced a parent master. Otherwise, any referencing being done will not work. Having your MCM as a parent master ruins the "simple add-on" aspect.

 

If you really want the MCM to be a separate plugin, here is your solution:

  • Create MCM script in its plugin

  • Make the following changes to your script(s) that are referencing variables / functions on the MCM script.

  • Ditch the property that is pointing to the MCM quest script

  • Add a local variable that you will use for the MCM script

  • Use GetFormFromFile to obtain the quest from your MCM plugin and cast it into the script and assign it to the local variable


 

Example code taken from my Random Mining MCM mod.

 

 

;added variables for Random Mining

Bool DoAlteration = false

Bool DoSmithing = false

Bool DidActivation = false

Bool DoOnce = false

abim_RM_MCM_QuestScript MCMScript

;----
;inside the OnCellAttach event

    MCMScript = (Game.GetFormFromFile(0x00000D62,"Random Mining MCM.esp") as Quest) as abim_RM_MCM_QuestScript

;ensures that the quest script is assigned to the local variable for later use in the script
 
;inside both OnActivate and OnHit events
            RandomizeOreAndStrikes(MCMScript.DoHardWorker,MCMScript.DoLazyman) ;Random Mining added function call

;sends MCM variables to a local function in order to modify behavior of the ore vein
 
;inside giveore function (an original local function on the script)
                GiveOreAndSkill(MCMScript.GiveSkill,MCMScript.DoLazyman) ;Random Mining added function call

;sends MCM variables to a local function in order to modify behavior of receiving items

 

 

 

 

Otherwise, just update the mod's initial plugin to have the MCM. There is nothing wrong with having two files available on your mod page. One with MCM and one without.

Link to comment
Share on other sites

  • 3 weeks later...

Okay, this is interesting stuff thankyou!

I'll be playing and testing for a while....

 

I may be back :smile:

 

diziet

Well I am back!

 

First off I managed to get the variable access from another scrip working, but only when both scripts are in the same esp.

Which is a problem for my mods, since each can be used individually; if I put an mcm quest in their esp, using two or more results in the mcm menu being repeated, and I only want one mcm menu.

I tried putting the mcm quest in a separate esp and making that a master of the other mods, I also tried putting my custom spell in that master mod to avoid the ugliness of duplicate spells (though that didn't seem to cause a problem).

 

I get the following error i papyrus.0.log:

Error: Property dz_naked_spell_ver2 on script dz_skse_ver2_nakedactivator attached to (0E005905) cannot be bound because <nullptr form> (0E000D62) is not the right type

 

this is with just the spell in the master mod, with the mcm quest in the master mod I get extra similar errors.

 

All the properties of the dz_skse_ver2_nakedactivator script are filled in via the creation kit, including "spell property dz_naked_spell_ver2 auto"

 

Is it not possible for a mod to use custom spells in other mods? D I have to reference them in a different way?

 

edit:

master mod has a spell made from the following scripts:

 

 

Scriptname dz_ver2_nakedeffectscript extends activemagiceffect
armor property JewelryRingGold auto
Event OnEffectStart(Actor akTarget, Actor akCaster)
debug.notification("effect script: adding effect")
akTarget.unequipall()
endEvent
Event OnEffectFinish(Actor akTarget, Actor akCaster)
debug.notification("effect script: removing effect")
akTarget.additem(jewelryringgold,1,true)
akTarget.equipitem(jewelryringgold,false,true)
akTarget.removeitem(jewelryringgold,1,true)
endEvent

 

 

which provides the magic effect used by the spell

 

 

 

 

Scriptname dz_skse_ver2_nakedactivator extends ObjectReference
spell property dz_naked_spell_ver2 auto
Actor Property PlayerRef Auto
;define variable for camera state
int iCameraState
;define variables for weapons in each hand
Weapon equippedleft
Weapon equippedright
;define varialbe for player shield
Armor player_shield
;define variables for  the follwing slots, 30 to 49, 52 to 61
Armor  slot32
Armor  slot30
Armor  slot31
Armor  slot33
Armor  slot34
Armor  slot35
Armor  slot36
Armor  slot37
Armor  slot38
Armor  slot39
Armor  slot40
Armor  slot41
Armor  slot42
Armor  slot43
Armor  slot44
Armor  slot45
Armor  slot46
Armor  slot47
Armor  slot48
Armor  slot49
Armor  slot52
Armor  slot53
Armor  slot54
Armor  slot55
Armor  slot56
Armor  slot57
Armor  slot58
Armor  slot59
Armor  slot60
Armor  slot61
;Armor  slot130
;Armor  slot131
;Armor  slot141
;Armor  slot142
;Armor  slot143
;Armor  slot230
;define variables for possible spells
Spell  spell0
Spell  spell1
Spell  spell2
Spell  spell3
Event OnTriggerEnter(ObjectReference triggerRef)
;debug.notification("found something!")
Actor akactionRef = triggerRef as Actor
if (akActionRef != game.getplayer() && !akActionRef.isincombat())
;debug.notification("effect applied")
dz_naked_spell_ver2.cast(akactionref,akactionref)
else
  ;find camera state
  iCameraState = Game.GetCameraState() ; this is an skse function
  ;change to third person if not already
  If(iCameraState == 0)
  Game.ForceThirdPerson()
  Endif
 
;get weapons
equippedleft = PlayerRef.GetEquippedWeapon(true)
equippedright = PlayerRef.GetEquippedWeapon()
;get shield
player_shield = PlayerRef.GetEquippedShield()
;get spells
spell0 = PlayerRef.GetEquippedSpell(0)
spell1 = PlayerRef.GetEquippedSpell(1)
spell2 = PlayerRef.GetEquippedSpell(2)
spell3 = PlayerRef.GetEquippedSpell(3)
;get armor slots
slot32 = PlayerRef.GetEquippedArmorInSlot(32)
slot30 = PlayerRef.GetEquippedArmorInSlot(30)
slot31 = PlayerRef.GetEquippedArmorInSlot(31)
slot33 = PlayerRef.GetEquippedArmorInSlot(33)
slot34 = PlayerRef.GetEquippedArmorInSlot(34)
slot35 = PlayerRef.GetEquippedArmorInSlot(35)
slot36 = PlayerRef.GetEquippedArmorInSlot(36)
slot37 = PlayerRef.GetEquippedArmorInSlot(37)
slot38 = PlayerRef.GetEquippedArmorInSlot(38)
slot39 = PlayerRef.GetEquippedArmorInSlot(39)
slot40 = PlayerRef.GetEquippedArmorInSlot(40)
slot41 = PlayerRef.GetEquippedArmorInSlot(41)
slot42 = PlayerRef.GetEquippedArmorInSlot(42)
slot43 = PlayerRef.GetEquippedArmorInSlot(43)
slot44 = PlayerRef.GetEquippedArmorInSlot(44)
slot45 = PlayerRef.GetEquippedArmorInSlot(45)
slot46 = PlayerRef.GetEquippedArmorInSlot(46)
slot47 = PlayerRef.GetEquippedArmorInSlot(47)
slot48 = PlayerRef.GetEquippedArmorInSlot(48)
slot49 = PlayerRef.GetEquippedArmorInSlot(49)
slot52 = PlayerRef.GetEquippedArmorInSlot(52)
slot53 = PlayerRef.GetEquippedArmorInSlot(53)
slot54 = PlayerRef.GetEquippedArmorInSlot(54)
slot55 = PlayerRef.GetEquippedArmorInSlot(55)
slot56 = PlayerRef.GetEquippedArmorInSlot(56)
slot57 = PlayerRef.GetEquippedArmorInSlot(57)
slot58 = PlayerRef.GetEquippedArmorInSlot(58)
slot59 = PlayerRef.GetEquippedArmorInSlot(59)
slot60 = PlayerRef.GetEquippedArmorInSlot(60)
slot61 = PlayerRef.GetEquippedArmorInSlot(61)
;slot130 = PlayerRef.GetEquippedArmorInSlot(130)
;slot131 = PlayerRef.GetEquippedArmorInSlot(131)
;slot141 = PlayerRef.GetEquippedArmorInSlot(141)
;slot142 = PlayerRef.GetEquippedArmorInSlot(142)
;slot143 = PlayerRef.GetEquippedArmorInSlot(143)
;slot230 = PlayerRef.GetEquippedArmorInSlot(230)
akActionRef.unequipall()
endif
endEvent
Event OnTriggerLeave(ObjectReference triggerRef)
Actor akactionRef = triggerRef as Actor
if (akActionRef != PlayerRef)
;debug.notification("effect removed")
akActionRef.dispelspell(dz_naked_spell_ver2)

else
  ;return to first person if necessary
  if (iCameraState == 0)
  Game.ForceFirstPerson()
  Endif
;if the trigger is the player then equip all weapons, spells and armor
;equip weapons
PlayerRef.EquipItemEx(equippedleft,2,true) ;this is an skse function
PlayerRef.EquipItemEx(equippedright,1)  ;this is an skse function
;equip shield
PlayerRef.EquipItem(player_shield)
;equip spells
PlayerRef.EquipSpell(spell0,0)
PlayerRef.EquipSpell(spell1,1)
PlayerRef.EquipSpell(spell2,2)
PlayerRef.EquipSpell(spell3,3)
;equip armor
PlayerRef.EquipItem(slot32,false,true)
PlayerRef.EquipItem(slot30,false,true)
PlayerRef.EquipItem(slot31,false,true)
PlayerRef.EquipItem(slot33,false,true)
PlayerRef.EquipItem(slot34,false,true)
PlayerRef.EquipItem(slot35,false,true)
PlayerRef.EquipItem(slot36,false,true)
PlayerRef.EquipItem(slot37,false,true)
PlayerRef.EquipItem(slot38,false,true)
PlayerRef.EquipItem(slot39,false,true)
PlayerRef.EquipItem(slot40,false,true)
PlayerRef.EquipItem(slot41,false,true)
PlayerRef.EquipItem(slot42,false,true)
PlayerRef.EquipItem(slot43,false,true)
PlayerRef.EquipItem(slot44,false,true)
PlayerRef.EquipItem(slot45,false,true)
PlayerRef.EquipItem(slot46,false,true)
PlayerRef.EquipItem(slot47,false,true)
PlayerRef.EquipItem(slot48,false,true)
PlayerRef.EquipItem(slot49,false,true)
PlayerRef.EquipItem(slot52,false,true)
PlayerRef.EquipItem(slot53,false,true)
PlayerRef.EquipItem(slot54,false,true)
PlayerRef.EquipItem(slot55,false,true)
PlayerRef.EquipItem(slot56,false,true)
PlayerRef.EquipItem(slot57,false,true)
PlayerRef.EquipItem(slot58,false,true)
PlayerRef.EquipItem(slot59,false,true)
PlayerRef.EquipItem(slot60,false,true)
PlayerRef.EquipItem(slot61,false,true)
;PlayerRef.EquipItem(slot130,false,true)
;PlayerRef.EquipItem(slot131,false,true)
;PlayerRef.EquipItem(slot141,false,true)
;PlayerRef.EquipItem(slot142,false,true)
;PlayerRef.EquipItem(slot143,false,true)
;PlayerRef.EquipItem(slot230,false,true)
;debug.messagebox("Leave Triggered")
endif
endEvent

 

 

 

which is attached to a trigger box, the trigger box being in an individual mod, the intent is to have multiple esps with trigger boxes that players can use as wished and all use the spell in the master mod, and eventually all access the mcm menu variables in the master mod - that version of the script not shown.

 

 

diziet

Link to comment
Share on other sites

  • Recently Browsing   0 members

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