Jump to content

Need Help: Casting/Calling Scripts from Package Fragments


jayne2132

Recommended Posts

The situation: You have a package (NOT a quest package). You attach MyPackageScript to the package. From a fragment (on begin, on end, on change), how do you call a MyFunction() in MyPackageScript?

 

In all my testing, this is the only thing I've been able to put into a fragment without getting compiler errors:

((Parent as Form) as MyPackageScript).MyFunction()    ; non-working example

However, in game, the package starts, the fragment executes, but MyFunction() does not run.

 

Unless I'm missing something obvious here, I'm about ready to conclude that this might not be possible. I haven't found any examples from the base game that do what I'm trying to. For example, Nilsine's flower basket package On Begin casts a script and calls a function in it -- but the script is attached to the actor, not the package.

 

However, why would the package window let you attach scripts if you can't use them?

 

(Imperfect) Workarounds:

- Making MyFunction() a global function and call it, but there's no need then to attach it to the package, and you'd have to define any properties in the package fragment, and send them to the script.

- Attaching the script to the actor (like in Nilsine's case), but this may not be efficient if you have multiple actors using the package who are not based on a mutual template

 

 

Thanks in advance for any insight!

 

Link to comment
Share on other sites

The fragments are functions. You cannot define a function inside of another function.

 

Solution to try:

Edit the actual script file and add your function to the bottom of the script. Save & compile.

Go back to the fragment in question and use your function as you would with any other local function.

Link to comment
Share on other sites

The fragments are functions. You cannot define a function inside of another function.

 

Solution to try:

Edit the actual script file and add your function to the bottom of the script. Save & compile.

Go back to the fragment in question and use your function as you would with any other local function.

 

 

Thanks for your reply. I can see that would be a viable workaround. However, I'm talking about attaching another external script file to the package because I've found the fragment scripts rather buggy and difficult to use. If this isn't possible, that may be the solution.

Link to comment
Share on other sites

I didn't realize you were adding a NEW and separate script to the package.

 

Create a dummy fragment first so that the fragment script itself will be created.

Edit the fragment script itself and manually add a property line as follows:

OtherScriptName Property SomeVar Auto

Save & Compile

Edit the fragment and add the function call. Something like:

SomeVar.SomeFunction()

 

Do be sure to fill that property for the script. It has to be done manually. When done correctly, it will give you as a list of options all the objects that the script is attached to. Most cases it is just one object but it can be multiple objects. Simply pick the correct object holding the instance of the script you want to use.

 

This setup has worked in a lot of situations for me. I don't see why it would not work here.

Link to comment
Share on other sites

This setup has worked in a lot of situations for me. I don't see why it would not work here.

 

It worked! Thanks so much. I can see this would be useful in other situations, too. I didn't realize you could make the script a property.

 

Note for others trying the same thing:

When I created the new script in the package script box, it automatically extended 'ObjectReference'. I had to change it to extend 'Package' to be able to select it in the Properties drop-down.

 

Edited to add: Have you thought about adding this to the wiki? (unless it's already there and I missed it). This could be helpful to others, I'd think.

Edited by jayne2132
Link to comment
Share on other sites

This is awesome. I can't believe I didn't know about this -- I've been playing with it the last few days and it really opens up the possibilities.

One more question. Sometimes I get the wrong selector in the Properties window. There doesn't seem to be any clear reason why, and eventually it fixes itself, but I haven't figured out a reliable way to fix it. Sometimes closing and reopening creation kit will do it, but not always.

Is this a known bug? Is there any reliable way to get it working?

Thanks!

One is working...
http://i60.tinypic.com/sq057c.jpg

The one below is not...

http://i58.tinypic.com/2jdjhh4.jpg

Link to comment
Share on other sites

Same quest?

 

Are you trying to link to the script within the same session that you created the script? If so, that may explain it. Create your scripts first. Close all open records. Reopen the records and then fill the properties. Sometimes the CK won't update an open record's information about what is available. Closing and reopening should fix it.

 

But I've never experienced this, it is the first I've heard of it. So... take it all with a grain of salt.

 

I would have had a ready explanation if you were attaching the script(s) to place-able objects.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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