Jump to content

Photo

Calling function from another script


  • Please log in to reply
7 replies to this topic

#1
dizietemblesssma

dizietemblesssma

    Old hand

  • Premium Member
  • 588 posts

Hi,

I have succesfully referenced variables etc. in one script from another script in the past, but I've noticed that it has always been a script attached to a quest (MCM quest) that was referenced as a property in the CK.

I've noticed this because I can't reverse this, I can't write:

script1 property sc auto

in my script2 when script2 is the quest script, and script1 is just another script that I compiled.

 

So I made a dummy quest and attached script1 to it, but still in the CK I cannot choose it as property for script 2.

 

What would script1 need to be attached to for the CK to offer it as an option when filling properties?

 

 

diziet

 

 

edit: sudden thought, is it whether the script1 is referenced by something in the same plugin that has script2 attached?



#2
Rasikko

Rasikko

    The Arbiter of Time

  • Members
  • PipPipPipPip
  • 1,237 posts

I'm probably misunderstanding the question but here goes..

 

Script1 needs to be the exact name of the quest script.

 

Example.

 

Say your quest script is named TeleportWhiterunQuestScript, the script property needs to be:

TeleportWhiterunQuestScript property TWQScript auto

You can do it the opposite way as well through casting and assignment.

Quest property TeleportWhiterunQuest auto
 
 
Event OnInit()
 
    TeleportWhiterunQuestScript TWQScript = TeleportWhiterunQuest as TeleportWhiterunQuestScript
 
    TWQScript.SomeFunctionItHas()

EndEvent

This method is not exclusive to quest objects and its scripts, this works for all objects and their scripts, including ActiveMagicEffect scripts(those are the least useful through since by design they don't stick around).


Edited by Rasikko, 17 September 2020 - 04:26 AM.


#3
dylbill

dylbill

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,370 posts

Yes, to reference another script, they both have to be attached to something in the same plugin. To get around that, you can use the GetFormFromFile function in an Event: https://www.creationkit.com/index.php?title=GetFormFromFile_-_Game 

 

Example: 

 

TestModQuestScript ScriptRef

Event OnInit()
    ScriptRef = ((Game.GetFormFromFile(0x02000D62, "TestMod.esp") As Quest) as TestModQuestScript)
    Utility.Wait(1) 
    ScriptRef.SomeFunction()
EndEvent

The form ID you use in GetFormFromFile should be the form the script you're trying to reference is attached to in the other plugin. Change TestModQuestScript to the name of the script you're trying to access.



#4
dizietemblesssma

dizietemblesssma

    Old hand

  • Premium Member
  • 588 posts

Thanks for this.:)

 

 

The form ID you use in GetFormFromFile should be the form the script you're trying to reference is attached to in the other plugin.

Woudn't the formid depend on load order?

 

When compling a script the plugin with the other script to be referenced needs to be loaded in the CK?  Is this somehow automagically dealt with by the game engine?

 

 

diziet



#5
IsharaMeradin

IsharaMeradin

    The Pale Redguard

  • Members
  • PipPipPipPipPip
  • 8,992 posts

Thanks for this. :smile:

 

 

The form ID you use in GetFormFromFile should be the form the script you're trying to reference is attached to in the other plugin.

Woudn't the formid depend on load order?

 

When compling a script the plugin with the other script to be referenced needs to be loaded in the CK?  Is this somehow automagically dealt with by the game engine?

 

 

diziet

No. It is not dependent on load order.  The ID used is relative to the supplied plugin and thus the first two digits will always be 00.

 

I do not recall if the PSC needs to be present or not when using GetFormFromFile.  All my uses had them present anyway.  Sure way to find out would be to try compiling without said PSC present, if the compiler complains then you need it.  As far as the game goes, if the supplied plugin is not present and papyrus logging is on there will be a harmless notice written.  This means that you should always do a sanity check to ensure that the variable intended to hold the result of GetFormFromFile is not null.



#6
dizietemblesssma

dizietemblesssma

    Old hand

  • Premium Member
  • 588 posts

No. It is not dependent on load order.  The ID used is relative to the supplied plugin and thus the first two digits will always be 00.

Aha!  I saw the example:

ScriptRef = ((Game.GetFormFromFile(0x02000D62, "TestMod.esp") As Quest) as TestModQuestScript)
 

and got confused, thankyou.

 

 

diziet



#7
dizietemblesssma

dizietemblesssma

    Old hand

  • Premium Member
  • 588 posts
TestModQuestScript ScriptRef

Event OnInit()
    ScriptRef = ((Game.GetFormFromFile(0x02000D62, "TestMod.esp") As Quest) as TestModQuestScript)
    Utility.Wait(1) 
    ScriptRef.SomeFunction()
EndEvent

 

Holy Compiling Batman! This works, yes!. :smile:

 

diziet



#8
dylbill

dylbill

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,370 posts

Awesome, glad you got it working!






Page loaded in: 0.927 seconds