sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Toggle script (working)

    Scheduled Pinned Locked Moved SketchyPhysics
    18 Posts 8 Posters 2.3k Views 8 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      CPhillips
      last edited by

      @wacov said:

      Maybe you could build the script into a function? Like, (onButton("a",1)toggleVar("foo")). Or something like that, just a way to detect presses and other changes as an event. You could even have stuff like 'onVar("foo",1)' , 'onJoy("lefty",0.8)' , and 'onKey("space",1)'.

      Thats interesting. I might be able to do something like an onKeyDown event. Because it is onKeyDown you only get the event when it is pressed not each frame.

      But I dont understand exactly what the other examples you gave. What would onVar('foo',1) do?

      1 Reply Last reply Reply Quote 0
      • W Offline
        Wacov
        last edited by

        Ok cool... with the other examples, it would do something when the var, joystick or whatever reaches the stated position, but would treat it like a key down; so it wouldn't repeat until the input has changed and gone back.

        http://sketchup.google.com/3dwarehouse/cldetails?mid=3096a836877fb9af6cd8ad826e9017b8&prevstart=0

        1 Reply Last reply Reply Quote 0
        • B Offline
          BTM
          last edited by

          ...I've just started to learn if functions about 45 minutes ago, so I'm not that great at it, but I managed to make a 2 key switch, like a light-switch works 😳

          if frame==1 then setVar("1",0);elsif key("up")==1 then getSetVar("1",1);elsif key("down")==getVar("1") then getSetVar("1",0);end;
          
          
          1 Reply Last reply Reply Quote 0
          • C Offline
            CPhillips
            last edited by

            Not bad for 45 minutes worth of experience!

            1 Reply Last reply Reply Quote 0
            • S Offline
              Sgt.A.Johnson
              last edited by

              surely could you not have a controller command: togglekey('space') or something and use that as the command in future versions. it would make it alot easier for newbies and pros alike i reckon.

              1 Reply Last reply Reply Quote 0
              • C Offline
                CPhillips
                last edited by

                @sgt.a.johnson said:

                surely could you not have a controller command: togglekey('space') or something and use that as the command in future versions. it would make it alot easier for newbies and pros alike i reckon.

                As soon as I can figure out a simple way to handle all the use cases I will.

                1 Reply Last reply Reply Quote 0
                • F Offline
                  FredIIVX
                  last edited by

                  Couldn't you just put something like this in the OnTouch/OnTick box?

                  if getVar('On')==0;
                  setVar('On',1);
                  elsif getVar('On')==1;
                  setVar('On',0);
                  end;
                  
                  1 Reply Last reply Reply Quote 0
                  • W Offline
                    Wacov
                    last edited by

                    @frediivx said:

                    Couldn't you just put something like this in the OnTouch/OnTick box?

                    if getVar('On')==0;
                    > setVar('On',1);
                    > elsif getVar('On')==1;
                    > setVar('On',0);
                    > end;
                    

                    In this case the elsif isn't necessary, and this is basically how my script ends. The problem is that in the onTick box, this would toggle every frame (Or every rate frame), and in onTouch every frame while a collision is occuring. The complexity of my script is just the way I've got SP to only toggle when the button is pressed, rather than every frame it's held down.

                    If my suggestion was implemented, it would be as simple as:

                    (onJoyButton("a",1); toggleVar("foo")) 
                    

                    http://sketchup.google.com/3dwarehouse/cldetails?mid=3096a836877fb9af6cd8ad826e9017b8&prevstart=0

                    1 Reply Last reply Reply Quote 0
                    • C Offline
                      CPhillips
                      last edited by

                      I think the way it will work is there will be a onJoybutton event like onTouch is currently.

                      In the script field you will be able to tell what key was pressed. Something like this.

                      if(key=='a')
                      toggleVar("toggle")
                      end  
                      

                      BTW. Here is the shortest script I could think of to do a toggle.

                      
                      setVar("toggle",getVar("toggle")==0? 1;0)
                      

                      That is pretty dense so i will try to explain.

                      
                      setVar("toggle",
                      

                      This is what we are going to do. Set the value of "toggle".

                      ...getVar("toggle")==0?
                      

                      Get the value of "toggle" and if it is 0

                       ..."toggle")==0? 1;0)
                      

                      Then 1 else 0.

                      so the result of the second part

                      getVar("toggle")==0? 1;0)
                      

                      Gets stored in "toggle" by the first setVar part:

                      setVar("toggle",getVar("toggle")==0? 1;0)
                      

                      The "value" of setVar is always the value you set it to. If you set a joint controller to:

                      setVar("foo",1.0)
                      

                      The joint will move to 1.0

                      That is so you dont have to do:

                      
                      setVar("foo",1.0);getVar("foo")
                      
                      1 Reply Last reply Reply Quote 0
                      • W Offline
                        Wacov
                        last edited by

                        OOOPPS... I just realised there was an unnecessary couple of lines in there 😳

                        I've removed them from the original and new posts... sorry guys...

                        http://sketchup.google.com/3dwarehouse/cldetails?mid=3096a836877fb9af6cd8ad826e9017b8&prevstart=0

                        1 Reply Last reply Reply Quote 0
                        • W Offline
                          Wacov
                          last edited by

                          Thanks Chris.

                          Everyone, here's a slight addition to the script; it's for weapon switching, or for cycling through values. The output changes from 0, to 1, 2, then 3, then cycles back to 0, every time the y button is pressed. This is really for placement in the onTick field, but you can use it as a controller. I know for a fact that it doesn't have to be this complex, but this was the easiest modification to make; I'll refine it soon, make it easier to change the number of values to cycle.

                          if frame==1;
                          setVar("repeat",0);
                          setVar("weapon",0);
                          end;
                          
                          if joybutton("y")==0;
                          setVar("repeat",1);
                          getVar("weapon");
                          elsif getVar("repeat")==0;
                          getVar("weapon");
                          elsif getVar("weapon")==0;
                          setVar("repeat",0);
                          getSetVar("weapon",1);
                          elsif getVar("weapon")==1;
                          setVar("repeat",0);
                          getSetVar("weapon",2);
                          elsif getVar("weapon")==2;
                          setVar("repeat",0);
                          getSetVar("weapon",3);
                          elsif getVar("weapon")==3;
                          setVar("repeat",0);
                          getSetVar("weapon",0);
                          end
                          

                          Ok, I've released a partially finished demo of this; see http://sketchup.google.com/3dwarehouse/details?mid=2089b6db29e339096cd8ad826e9017b8

                          http://sketchup.google.com/3dwarehouse/cldetails?mid=3096a836877fb9af6cd8ad826e9017b8&prevstart=0

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement