Changing textures from a script

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

Hi,

I’m making a small game where I need to display numbers on game objects. I initially need to be able to change these numbers from some sort of generating script. There will be a lot of numbers, so doing it manually is not an option. The numbers do not need to be changed during gameplay, just hidden away at times.

If anyone could point me in the right direction I’d be really thankful.

Thanks.

What kind of game is that? 2D or 3D? Can those numbers just show up above the objects as labels instead of on the objects as textures?

johnygames | 2019-12-30 18:10

It’s a clone of the old DS game picross 3d: https://youtu.be/argEXd8yryY?t=216 It’s a 3D game. I want the numbers to show up on the objects like in the video linked, so I’m guessing that they have to be textures.

Thanks for the answer and sorry for the lack of information, I’m kind of new to this.

isakalmq | 2019-12-30 19:35

:bust_in_silhouette: Reply From: johnygames

I think the fact that you are dealing with cubes makes your life a bit easier. I have come up with a way of creating picross cubes that change textures, but it might not suit you.

So, I’ve thought of creating a cube using plane primitives. You basically arrange six planes in such a way that you create a nice little cube.

Those planes should be held together by being attached to a spatial node in their own scene. Now starts the fun part: Each plane of your cube is essentially a separate object. This means that it has got its own material.

Create new materials an assign a different texture to each of them. The idea is to create 10 different materials, one for every number form 0 to 9. Create the textures with the numbers on them in an external image processing program. Load the materials in memory and assign them to the cube’s faces like this:

extends Spatial

onready var mat0 = preload("res://Materials/picross_mat_0.tres")
...
onready var mat9 = preload("res://Materials/picross_mat_9.tres")

onready var face1 = $face1
onready var face2 = $face2
onready var face3 = $face3
onready var face4 = $face4
onready var face5 = $face5
onready var face6 = $face6

func _process(delta):
	if Input.is_action_just_released("ui_left"):
		face5.set_surface_material(0, mat6)

To summarize: Create a cube using planes. Make a new material for every number. Manipulate the planes’ material programmatically.

There are probably other ways of doing this, like setting up a complex shader that offsets the UVs thus altering the cube’s texture. I kinda tried something like that, but it’s too complicated. If my solution was helpful, please upvote and mark this answer as best.