0 votes

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.

Godot version 3.4.4
in Engine by (37 points)
edited by

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

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.

2 Answers

+1 vote

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

by (172 points)

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...
}

Sorry, my bad, I've typed wrong.

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

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 :)

and you can change the font manually or by code

enter image description here

by (172 points)
edited by

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

Sorry, my bad, I've typed wrong.

 [Export] public Array<Sprite> sprites = new Array<Sprite>( );  // This Doesnt work
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.