Godot Version
4.2.1
Question
I’m having issues with casting GodotObjects into my interface.
interface
namespace yume2kkiremake
{
public interface IInteractable
{
void Interact();
}
}
… player .cs …
private void CheckForInteractable()
{
if (rayCast.IsColliding())
{
Variant collider = rayCast.GetCollider();
GodotObject Interactable = (GodotObject)collider.Obj;
if (Interactable != null)
{
IInteractable interactable = Interactable as IInteractable;
if (interactable != null)
{
interactable.Interact();
}
else
{
GD.Print(“Collider is not interactable.”);
}
}
}
}
I was creating an interact feature for my player, but after making it work, i ran into an issue, after making another interactable, it wasn’t able to be casted. after looking everywhere I made a clone of the script that did work, yet it still failed to cast.
Interact Script of object
using Godot;
using System;
using yume2kkiremake;
public partial class Fd : StaticBody2D, IInteractable
{
public void Interact()
{
GD.Print("Scene TP Requested: ", GetMeta(“SceneName”));
}
}
…
(another but ‘Fd’ instead of ‘Teleport’)
Can someone help me with this?
I’m simply switching the script of the node between the two scripts, one succeeds in the cast (teleporter), and one doesn’t (fd).