Scaling custom resources with script resources?

Godot Version

4.2

Question

Hello,
I’m trying to use resources in order to be able to use template scenes that can be turned into various instances with the simple drop of a custom resource.

Where I’m not too sure what to do is when I want to have certain entities have, for example different Effects.For context, I have an Effect class, that gets inherited by various variants of it.

Ideally, I’d like to have to ability to define that effect as an exported variable, but when I do that, it seems I’m dropping the actual script resource (I’m using C#), which returns a runtime error that CSharpSript cannot be cast into my type Effect (which makes sense).

Ultimately, the goal here would be to be able to get that script resource into a runtime instance, but I’m not sure how to go about that, or if I’m over complicated this way too much.

Thanks!

Okay I think I’ve resolved this. In case this is useful to someone in the future what I did is in my resource, I’m Exporting a CSharpScript. When needed I can call new on that.

public partial class MyResource : Resource
{
    [Export] public string Id { get; set; }
    [Export] public string Name { get; set; }
    [Export] public string Description { get; set; }
    [Export] public CSharpScript EffectScript { get; set; }
}

Instantiation

public void PlayEffect()
    { 
        Effect effect = (Effect) EntityResource.EffectScript.New();
        // At this point you have an instance of Effect and can call methods on it.
    }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.