#Help changing Sprite2D's texture with code

27 messages · Page 1 of 1 (latest)

waxen salmon
#

Assuming you have a reference to the sprite, you can do something like this.

var energy : int :
    set(value):
        energy = value
        if energy >= 10:
            sprite.modulate = Color.WHITE
        else:
            sprite.modulate = Color.BLACK

Color.WHITE is the constant for the default white setting, which just multiplies the pixel color of each pixel's rgba values by 1.0, and Color.BLACK multiplies them all by 0.0 (leaving the alpha at 1.0). So, WHITE will be the normal image, and BLACK will be that image, but blacked out entirely. If this is in a script on the direct parent of your sprite, you could get the reference by doing $Sprite2D (or replace "Sprite2D" with whatever you renamed it to). If not, you'll have to find another way to do it. One possibility is to have a function in a script on your sprite that takes a energy value and sets the sprite's modulate based on it, then have this set method of your energy emit a signal (which you would declare with a parameter like this signal energy_changed(energy), and emit like this energy_changed.emit(energy) from inside the setter for energy (as shown above). Then you can take the if block out of this setter, and put it into that function on your Sprite script, and connect the signal to the function. You can make such a connection in the editor from the Node tab (usually to the right of the inspector tab in that panel), under signals. This works if you have these things each already present in your scene tree. If not, you'll have to figure out how to get this connected in code. One possibility is to add whatever node has the script with energy in it to a group with a group name. Call it whatever you want, but as long as it's the only node in the group with the name you give it, you can get a reference to it using var energy_node = get_tree().get_first_node_in_group("energy group name"), where I just made up names for the example.

#

Another benefit of this is let's say this sprite represents an ability that becomes usable when you have enough energy for it. You can have a variable in its script (let's call it energy_cost), and it's if condition can use that as the threshold for changing the blacked-out state like so:

var energy_cost : int = 10

if energy >= energy_cost:
    sprite.modulate = Color.WHITE
else:
    sprite.modulate = Color.BLACK

And if you want to be able to manually adjust this value in the inspector, you can change the variable declaration to @export var energy_cost : int = 10 and it will allow you to change it in the inspector. These are just a few hints of things you can do, but of course there are many more ways to accomplish what you are trying to do, as well. Keep exploring and don't be afraid to have a look at the documentation. It's among the best I've ever seen for a game engine.

fast rock
waxen salmon
fast rock
waxen salmon
fast rock
#

not that i know of no, the code is the same as it is shown in the image above but $Sprite2D. is removed

waxen salmon
fast rock
#

where the error message and arorow are in the image above

waxen salmon
#

So, did you stop running and restart? Or did you edit while running?

#

Also, if you clicked where that yellow arrow is, it may have added a breakpoint there. You can remove it by clicking it again, I believe.

fast rock
#

all those break point errors have gone but now its just not changing toi WHITE when its over 10

waxen salmon
#

_ready() is called when the node is first added to the scene tree. That's it. Process happens each Engine tick (which can vary, but is usually around once every 1/60th of a second by default).

fast rock
#

oh so what func name shoudl i use... _process(delta)????

waxen salmon
#

Well, that will work, yes.

#

That is a built-in function, and when you use it in a script, you are overriding it with your own code. Think of it like this: _process(delta) runs whether you use it or not, but you are adding to it when you use it. the 'delta' part is just the time since the last frame (and that's so you can smoothly calculate things based on framerate).

#

The actual process function is doing lots of other stuff under the hood for us. This one we override is effectively just called additionally for us to do things with.

#

The reason I showed you the first way is so that you would not have to use any of these functions at all for this, btw.

#
extends Sprite2D

var energy : int :
    set(value):
        energy = value
        if energy >= 10:
            modulate = Color.WHITE
        else:
            modulate = Color.BLACK

This will handle things all by itself.

#

It only does the check when energy actually changes, so you aren't having that extra stuff happening each and every frame.

fast rock
waxen salmon
#

When you declare a variable like this (The way I have it in my example above), you are overriding its setter function with your own. That means, anything that changes the variable's value also calls this function to do it. Which in turn means it executes any code you put in the function. You can use these to do all sorts of neat things, but one use is to have something change when certain conditions are met by the variable's value changing.

#

That's why this is all you need for what you're trying to do.

#

Also, don't try copy/paste of this, because it replaces tabs with spaces and the editor will give you errors. You're better off just typing it out.

fast rock
# waxen salmon I don't even know what you are asking here... For what you're trying to do, you...

I understand that that script is beter but the reason I did the is unlocked varible is because then I can call the varible in other scripts to change other things when its unlocked. I also was trying to do my script because, though worse than your i understand it and im kinda new to godot. I put your script into the code, replacing spaces with tabs and then ran it, there were no errors but it didnt change at all, othing happened.