#functions in resources

1 messages · Page 1 of 1 (latest)

meager notch
#

Hi, I have a resource with a function.

extends Resource
class_name Test_Event

func choice_one():
    group.stat += x
    print("group gain x stats")

it's called by pressing a button

extends Node
func _on_choice_one_pressed():
    event_resource.choice_one()

how do I have it affect a group?
I'm using godot 4.2
thank you

cloud orbit
#
  node.choice_one()```
faint shale
#

there's a method specifically for this, get_tree().call_group("group", "choice_one")

meager notch
#

choice one is a function in a resource script

#

which is not part of the scene tree so can't use get_tree()

cloud orbit
#

So it is. phooey.

#

You could add a _ready() function that connects a signal that you emit instead of calling the function to the function itself.

#

That raises the question of how you get a reference to the signal in question. You could make it part of an autoload, for instance. There's also an interesting-looking solution found at this StackOverflow thread.

#

This requires an understanding of the "static" keyword, which allows a var to be accessed through the type itself rather than an instance of the variable. This is of course exactly the same as the autoload approach, but it's symbolically tied to the custom class you've created, which just feels better to me personally.

#

(example of static vs. non-static would be Color.BLACK accessing a const inside of the script for the Color type, whereas var instance:Color; instance.a accesses the a-component of an existing Color-type variable.)