Script Initialization Order
I have a strange bug that happens...intermittently...sometimes happens the first time I open my lens studio project but never returns after resetting the lens. The error sometimes causes issues when pushing to a device, and seems to be causing the lens to be rejected. It happens in this line:
hornScript[i].api.startHorn();
The error is:
TypeError: cannot read property 'api' of undefined
Everything in the scene is confirmed linked correctly; as soon as I reset the lens, the whole thing works without error. It makes me wonder if I misunderstand how the scripts are referencing each other and when. My SceneObject setup involving the scripts is like this:
>> Object 1 // has script component that contains the errant line above
>> >> Object 2 // has script component that includes the startHorn function
It seems pertinent that the parent object's script is making a call to its child's script- the parent's script takes account of the child's script like so (below is edited code from the parent object's script):
// @input SceneObject[] horns
// each of the horns is a SceneObject that is a child of this SceneObject
var hornScript = []
function initialize() {
// runs through the scene-input objects array and makes an array of their ScriptComponents for easier access
for(var i = 0; i < script.horns.length; i++) {
hornScript[i] = script.horns[i].getFirstComponent("ScriptComponent");
}
}
function startPop() {
// calls each ScriptComponent's startHorn()
for(var i = 0; i < script.horns.length; i++) {
hornScript[i].api.startHorn(); // you may recognize this as the offending line
}
}
var startEv = script.createEvent( "TurnOnEvent" );
startEv.bind( initialize );
var mouthOpenEv = script.createEvent( "MouthOpenedEvent" );
mouthOpenEv.bind( startPop );
The script objects for the horns are created in the local script context on LensTurnOn, but the line with the error is in a function tied to a mouth open event. And that error fires before any mouth is opened (as far as I can tell). And again, the whole project works, just not the first time it's loaded. Am I mishandling something? [EDIT: Added a few comments in my code.]
Hey Naomi,
Your code looks right and your situation definitely sounds like a bug, but I'm having trouble reproducing it in a test project. While I keep looking into it, can you try a small change and see if it helps?
Instead of referencing the SceneObjects and using getFirstComponent(), you can reference the ScriptComponents directly like this:
Then, you can use script.hornScripts directly instead of building the list using getFirstComponent(). I hope this can fix the problem if it's somehow caused by the references becoming invalid. Please let me know if this helps!
Jacob
Hi Jacob,
Thanks so much for the response! I'll give this a try. I had actually happened upon this during some further testing, realizing that this didn't run any errors:
...and submitted, still to no avail. I have just resubmitted with both of the inputs as ScriptComponent[] instead of using Component[] for one of them, so we'll see if that works. Of note, in the dialog box when I attach the scene components to the proper Script inputs, I am allowed to attach SceneObjects, but I can't expand those to select their components, even though the script input type is asking for a ScriptComponent.
That's supported by some more evidence: I had some print statements in this newest version:
...and these, in all iterations, for both horns and boppers, return the name of the scene object that was @input, followed by "Script". So @inputting the SceneObjects as ScriptComponents definitely still seems to reference them as SceneObjects, not directly referencing the scripts like you mentioned.
Here's the full project if you'd like to play around in a non-test environment:
https://www.dropbox.com/s/ul4lphvr3flzfbm/Grad19_v7i_LSPROJ.zip?dl=0
Thanks again!!
Hey it's live!
Seems changing it from Component to ScriptComponent did the trick!
Hey Naomi,
Glad it's working now!
Just a note, you should use "Component.ScriptComponent" as the input type instead of just "ScriptComponent", and using "Component" by itself isn't supported. I think that's where the strange behavior is coming from. I'm pretty surprised you were able to do that, I get error messages when I try. Are you using the Windows or Mac version?
Thanks!
That definitely sounds odd. I am using Windows. I also had a coworker QA test various iterations of the "final" lens on Mac (no errors found there either),
Not sure how relevant this is but another coworker designed the confetti particles on a mac as well. I opened his LSPROJ file in Windows to make a prefab, which I then imported into my main Windows project.
If you happen to find more insight on why importing the objects as SceneObjects wasn't working, I'd love to learn if there is anything I can do about that in the future! Thanks again for your help.
Hey Naomi,
It looks like there's a bug where changing an array property to an invalid type continues to work, instead of giving an error message and not drawing the script UI. So I think what happened was:
1. There was an //@input SceneObject[] list
2. This was changed to an unsupported type like //@input Component[] list
3. Due to this bug, because the new type was invalid, the list continued acting as the last valid type (SceneObject)
Hope this makes sense! We will try to get this bug fixed as it definitely can be confusing. To avoid this in the future you can make sure typenames are valid by temporarily adding a new property to test them like:
This should (correctly) give an error message when the script UI is drawn. This bug only occurs when switching an already existing array from a valid type to an invalid type.
Let me know if you have any more questions!
Interesting! I'm glad to have that in my bag o debugging tricks!
I'm still confused as to why that would affect the initial problem:
It seems like this section of code is using a valid type, SceneObject, and a valid function to access the ScriptComponent. As a refresher, this function was bound to "TurnOnEvent". Upon opening the LSProj, a Reference to Undefined ScriptComponent error sometimes ran from a reference to hornScript[] members within a function bound to "MouthOpenedEvent". It ran before a mouth was opened, and it definitely seems like a runtime-specific error. After a lens reset, the lens always worked perfectly, but was still unsubmittable.
I've added a version of the project with that older setup as shown above if that may be of assistance (v7g): https://www.dropbox.com/sh/wcqg1niwt0artyx/AACVbeOW4hUykrmhY6cLynnta?dl=0