Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_init issue for OOP #34365

Closed
nonunknown opened this issue Dec 15, 2019 · 12 comments
Closed

_init issue for OOP #34365

nonunknown opened this issue Dec 15, 2019 · 12 comments

Comments

@nonunknown
Copy link
Contributor

nonunknown commented Dec 15, 2019

Godot version:
3.2
OS/device including version:
ubuntu 18
Issue description:
` class FSM:
var manager = null
func _init(man):
manager = man

class FSM_Test extends FSM:
pass

func _on_ready():
var manager = "test"
var state = FSM_Test.new(manager) `
If you pass the manager arg the following error occurs:
Invalid call to function new Expected 0 args
if you does not pass the arg:
Too few arguments for _init() expected at least 1

@KoBeWi
Copy link
Member

KoBeWi commented Dec 15, 2019

I can't seem to reproduce it. If you don't provide a constructor for FSM_Test, it will inherit one. The code works perfectly for me (except that it complains about pass after class).

If you write your own constructor in the inheriting class, you pass the arguments like this: https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_basics.html#class-constructor

@nonunknown
Copy link
Contributor Author

nonunknown commented Dec 15, 2019

Try to reproduce with this one.

Utils.gd - AutoLoad

class FSM_Manager:
	var current:FSM = null
	var states = {}

class FSM:
	var manager = null
	func _init(m):
		manager = m

	func _on_enter():
		pass

Selectable.gd

enum {ST_UNSELECTED,ST_SELECTED}

class St_unselected extends Utils.FSM:
	
	func name():
		return "unselected"

	func _input(event):
		pass

class St_selected extends Utils.FSM:
	func name():
		print("selected:  mudafucka")

onready var manager = null
func _on_ready():
	manager = Utils.FSM_Manager.new()
	manager.states[ST_UNSELECTED] = St_unselected.new(manager)

@KoBeWi
Copy link
Member

KoBeWi commented Dec 15, 2019

Ok, this is weird. I don't see any significant difference from previous code, but this one doesn't work.

@nonunknown
Copy link
Contributor Author

thanks for testing dude, I think its because the Autoload (singleton) but its only a tought

@eon-s
Copy link
Contributor

eon-s commented Dec 16, 2019

Utils is not a class (autoloaded elements are node instances) and you refer to the internal classes defined in the script (which should be loaded first and get a reference), if you name it with class_name it should work.

@nonunknown
Copy link
Contributor Author

@eon-s I'll give a try, but if it is a node instance extending a node this code should work...

@eon-s
Copy link
Contributor

eon-s commented Dec 17, 2019

No, it can't work because you are trying to access to class definitions via a node instance and not via the script.
It should be something like extends load("res://Utils.gd").FSM but I do not think it will work on a class definition, unlike named classes.

@KoBeWi
Copy link
Member

KoBeWi commented Dec 17, 2019

Aaand I just encountered the same issue, with a script inheriting a class defined with class_name. What I did is however this

export var event_script: Script
export var events: MapEvents

func _ready():
    if event_script:
        events = event_script.new(self)

where provided event_script resource extends MapEvents, but does not have a name itself (it's also a built-in script, but I don't think it's relevant).

@eon-s
Copy link
Contributor

eon-s commented Dec 18, 2019

@KoBeWi built in scripts do not support some features, like named classes, see #27876

@KoBeWi
Copy link
Member

KoBeWi commented Nov 18, 2020

I can't reproduce anymore with the code in #34365 (comment)

Can anyone still reproduce this bug in Godot 3.2.4 beta2 or any later release?

@nonunknown
Copy link
Contributor Author

@KoBeWi Tested here, no error
image

@KoBeWi
Copy link
Member

KoBeWi commented Nov 18, 2020

So looks like it's resolved.

@KoBeWi KoBeWi closed this as completed Nov 18, 2020
@KoBeWi KoBeWi added this to the 3.2 milestone Nov 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants