Save a colour and change that colour in the game.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

I am creating a quiz game. The answers are button which are in random colours . When a answer is choosen the selected button should change it’s colour to white. If the answer is correct the white colour remains but if the answer is wrong the button should return to it’s first colour before selecting . Can anyone tell me how to do it ?

:bust_in_silhouette: Reply From: Zylann

You could put the normal color of the button in a variable, this way you will be able to restore it if needed.

Example code (adapt to your needs):

extends Button

var _normal_color : Color

# Call this after you assigned the color to your button,
# assuming you used `modulate`
func remember_color():
	_normal_color = modulate

# Call this when you need the button to become white
func make_white():
	modulate = Color(1, 1, 1)

# Call this when the answer is wrong
func restore_color():
	modulate = _normal_color

Thanks for your help. But here the buttons are given random colours not a specific colour.I randomised the group of colours.

THE HELIX | 2019-12-01 18:02

In any case you HAVE to remember the color the button had if you want to restore it, and you would do that just after you randomise it.

modulate = *random color*
_normal_color = modulate

If you actually want the “restored” color to be random, then you would not need a variable and just randomize it again… it could become a different color though.

Zylann | 2019-12-01 18:35

In the game there is a set of five colours. When running the game the button chooses a random colour from the five colours. When I press the button it’s colour should turn to white. If the answer is wrong the button should return to the colour it had first.
I need to create a function to save the first colour before turning to white and if the answer is wrong change the white to the saved colour.
Please can u help me as I am new to coding ?

THE HELIX | 2019-12-01 19:16

Then store the color the button had before you make it white. When you want to revert it back to original, set modulate using the color you stored in the variable.

Zylann | 2019-12-01 19:18

Can u tell me how to store the colour ?

THE HELIX | 2019-12-01 19:22

I wrote a code example in my original answer, and another in a comment above. Use a script with a member variable.

Zylann | 2019-12-01 19:24

I didn’t understand.can u please show me how to save it ?

THE HELIX | 2019-12-01 19:28

I don’t know how else I could show that to you… the code I provided is litterally explaining what you need to do. I don’t know how you made the rest of your game so doing it in your project would obviously be a little different, but I thought you could figure out from my example how to do it in your case.
At this point you could share your project and I can modify it to show you how it can be done specifically in your project… unless you have no project at all and you are asking how to do the WHOLE thing?

Zylann | 2019-12-01 19:34

I put your code in the game.Now on pressing the buttons they change to white but if it is a wrong answer they are not changing to original color they remain in white color only.
In the game I have put all the correct answers in an autoload script and access them using the script attached to buttons.

THE HELIX | 2019-12-02 15:05

I still don’t understand how exactly your project is made, there are details I don’t get. For example, you say when a button is pressed it changes to white, but when it’s a wrong answer it doesn’t change back to its color… but when are they supposed to go change back? Why would they become white? Is there a timer? Is it only white when the button is being held?

It also seems you haven’t much knowledge of scripting in Godot. Did you follow the tutorials? Step by step — Godot Engine (3.1) documentation in English

Zylann | 2019-12-02 18:56

Sorry my project is not a quiz game but a game similar to the imageenter image description here

In the project there are circles of different colors, randomly arranged.I need to match same colored circles. When I press same colored circles they change to white and are cleared. But when I select different colors they change to white ; since they are different colors I need to change their color from white to their original color [Here white color is used for selecting].
How to change the circles white color to their original color if the selected circles are of different color ?

Here is the project Game

THE HELIX | 2019-12-03 15:03