Is it possible to use Region positions to make a moving looping background

Godot Version

Godot 4.2.2

Question

Hi! I’m trying to use Sprite2D’s region in a script so i could make a looping background, idk if it is even possible but got curious if it would work that way :'D

here’s something i tried:

extends Sprite2D

func _process(delta):
	$".".region_rect += Rect2(1,1,0,0)

it gives me this error "Invalid operands ‘Rect2’ and ‘Rect2’ in operator ‘+’.

if this isn’t the way to go in making a loopable background with sprite2d then i would appreciate some advice with it, since i’m still in the big learning process with GDScript and Godot tbw :slight_smile:

edit: with loopable moving background im trying to go with the yume nikki style / other RPGMaker games

1 Like

I use shaders to apply loopable backgrounds when I need, easy as set the ShaderMaterial on the Sprite, TextureRect, ColorRect and I can change the parameters without any effort:

// https://lucassaturn.co.uk/dev/godot/texture_scroll.html 
shader_type canvas_item; //set type of object it applies to

    uniform vec2 direction; //x and y direction
    uniform float speed; //speed value
    varying vec4 modulate; //grab colours after modulate/self-modulate
    
    void vertex(){ //applies to every single vertex
        modulate = COLOR;
    }
    
    void fragment(){ //applies to every single pixel
        vec2 move = direction * TIME * speed; //create offset
        COLOR = texture(TEXTURE, UV+move) * modulate; //set colour
    }
1 Like

Yes, set the CanvasItem.texture_repeat to Enabled or Mirror depending on the effect you want to get. Enable Sprite2D.region_enabled and set the Sprite2D.region_rect size to the size you want to cover. Then you can change the Rect2.position property of that region_rect to achieve texture scrolling.

extends Sprite2D

func _process(delta):
	region_rect.position += Vector2(1, 1)
1 Like

thx, i’ll def check this one out :smiley: since i think learning shaders would be pretty important tbw

thx this is what i was looking for but my dumbass wouldnt figure it out :'D