Godot Version
4.2.2
Question
I want to know when I am clicking on my background and not other things, I realized that if I set my backgrounds alpha value to 244 and then check the alpha value where I clicked I can. Is there a way to do this?
4.2.2
I want to know when I am clicking on my background and not other things, I realized that if I set my backgrounds alpha value to 244 and then check the alpha value where I clicked I can. Is there a way to do this?
If you have an image you can call Image.get_pixel(x,y) and it’ll return the color.
Image documentation
Is there a way to turn what the camera sees into an image so then I can get the color that way? If I just turn my background into an image then I wouldn’t be able to do what I want.
this should work:
viewport.get_texture().get_image()
Lemme add to my previous reply : get_viewport() and then get_texture().get_image()
What are you using this for? There is probably a better way to achieve what you are trying to do.
Everything is actually working with this plan all I am having trouble with is this,
func _process(_delta):
if Input.is_action_just_pressed("Click"):
$"../Background".texture = load("res://Graphics/Backgrounds/RandomColorBackGround.png")
var camera_image = get_viewport()
camera_image = camera_image.get_texture().get_image()
var pixel_color = camera_image.get_pixelv(get_viewport().get_mouse_position())
if pixel_color == Color(0.4157, 0.4549, 0.0824, 1):
print("this is the background")
print(Color(0.4157, 0.4549, 0.0824, 1))
print(pixel_color)
Even though when I run it and click they they both printout the two color values I don’t get the “this is the background” to print my guess is that the print value only prints color to 4 decimal places.
Try putting an await get_tree.process_frame() before the if statement, maybe it needs an extra frame to process it. And maybe put in in a few extra places as well like before variable declaration. The process frame function is very useful and has saved me multiple times
Nm I figured out that they are stored as floats but when you print them out they only print the first 4 decimal places i solved it like this
func _process(_delta):
if Input.is_action_just_pressed("Click"):
$"../Background".texture = load("res://Graphics/Backgrounds/RandomColorBackGround.png")
await get_tree().process_frame
var camera_image = get_viewport()
camera_image = camera_image.get_texture().get_image()
var pixel_color = camera_image.get_pixelv(get_viewport().get_mouse_position())
if is_equal_approx(pixel_color.r, 0.41568627953529):
print("this is the background")
$"../Background".texture = load("res://Graphics/Backgrounds/spacebackground.png
The only problem is that I need to use an await frame which means that my background flickers into a random color. I honestly don’t think this method will work anymore because of that. if was the alpha value changing by a little it wouldn’t really be noticeable but because turning the viewport into an image makes all alpha values into 1.
Have you tried putting the logic in the input function?
Can you explain write where you want me to put it in my code?
Sure thing:
func _input(event):
if event.action_pressed("Click"):
#Logic here
Nope after moving it there it doesn’t do anything. The problem is that for this to work the viewport needs to see the backgrounds color change, that is why the process frame is needed after the backgrounds texture is changed. Since the viewport needs to see the new color I will always see which I just don’t want to see that new color of the background which is why this most likely wont work.
Perhaps the issue is loading the texture on the spot, what if you first preload the new texture in an on ready variable above the process?
No didn’t work.
Like someone mentioned, you should explain why you want solve it this way.
for example, you can have general func _unhandled_input(event):
at root scene node.
that handle all “still unhandled inputs”
so if you click anything else than background and handle their inputs, this mean this method will not ne triggered, otherwise it mean its background.
it is described here:
Does it flash before you change the background The last time? The last idea that I have is if you put one more await right before you change the image the last time, besides that I’m lost.