C# Sprite and Textures, Change font color and create a RectangleShape2d (Extents) by code.

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

Hi.
I have some questions in C#.
1) From Sprite, cant get a texture. Edit: From a current Sprite class, Get from an external List/Array Sprites get randomly the texture, and assign it to the current Sprite class.

// I did not write all the code, because is annoying writing code in this page. Just skipped the random generation code.
public class RandomSprite : Sprite
{
 [Export] public Array<Sprite> sprites = new Array<Sprite>( ); // This doesnt works
 [Export] public Sprite mysprite; // This doesnt work
 [Export] public Texture myTexture; // This works.
 
 public override void _Ready()
 {
     Texture = sprites[0].Texture; // This does not work. // Error: Casting. Even if is not null
     Texture = mysprite.Texture; // This does not work. // Error: Casting. Even if is not null
     Texture = myTexture; // Why this works?
 }
// code...

}

2) Change font color from a button. The only way is by code or adding RichTectLabel, Label, and change the color?

 myButton.Set("custom_colors/font_color", Color.Color8(1, 1, 1, 255));

I think, there is no solution for this.
3) I need to create, inside of CollisionShape2D a Shape (RectangleShape2D), BUT, the Extents will be by code.
The thing is, I am translating one tutorial from Unity to Godot (Making a procedural level) and Creating a Room. Adding a Sprite(Tile) and random will get from a List, in this case an Array a sprite and show it.
BUT, also will get a collider (The walls or floor), BUT, the collision shape needs to fit in the sprite.
So, I dont know how to create a RectangleShape2D with its Extents in the CollisionShape2D.Shape by code.

Thank you.

Hi.
Can you please give me more Information, what you are even trying to do?

juppi | 2022-07-24 15:55

Trying to implement this in Godot: https://www.youtube.com/watch?v=hk6cUanSfXQ&t=6s

  1. Sprite generator randomly.
  • Place each sprite generator around the room.
  • In the Sprite, get a List < Sprite > , in this case an Array < Sprite >.
  • Random, get from the Array a sprite.texture and place it

[Export] public Array<Texture> sprites = new Array<Texture>( );
Texture = sprites[randomnumber].Texture; // This gets an error of Casting.

Cristian | 2022-07-24 16:59

:bust_in_silhouette: Reply From: Christoffer

if you want to use the same sprite texture as another one you have to do something like this

From a Sprite class, cant assign a texture from an external Sprite.

public class RandomSprite : Sprite // Sprite class
{
 [Export] public Array<Sprite> sprites = new Array<Sprite>( );  // This Doesnt work

 public override void _Ready()
 {
 Texture = sprites[0].Texture; // This does not work. // Error: Casting. Even if is not null
 }
// code...
}

Cristian | 2022-07-25 01:58

Sorry, my bad, I’ve typed wrong.

  [Export] public Array<Sprite> sprites = new Array<Sprite>( );  // This Doesnt work

Cristian | 2022-07-25 02:05

:bust_in_silhouette: Reply From: Christoffer

Texture = sprites[0]; ← That one worked for me? you don’t need to add “.Texture” to an already texture.
You can get lists or arrays of textures this way.

using Godot;
using System.Collections.Generic; //Dont forget this 
using Godot.Collections;

[Export] private Texture[] myTextures;
[Export] private Array<Texture> myTextureArray = new Array<Texture>();
[Export] private List<Texture> myTextureList = new List<Texture>();

But this Texture = mysprite.Texture; Sprite in godot are nodes, inherited from Node2D so it’s not an resource and cannot be used as one :slight_smile:

and you can change the font manually or by code

From Textures, I dont have any problem.
From Sprite, cant get the Texture and assign it to another one.

Cristian | 2022-07-25 01:59

Sorry, my bad, I’ve typed wrong.

 [Export] public Array<Sprite> sprites = new Array<Sprite>( );  // This Doesnt work

Cristian | 2022-07-25 02:06