Toggle script (working)
-
Finally got around to getting my toggle script working with the global variables; here it is, in all its absurdly long glory!
if frame==1;setVar("repeat",0);setVar("toggle",0);end;if joybutton("a")==0;setVar("repeat",1);getVar("toggle");elsif getVar("repeat")==0;getVar("toggle");elsif getVar("toggle")==0;setVar("repeat",0);getSetVar("toggle",1);else;setVar("repeat",0);getSetVar("toggle",0);end;
And the more readable version:
if frame==1; setVar("repeat",0); setVar("toggle",0); end; if joybutton("a")==0; setVar("repeat",1); getVar("toggle"); elsif getVar("repeat")==0; getVar("toggle"); elsif getVar("toggle")==0; setVar("repeat",0); getSetVar("toggle",1); else; setVar("repeat",0); getSetVar("toggle",0); end;
There's probably an easier way, but this could be useful for stuff like landing gear, doors, and switches of any kind.
-
OMG wacov!!!! that's awesome! how do you know so much about scripts?
-
@wacov said:
Finally got around to getting my toggle script working with the global variables; here it is, in all its absurdly long glory!
if frame==1;setVar("repeat",0);setVar("toggle",0);end;if joybutton("a")==0;setVar("repeat",1);getVar("toggle");elsif getVar("repeat")==0;getVar("toggle");elsif joybutton("a")==0;getVar("toggle");elsif getVar("toggle")==0;setVar("repeat",0);getSetVar("toggle",1);else;setVar("repeat",0);getSetVar("toggle",0);end;
And the more readable version:
if frame==1; > > setVar("repeat",0); > setVar("toggle",0); > > end; > > if joybutton("a")==0; > > setVar("repeat",1); > getVar("toggle"); > > > elsif getVar("repeat")==0; > > getVar("toggle"); > > > elsif joybutton("a")==0; > > getVar("toggle"); > > > elsif getVar("toggle")==0; > > setVar("repeat",0); > getSetVar("toggle",1); > > else; > > setVar("repeat",0); > getSetVar("toggle",0); > > end;
There's probably an easier way, but this could be useful for stuff like landing gear, doors, and switches of any kind.
MAN that's is unbelievable thanks wacov
-
I'm no expert, this is just a bunch of if functions! I basically did this a while back, it only works now with the proper variable system, now that they are saved between frames.
-
Thats cool.
I'll figure out a way to make it easier with a future version. Making a toggleVar("foo") would be easy. But the complicated part (as you discovered) is avoiding key repeats. Not sure how to handle that yet...
-
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)'.
-
Thanks a lot for this!
-
@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?
-
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.
-
...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;
-
Not bad for 45 minutes worth of experience!
-
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.
-
@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.
-
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;
-
@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"))
-
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")
-
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...
-
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
Advertisement