4
$\begingroup$

I am creating an options menu for a simple game. I want to make a button that will adjust my lamps energy up or down.

I would like both ways, a button that adjust the lamp energy up and a way to adjust it down.

$\endgroup$
4
$\begingroup$
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
light = scene.lights['Lamp']

if cont.sensors['Mouse1'].hitObject.name == 'button.up' and cont.sensors['Lclick'].getButtonStatus(bge.events.LEFTMOUSE) == bge.logic.KX_INPUT_JUST_ACTIVATED:
    light.energy += 0.5

if cont.sensors['Mouse1'].hitObject.name == 'button.down' and cont.sensors['Lclick'].getButtonStatus(bge.events.LEFTMOUSE) == bge.logic.KX_INPUT_JUST_ACTIVATED:
    light.energy -= 0.5

Attach this script to your two buttons with these logic bricks. The two buttons must be named button.down and button.up, or you will have to change the names in the script.
logic bricks

The two buttons will increase and decrease the default Lamp energy by 0.5 per click.

$\endgroup$
4
$\begingroup$

The "I'm not a programmer!" approach.

First step: Create the button(s), create the light.

Second Step: Create an animation for the lamp where it is dim at frame 1 and bright at frame 200.

Third Step: Switch to game logic and add a property to the light named Bright. Make an action actuator with the play mode set to property set the property and the frame property of the actuator to be Bright. Do the rest of the setup as follows.

Logic setup

This basically means If the mouse is over the button (cube) and the mouse is clicked, add two to the Bright property. Play the PointAction as Bright is increased.

Forth step: (optional) Add an always sensor and a mouse visibility if your game has no way of knowing where the mouse is. Create another button with the exact same setup except make the property Bright decrease by two under the property actuator

$\endgroup$
  • 1
    $\begingroup$ I didn't even know that you could have animated lamps playing in the BGE! (jeep's headlights will be toggling on/off very soon.) $\endgroup$ – X-27 the fluffy unicorn Jul 13 '15 at 20:37
  • 2
    $\begingroup$ Learned a trick or 2 from BRSky $\endgroup$ – Scalia Jul 13 '15 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.