so foo and bar share the same type and value, right?
which means they can be used in the same context interchangeably, dont they?
print(foo.has_source_code()) # true
print(bar.has_source_code()) # Parser Error: Cannot call non-static function "has_source_code()" on the class "MyScript" directly. Make an instance instead.
i wonder if you loaded the MyScript already? or itâs just a class_name inside your res://myscript.gd
if thatâs the case, you will need to construct/create new of myscript.gd from its class_name MyScript
by doing
var foo = load("res://myscript.gd")
var bar = MyScript.new()
this should give no error
if you use its class_name and try to call function name like what you are doing, it will be treated as calling static function, because thereâs no instances
If you didnât understand: Iâm not trying to fix anything.
From my original post,
Iâm wondering why this is not the case and how it can be explained.
In your example,
foo and bar no longer have the same type ( nor the same value ) so it completely misses the point
and also your code will just fail because I never specified that MyScript has has_source_code method (it doesnât)
so the reason why godot cant find the has_source_code() method for MyScript is because this new MyScript is not loaded on runtime to have exact Script classâ methods
They arenât identical obviously.
In this I would guess that maybe the is keyword is returning incorrectly on bar. typeof() - returns the generic 24 - some kind of object foo == bar - this is likely doing a conversion as it states in the docs printt(foo, bar) - they output the same however this will convert to string foo is GDScript - this seems ok bar is GDScript - possibly returning incorrectly. If bar is GDScript then going up the chain of inheritance we find Script which has the method has_source_code().
If you change this to (bar as GDScript) then it finds the method has_source_code()
The keyword âisâ is ridiculously hard to search for so I didnât search too hard on the github for issues.
You left out extra parenthesis on your first 2 prints in the sample. Probably typo.
Anyhow, I believe you ran into a compile time bug you, where the editor prevents you from running the code. If you remove the offending line, in the debugger both foo and bar are Resources so should work. Other methods donât work either like bar.get_class()
I thought your question was perfectly clear and very interesting and certainly made me go âhuh?â. My first thought was that perhaps foo or (in particular) bar might be reserved words somehow but they are not. (Forgive me, I am a beginner).
I will be watching this closely for a complete answer, as it still seems really weird to me.
(It also made me giggle a bit when you kept saying âI am not asking to fix anythingâŚâ, I could feel your frustration! Great question I thought. (But as I said, I am just a beginner.)