#Exporting Callables

11 messages · Page 1 of 1 (latest)

fiery fern
#

Hey all,
I'm currently making my own weapon system and I'm having one hiccup that's preventing me from finishing it. I have this custom resource w/ class_name of Gun. It has a bunch of export properties I can set: fire_rate, view_model, and some animations. I also want to be able to define a "shoot method" that I can export and set in the inspector. My instinct was to use export a Callable, but I have no clear way to define it's value.

Any ideas on how to do this or any alternative approaches?

digital marsh
#

Callable isn't really a resource or an Object
you can use an @export_enum and list out the callable names - and use the export to populate your Callable property, but I don't think the inspector really has any support for exporting a list of functions of the object, nor constructing a lambda if that's the goal

regal igloo
#

where do you currently define the methods you intend to pass there?

fiery fern
#

Nowhere at the moment; I wanted the flexibility to be able to define what I want when I want to. But I guess the best approach for now would be to make an exported enum of predefined shooting behaviors that I could point to.

#

And whenever I want to add a new behavior I could just make a new function and add it to the enum.

regal igloo
#

i see 2 more options:

  • subclassing
  • creating a Resource-extending script for each gun that you can then set where you now have the Callable
#
# Interface type
extends Resource
class_name GunImpl

# Example Gun
extends GunImpl

func shoot() -> void:
  pass

# Gun.gd
@export var gun_impl: GunImpl

@onready var shoot_method = gun_impl.shoot```
#

that last bit may need instancing - haven't tried this yet

fiery fern
#

I ended up going with this approach. Seems to be working well so far:

regal igloo
#

nice