Question How do I control a light from a script on another object?

Jiraiya

Well-known member
Messages
481
Reactions
1,020
Points
93
Ok, I know you can setup triggers on objects etc but that doesn't do what I need.
I have a rather complex script to control various assets of a CUA. I want to control other items from that single script.
For example, I want to turn a light on and off in sync with other actions in the script that change the CUA. I don't know how to address the invisiblelight though?

I have managed to work out how to use triggers on other items to change variables in my script, but I can't go the other way yet.
 
If you happen to know the name of the InvisibleLight atom, in this case "InvisibleLight#2", you can do it like this:
C#:
using UnityEngine;
using UnityEngine.Events;

namespace MacGruber
{
    public class Light : MVRScript
    {   
        public override void Init()
        {   
            SetupButton("Toggle", OnButtonPress, false);
        }
        
        private void OnButtonPress()
        {
            JSONStorable lightStorable = GetAtomById("InvisibleLight#2")?.GetStorableByID("Light");
            if (lightStorable != null)
            {
                JSONStorableBool lightOn = lightStorable.GetBoolJSONParam("on"); // if you need it often, you might want to use a member variable to keep it around ("caching")
                lightOn.val = !lightOn.val;
                
                // Alternatives:
                //     bool GetBoolParamValue(string param)
                //     void SetBoolParamValue(string param, bool value)
                
                // Also other types: Float, Color, String, etc.
            }
                
        }
        
        private void SetupButton(string label, UnityAction callback, bool rightSide)
        {
            UIDynamicButton button = CreateButton(label, rightSide);
            button.button.onClick.AddListener(callback);
        }
    }
}

If you are looking for a more flexiable approach where the user of your plugin can setup triggers him/herself, check out how LogicBricks does it. It's offering VaM's regular trigger menus so the user can hook up anything they want. Of course that's a bit more complicated to do, but once you have it, its much more flexible.
1630136025553.png
 
Upvote 0
Hi, thanks SO MUCH for your help. It's amazing that there is somebody like you here in this community always willing to help. Thank you not only for this answer but all the work you do!
Last night I spent some hours and worked out this bit
JSONStorable lightStorable = GetAtomById("InvisibleLight#2")
To address other atoms directly but I was having issues trying to get deeper into the light to find the on/off variable.
)?.GetStorableByID("Light");
That's the part I couldn't get!
Thank you so much again. I will no go forth and add in the lighting control to my script. I don't need to worry about making it nice for other people to use because it's a single use custom script. I am sure people will be able to learn a lot by reading it and take parts for their own scenes but as a whole it will only be any use for my scene. I shall hope to release a small beta of "level 1" soon :)
Logic Bricks looks amazing btw. I should probably sit and read through the whole script as a learning experience.
One of my goals is to use as little external content as possible, even if it's free. If I could make this a single VaR with no dependencies I would be happy.
 
Upvote 0
If I could make this a single VaR with no dependencies I would be happy.
Well, my stuff is hub-hosted. People just have to press "Download All" (or "Scan Hub For Missing Packages...") and they get it automatically. In my opinion better than everyone trying to invent their own wheels. Of course, maybe its about the learning experience, not about the scene ;)
 
Upvote 0
It's certainly about the learning experience because I want to do things I don't think I can do with your tools. I would rather learn how to make my own scripts than how to use somebody else's tool which may or may not impose it's own limits on me.
My next challenge is to read and write back the camera position.
I want to be able to stop the player walking through walls.
I had an idea to affix an invisible sphere around the players head (camera position) and track it's movement. Any collision even would set the position of the camera back to the last tracked position (before the collision). As far as I can see this would simply stop you walking through walls or looking through objects.
(Yes, I am pretty much making a full Unity game in VaM as a scene...)
 
Upvote 0
Ok, another question. When I try to turn other atoms on and off I get an error about the "on" status being read only?
Based on your example above I have this,
CollisionTrigger = GetAtomById("CollisionTrigger")?.GetStorableByID("Trigger");
CollisionTriggerOn = CollisionTrigger.GetBoolJSONParam("triggerEnabled");
CollisionTriggerOn.val=true;

But this doesn't seem to work at all. The example for the light works fine, I can turn them off and on but I have failed to do the same for anything else. I am a little confused why I can't turn things off and on more easily.
 
Upvote 0
Back
Top Bottom