To script or not a script

Suppose we have 2 definitions

var foo = load("res://myscript.gd")
var bar = MyScript

Now lets do some tests

print(type_string(typeof(foo)) # Object
print(type_string(typeof(bar)) # Object
print(foo is GDScript) # true
print(bar is GDScript) # true
print(foo == bar) # true

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.

explain pls what’s going on here

5 Likes

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

Yes, i know. That’s not the point.

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)

2 Likes

Loading a script loads a script, not a type

1 Like

you will need to actually create it to be able to access the has_source_code function
because has_source_code is not a static function

yes i just realized you are actually calling Script’s built in method, else the .new() would have worked

this function already exists in Script class.
That’s what I’m accessing.

1 Like

looks like you are tying to do what this do

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

  1. I’m not trying “to do” anything
  2. this other post has a broken link so it’s hard to say for sure, but it’s not what my question is about

i see, so nothing to see here

so 2 identical objects can somehow have different methods?
isn’t it strange

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.

2 Likes

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()

Would be good idea to post it as a bug to github.

2 Likes

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.)

3 Likes