Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thomas-Hawk · Jun 17, 2018 at 06:14 PM · scripting problemcoroutinebooleanreturnchain

I have chains of connectable objects which reference eachother. Need them to pass a simple boolean to eachother! How?

alt text

So, basically I have a system where my character can place wire. A little cube gets instantiated, and a new one starts following the mouse, with a linerenderer running the visual "wire". At this moment the cubes get references to eachother. Then the second cube can get placed down, to either run another length of wire or to end the connection at a powerable object, right now I have a "lamp".

We connect a lever to the first cube which triggers a function, "If the cubes we are connected to are not the same state as I am, make them the same state. and then trigger this same function on that cube's script"

This was working well enough logic-wise but once I got further into the system, I realized doing all of this in frame-based loops was inefficient and killed my FPS.

So I started trying to convert my functions into coroutines, but I can't seem to figure it out! I've gotten debug logs to show they are happening but the resulting behavior isn't reflecting it.

I left in some commented code to show my trial-and-errors here.

 public Coroutine Flip(){
         //state = !state;
         //StartCoroutine(PushState);//PushState ();
         print("Flipping");
         StartCoroutine ("PushState");
         return null;    
         //yield return null;
 
     }
 
     //public IEnumerator Flip() {
         //for(;;) {
     //        print ("Pushing state");
     //        PushState();
     //        yield return new WaitForSeconds(.05f);
     //    }
     //}
 
     
     WireNode node;
     public IEnumerator PushState(){
 
 
 
         print ("Pushing");
 
 
 
 
         foreach (Behaviour conn in ports) {
             if (conn != null) {
                 if (conn.name.Contains ("WireNode")) {
                     node = conn as WireNode;
                     if (node.state != state) {
                         node.state = state;
                         node.PushState ();
                     }
 
 
                     foreach (LineRenderer cable in node.wirelines) {
                         if (node.state == true)
                             cable.materials [0].color = onColor;
                         if (node.state == false)
                             cable.materials [0].color = offColor;
 
                     }
                 }
 
                 
                 if (conn.name.Contains ("Powerable")) {
                     Powerable p = conn as Powerable;
                     p.powered = state;
                     p.StartRefresh ();
 
 
                 }
 
             } else {
                 //yield return null;
             }
         }
         //}
 
         yield return null;
 
 
     }` 
 

TL;DR: I need each wire to tell its own list of other connected wires what to do / what state they are in (boolean).

I plan to allow for a huge number of wires taking place at once, so I am hoping to condense the behaviour into one that is not framerate dependant, will not bog down the framerate

I only need this message / state to be changed once at a time. I.e., all of the wires are simply in one state, and not doing anything, until they are told to refresh, which is one action that pushes their state to other wires.

Like Redstone.

scree.png (478.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PanzerPotato · Jun 17, 2018 at 06:21 PM

Take a look at this. You can try using yourOtherObject.SendMessage () to tell the next to do what ever it is you need to do.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thomas-Hawk · Jun 17, 2018 at 06:33 PM 0
Share

I actually am using Send$$anonymous$$essage as my "powerable" script's way of making machines do things.

You're saying, have the lever Send$$anonymous$$essage the wire to Send$$anonymous$$essage the next wire, etc etc?

I like the idea, but my question / concern is, Wouldn't every single message still happen in a single frame?

avatar image PanzerPotato Thomas-Hawk · Jun 17, 2018 at 08:37 PM 0
Share

So just to be clear, are you trying to have one segment of wire power on at a time?

avatar image Thomas-Hawk PanzerPotato · Jun 18, 2018 at 12:14 AM 0
Share

No, the player can place these nodes freely. Each node has a script with a list of 6 "slots" (behaviours) which is how each node knows what other nodes, or machines, it is connected to. So when a node is changed (say by a lever), it "pushes" its state to the nodes it is attached to, and tells them to do the same. I prevent a loop by only changing states which are not already the same.

If a node is attached directly to an activated source of power, the nodes state is forced to stay on, and continue its update chain to push its state to its connected wires.

I changed, in all scripts with a "refresh" or "push state" function, the "Invokes" with "Send$$anonymous$$essage". I believe I did get a performance boost, but I still get the feeling I'm misunderstanding the way to apply co routines here - my goal is, I want the computer to handle as many wire updates as it can without affecting the framerate. Framerate is priority primero.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[HELP] How to make camera smooth change position while follow object? 1 Answer

How do couroutines handle If Statements within a for loop? 1 Answer

Rotating cube steadily around world axis not working properly 0 Answers

Can you run a coroutine more often than Update? 1 Answer

1 out of 4 objects with the same script that reference the same transform list returning null? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges