x


GUI Buttons to change values.

Hi. So I want to make clickalbe buttons that changes variables. I have following code:

var walkSpeed = PlatformerControllerMovement.walkSpeed;
var runSpeed = PlatformerControllerMovement.runSpeed;
var inAirControlAcceleration = PlatformerControllerMovement.runSpeed;
var gravity = PlatformerControllerMovement.gravity;
var maxFallSpeed = PlatformerControllerMovement.maxFallSpeed;
var speedSmooth = PlatformerControllerMovement.speedSmoothing;
var rotateSmooth = PlatformerControllerMovement.rotationSmoothing;

var height = PlatformerControllerJumping.height;
var extraHeight = PlatformerControllerJumping.extraHeight;
var doubleJumpHeight = PlatformerControllerJumping.doubleJumpHeight;

function OnGUI () {
    chooseButton(0,0,"walkSpeed", walkSpeed);
    chooseButton(1,0,"runSpeed", runSpeed);
    chooseButton(2,0,"inAirCtrlAcc", inAirControlAcceleration);
    chooseButton(3,0,"gravity", gravity);
    chooseButton(4,0,"maxFallSpeed", maxFallSpeed);
    chooseButton(5,0,"speedSmooth", speedSmooth);
    chooseButton(6,0,"rotateSmooth", rotateSmooth);

    chooseButton(0,1,"hate", height);
    chooseButton(1,1,"extraHate", extraHeight);
    chooseButton(2,1,"doubleHate", doubleJumpHeight);
}



function chooseButton (posX, posY, variableName, variableObj){

    GUI.Box(Rect (10+(posX*95),10+(posY*85),90,55), variableName);
    if (GUI.RepeatButton (Rect (25+(posX*95), 33+(posY*85), 25, 25), "-")) {
        variableObj -= 0.1;
    }   
    if (GUI.RepeatButton (Rect (60+(posX*95), 33+(posY*85), 25, 25), "+")) {
        variableObj += 0.1;
    }   

    GUI.Box(Rect (10+(posX*95),68+(posY*85),90,22), variableObj.ToString());
}

...and when i click button "-" or "+" nothing happens. I noticed, then if i will extend code from a function and type variables by hand - everything works! There is a webdemo, if somebody want to see it in action: click here.

more ▼

asked Apr 12 '10 at 10:48 PM

Rafal Sankowski gravatar image

Rafal Sankowski
23 3 3 11

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

There is a hackish way of doing this in Javascript if you really want. Since classes are always passed by reference and not by value, you can make a class for your variables (that would normally be passed by value) that you want to be directly affected by functions. The class can work basically the same way as a normal passed-by-value variable, but takes more work to set up and use.

Here's an example that makes a class called RefFloat, that is basically a float, except you can use it like a reference variable. The DoubleButton function will make a button that doubles the value of any RefFloats that are passed in.

class RefFloat {
    var value : float;
    function RefFloat (a : float) {
        value = a;
    }
}

var var1 = new RefFloat(.33);
var var2 = new RefFloat(2.0);

function OnGUI () {
    DoubleButton("Variable 1", var1);
    DoubleButton("Variable 2", var2);
}

function DoubleButton (buttonLabel : String, variableName : RefFloat) {
    if (GUILayout.Button(buttonLabel + ": " + variableName.value.ToString())) {
        variableName.value *= 2;
    }
}
more ▼

answered Apr 12 '10 at 11:28 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Woaw! You're the best! Thank you!

Apr 13 '10 at 12:56 AM Rafal Sankowski
(comments are locked)
10|3000 characters needed characters left

Variables like ints and floats (which are passed by value) in functions are local to that function, so when you increase/decrease variableName, it changes it the function only, and the variable you passed in is not affected at all.

To get around that, you can affect the actual variables you're passing in by using reference variables instead of standard local variables. However, while you can use functions that have reference variables in Javascript, you can't make them. For that you need to use C# instead. It's very possible to make that function as a separate C# script, and keep the rest of the script in Javascript, if you're more comfortable with that.

more ▼

answered Apr 12 '10 at 11:03 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Thank you for that quickly feedback!

If I can't do it by functions (because I don't wanna use C#) - is there any other way? I don't want to copy that much of code.

Apr 12 '10 at 11:11 PM Rafal Sankowski

@Rafal Sankowski: Well, actually there is another way (see my other answer), but frankly it's easier to use C# for this sort of thing. Remember you'd only have to use it for that one function and nothing else.

Apr 12 '10 at 11:30 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3694
x789
x379
x58

asked: Apr 12 '10 at 10:48 PM

Seen: 1364 times

Last Updated: Apr 12 '10 at 10:48 PM