zaksen
December 24, 2023, 5:51pm
1
Godot Version
4.1.2
Question
I’m trying to create an action system and in my action method call from the Action.gd class. I have another class ChoiceAction.gd which is written on top of Action.gd (inherits it) but when I try to call a method from ChoiceAction.gd it doesn’t work (godot say that action is null):
try some debug prints
in _init
action = _action
print("_init, action:", action)
func _ready():
print("_ready, action:", action)
func call_action():
print("call_action, action:", action)
#......
zaksen
December 24, 2023, 9:57pm
3
i got this:
func _ready isn’t work becuse it Resource not Node.
It looks like it’s trying to convert my ChoiceAction to an Action and failing, but that’s not how it’s supposed to work.
example export SET debug, and that for your Action class?
@export var test: String:
set(value):
print("export test: ", value)
zaksen
December 24, 2023, 10:16pm
5
huh? it looks like it resetted somewhere.
try with print in your func _init
, maybe also via base class?
zaksen
December 25, 2023, 10:38am
7
So I fixed it, the problem was that in the ActionTextpart there was a Choice Action class and in init all the variables were optional and for some reason it wasn’t working fine, I change:
func _init(_variants :int, _choice_1 :Choice, _choice_2 :Choice...):
...
into
func _init(_variants := 2, _choice_1 := Choice.new(), _choice_2 := Choice.new(), _choice_3 := Choice.new(), _choice_4 := Choice.new()):
...
zaksen
December 25, 2023, 10:41am
8
oh not, that not fix problem
but now when it is set a second time, it is not null:
zaksen
December 25, 2023, 10:43am
9
in call_action method it still is null:
zaksen
December 25, 2023, 10:46am
10
looks like he just didn’t understand what class is base:
zaksen
December 25, 2023, 10:49am
11
now i really fix that:
i change:
var action: Action = Action.new():
set(value):
print(value)
from:
var action: Action:
set(value):
print(value)
zaksen
December 25, 2023, 10:54am
12
but now it call method only from base class:
extends Resource
class_name Action
func _init():
pass
func do_action(player):
printerr("Overwrite new do_action method in your class!")
pass
extends Action
class_name ChoiceAction
@export var variants :int
@export var choice_1 :Choice
@export var choice_2 :Choice
@export var choice_3 :Choice
@export var choice_4 :Choice
func _init(_variants := 2, _choice_1 := Choice.new(), _choice_2 := Choice.new(), _choice_3 := Choice.new(), _choice_4 := Choice.new()):
variants = _variants
choice_1 = _choice_1
choice_2 = _choice_2
choice_3 = _choice_3
choice_4 = _choice_4
func do_action(player):
print("Do your choice!")
pass
zaksen
December 25, 2023, 11:15am
13
hmm, i check remote and when i start game it change my type into action
zaksen
December 25, 2023, 12:17pm
14
ok, I’m doing something similar in another class and that didn’t work either. Looks like Godot just can’t do what I need
oh ok, I’ll try something out later
zaksen
December 25, 2023, 2:18pm
16
If you want to see what I’ve done so far, you can watch it here:
1 Like
but this function overloading
works
func _ready():
print("player _ready")
var action : Action = Action.new()
action.do_action(self)
var choise_action : ChoiceAction = ChoiceAction.new()
choise_action.do_action(self)
player _ready
Overwrite do_action method in your custom class!
player:player:<CharacterBody2D#29678896351>
Do your choice!
player:player:<CharacterBody2D#29678896351>
zaksen
December 25, 2023, 3:48pm
18
hmm, I think it works because you create in code. But when the ChoiceAction is created from the inspector, for some reason it doesn’t work.
and var choice_action = action as ChoiceAction
?
@export
var action: Action = Action.new():
set(value):
print("++++++++++++++++++++++++++++++++++")
print("ActionTextPart export set ", value)
print("get_class: ", value.get_class())
var choice_action = value as ChoiceAction
print("choice_action: ", choice_action)
print("-----------------------------------")
print debugs
++++++++++++++++++++++++++++++++++
ActionTextPart export set <Resource#-9223372007679195861>
get_class: Resource
choice_action: <null>
-----------------------------------
++++++++++++++++++++++++++++++++++
ActionTextPart export set <Resource#-9223372007863745238>
get_class: Resource
choice_action: <Resource#-9223372007863745238>
-----------------------------------
currently I don’t know how it can be tested in your running game
func call_action(player):
var choice_action = action as ChoiceAction
if choice_action:
choice_action.do_action(player)
zaksen
December 25, 2023, 4:29pm
20
So I have exactly the same problem in Sayer.gd:
extends Interactor
@export_category("Настройки текста")
@export
var text = Text.new([])
func _init(_text = Text.new([])):
text = _text
func interact(player):
player.open_text(text as Text)
Interact.gd:
extends Node
class_name Interactor
func interact(player):
printerr("Overwrite interact method in your custom class!")
pass
and last one text.gd:
extends Resource
class_name Text
@export var text_parts : Array[TextPart] = []
func _init(_text_parts : Array[TextPart]):
text_parts = _text_parts
when I try to pass a Text object to the player from Sayer.gd using player.open_text() it throws an error: