Jump to content

How to call a script's function in another script, with no property?


Recommended Posts

Posted

I want to use in script A a function from script B like: myscriptB.functionB(arg)

 

I'm reading this: https://www.creationkit.com/index.php?title=Function_Reference#Accessing_Functions_From_Other_Scripts

 

There are written two methods:

 

1) make a property in A, fill it with B, ok easy and obvious. But i hate filling property, especiall with TESVedit it's annoying

 

2) i can avoid it by using a variable but i dont understand the explanation.

 

I think i have to write something like ScriptB myscriptB = (?) as ScriptB and then call like above, but i dont get what is the (?) part. It says "an object the script is attached to"

What is that? Both my script A and B extend TopicInfo cuz they are for dialogues. Do i have to write in (?) the actor that is speaking? Compiler gives me error with that

 

 

Posted

Use Game.GetFormFromFile() to get the form object the script is on and then cast it to actual script object. 

 

For example, you can do this with my mod sexlab to get the api script without creating a property and/or hard mod dependency by doing this:

 

; // Get Quest object at 0xD62 in esm and cast it to attached script "SexLabFramework"
SexLabFramework SexLab = Game.GetFormFromFile(0xD62, "SexLab.esm") as SexLabFramework
if SexLab != none
	; // do stuff with the variable SexLab.
endIf

 

Posted (edited)

Another solution if the script is a quest script:

 

SexLabFramework SexLab = Quest.GetQuest("SexLabQuestFramework") as SexLabFramework

 

using Form IDs has two disadvantages:

  1. Form IDs might change due a mod reorganization (unlikely)
  2. Modder needs to fiddle around with magic numbers instead of human readable text


And Game.GetFormFromFile() produces warnings in the log if the mod is not installed. If you want to avoid warnings that can be misunderstood as errors you have to write
 

; // Get Quest object at 0xD62 in esm and cast it to attached script "SexLabFramework"
if Game.GetModByName("SexLab.esm") != 255
  SexLabFramework SexLab = Game.GetFormFromFile(0xD62, "SexLab.esm") as SexLabFramework
  if SexLab != none
	  ; // do stuff with the variable SexLab.
  endIf
endif

which is really unhandy for such a simple use case

Edited by zaira

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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