0 votes

Script is attached to "TextureRect" node, how can I access "Texture" property?

using Godot;
using System;

public partial class SlotHandler : Node
{
    [Export]
    Texture2D tex1, tex2;

    public override void _Ready()
    {
        this.Connect("gui_input", new Callable(this, nameof(GuiInput)));
    }

    public void GuiInput(InputEvent inputEvent)
    {
        if(inputEvent is InputEventMouseButton)
        {
            if((inputEvent as InputEventMouseButton).ButtonIndex == MouseButton.Left)
            {
                if ((inputEvent as InputEventMouseButton).Pressed == true) Texture = tex2;
                if ((inputEvent as InputEventMouseButton).Pressed == false) Texture = tex1;
            }
        }
    }
}

The problem is in lines "Texture = tex1;" and "Texture = tex2;"
I tried option "this.Texture = tex1;", that doesn't work

How to access the fields of a node?

Godot version Godot 4.0.1 stable
in Engine by (58 points)

2 Answers

+1 vote
Best answer

Change public partial class SlotHandler : Node to public partial class SlotHandler : TextureRect , and now you can access Texture =

by (372 points)
selected by

Thanks for your answer, this solves the issue.

I formulated my question differently, please take a look, maybe you will have some ideas:
https://godotengine.org/qa/150246/access-to-the-properties-of-the-node-using-getnode-godot-4-c%23

0 votes
by (1,081 points)

I am using "TextureRect" node, it has the property "Texture"
https://docs.godotengine.org/en/stable/classes/class_texturerect.html#properties

I'm trying to figure out how to access this property from a script

Yes, I thought about it, but I ran into another problem.
My script is used on different nodes, respectively, they have different names.

I tried like this

TextureRect tmpTex = GetNode((String)Name) as TextureRect;
if ((inputEvent as InputEventMouseButton).Pressed == true) tmpTex.Texture = tex2;

and like this

if ((inputEvent as InputEventMouseButton).Pressed == false) (GetNode((String)Name) as TextureRect).Texture = tex1;

It causes runtime errors:

E 0:00:23:0802 Godot.NativeInterop.NativeFuncs.generated.cs:332 @ void Godot.NativeInterop.NativeFuncs.godotsharpmethodbindptrcall(IntPtr , IntPtr , System.Void** , System.Void* ): Node not found: "TextureRect3" (relative to "/root/Node3D/TextureRect3").
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1365 @ get
node()
Godot.NativeInterop.NativeFuncs.generated.cs:332 @ void Godot.NativeInterop.NativeFuncs.godotsharpmethodbindptrcall(IntPtr , IntPtr , System.Void** , System.Void* )
NativeCalls.cs:5886 @ Godot.GodotObject Godot.NativeCalls.godot
icall1658(IntPtr , IntPtr , Godot.NativeInterop.godotnodepath )
Node.cs:685 @ Godot.Node Godot.Node.GetNode(Godot.NodePath )
SlotHandler.cs:56 @ void SlotHandler.GuiInput(Godot.InputEvent )
SlotHandlerScriptMethods.generated.cs:29 @ Boolean SlotHandler.InvokeGodotClassMethod(Godot.NativeInterop.godotstringname& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godotvariant& )
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godotbool Godot.Bridge.CSharpInstanceBridge.Call(IntPtr , Godot.NativeInterop.godotstringname* , Godot.NativeInterop.godotvariant** , Int32 , Godot.NativeInterop.godotvariantcallerror* , Godot.NativeInterop.godotvariant* )

E 0:00:23:0809 SlotHandler.cs:57 @ void SlotHandler.GuiInput(Godot.InputEvent ): System.NullReferenceException: Object reference not set to an instance of an object.
<C# Error> System.NullReferenceException
<C# Source> SlotHandler.cs:57 @ void SlotHandler.GuiInput(Godot.InputEvent )
SlotHandler.cs:57 @ void SlotHandler.GuiInput(Godot.InputEvent )
SlotHandlerScriptMethods.generated.cs:29 @ Boolean SlotHandler.InvokeGodotClassMethod(Godot.NativeInterop.godotstringname& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godotvariant& )
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godotbool Godot.Bridge.CSharpInstanceBridge.Call(IntPtr , Godot.NativeInterop.godotstringname* , Godot.NativeInterop.godotvariant** , Int32 , Godot.NativeInterop.godotvariantcallerror* , Godot.NativeInterop.godotvariant* )

I checked what the GetNode() function returns.
Added two lines to the _Ready() method

var getNodeResult = GetNode(GetPath());
GD.Print(getNodeResult.GetType());

GD.Print outputs the class name of my script "SlotHandler"

But how to access the node itself (and therefore its fields) and not the script attached to it?

var slotHandler = GetNode<SlotHandler>("SlotHandler"); or
var slotHandler = (SlotHandler)GetNode("SlotHandler"); or
var slotHandler = GetNode("SlotHandler") as SlotHandler;

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.