- Home /
How to define wich static variable to use with enum?
Here's the deal: I need one script to use one of the static variables in another script. This how the first script looks like (ButtonThatBindsKeys.js) :
//Button activeness variables
var GotKeyDown : boolean;
var ActivatonColor : Color;
var DeActivationColor : Color;
//For the WhathToBind dropdown
enum MyEnum{
Forward,
Backwards,
Left,
Right,
Interact,
ToggleFlashlight,
Sprint
}
//Whath this script shuold bind?
var WhatToBind = MyEnum.Forward;
function OnMouseDown () {
if(BindingGuard.AlreadyActive == false){
//Makes sure other button won't be activated
BindingGuard.AlreadyActive = true;
//Makes button active
renderer.material.color = ActivatonColor;
GotKeyDown = false;
while (GotKeyDown == false) {
if(BindingGuard.e.isKey && BindingGuard.e.keyCode != KeyCode.None){
//Here is the actuall binding
Settings.WhatToBind = BindingGuard.e.keyCode;
//De-activates the button
renderer.material.color = DeActivationColor;
GotKeyDown = true;
//Makes sure that other button can be activated
BindingGuard.AlreadyActive = false;
}
//So the game dosen't freeze
yield;
}
}
else{
print("Another or this one key binding button is already active :(");
}
}
The second script only needed for this script to work properly (BindingGuard.js) :
static var AlreadyActive : boolean = false;
static var e : Event;
function OnGUI() {
e = Event.current;
}
And the third script (only part of it, the one that has the static variables) (Settings.js):
static var Forward : KeyCode = KeyCode.W;
static var Backwards : KeyCode = KeyCode.S;
static var Left : KeyCode = KeyCode.A;
static var Right : KeyCode = KeyCode.D;
static var Interact : KeyCode = KeyCode.E;
static var ToggleFlashlight : KeyCode = KeyCode.F;
static var Sprint : KeyCode = KeyCode.LeftShift;
Edit: I posted my original code here, so it would be easier to understand what I am trying to achieve
On the line...
Settings.WhatToBind = BindingGuard.e.keyCode;
I'm assu$$anonymous$$g this is abridged, and that it'd be something like Settings.Sprint = BindingGuard.e.keyCode, for example... yeah?
I've updated my answer below. I think it may be more what you're looking for. You can configure a key binding through the enumeration displayed in the component's settings now and have it correctly set the static variables in the Settings script.
Answer by pazzesco · Mar 12, 2014 at 04:45 PM
If you're looking to set a certain static variable based on an enumeration value from one of your other scripts, you can do it with a switch statement. In your script, it looks like you have the given line as a placeholder...
Settings.WhatToBind = BindingGuard.e.keyCode;
Using a switch statement, you can add in conditionals to set the correct static variable based on a given enumeration. Replace the placeholder with the following...
switch (WhatToBind) {
case MyEnum.Forward: Settings.Forward = BindingGuard.e.keyCode; break;
case MyEnum.Backwards: Settings.Backwards = BindingGuard.e.keyCode; break;
case MyEnum.Left: Settings.Left = BindingGuard.e.keyCode; break;
case MyEnum.Right: Settings.Right = BindingGuard.e.keyCode; break;
case MyEnum.Interact: Settings.Interact = BindingGuard.e.keyCode; break;
case MyEnum.ToggleFlashlight: Settings.ToggleFlashlight = BindingGuard.e.keyCode; break;
case MyEnum.Sprint: Settings.Sprint = BindingGuard.e.keyCode; break;
default: Debug.LogWarning("Unknown key binding");
}
Using this method, it's the simplest fix that I know of for this problem. There are probably more effective and efficient ways of doing this. Every time you add a new key binding, you're going to have to update this block of code. At any rate, try that and let me know how it goes.
Well, I have to access those static variables in other script, so your answer doesn't help me :(
So, in these examples the scripts are named Scrip1 and Script2, just the first ones name doesn't appear anywhere.
Which script's variable are you trying to set, and what value are you trying to set it to?
I'm trying to change Scrip2 variable x(depending what I choose in the DropDown variable). There is no value in this example(value of abcR), but in original script it would be keycode. BTW, I just found a little mistake in the first script
Your answer
Follow this Question
Related Questions
Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer
EnumStates 1 Answer
Directly accessing enum types from other scripts (javascript) 0 Answers
Convert String into KeyCode (UnityScript) 0 Answers
Need help with enum and stuff (right, left click) in JavaScript 0 Answers