Can anyone tell me what mistake I made here?
Welcome to the forums @alex0264!
The issue is that you are trying to access a node that does not exist. Specifically, you are tyring to access a node called mobspawnlocation
. Because there are no nodes called mobspawnlocation
in the scene as a child of the main node, this is causing the issue.
You will need to add a node called mobpath
to the main
node, and then you will need to a child node to mobpath
called mobspawnlocation
. Then that should make it work and fix the issue. If you are following a tutorial, it may be that you missed a setup or something with the setup of the scene.
(Side note: On lines 25
and 26
, you have MobPath
as the name instead of mobpath
. In Godot, node names are case sensitive, so you'll need to make sure all the names are the same as the nodes in the scene)
I've already corrected it, but when I run it, I get this, what do I mean or how can I correct it?
@alex0264: A couple things could be causing the issue.
The first, is that you are instancing a scene called mob
(presumably stored in a class variable) into a local variable called mob
. This can confuse GDScript and is generally considered bad practice, because it can cause strange errors that are hard to debug. I would either change var mob = mob.instance()
to var clone = mob.instance
or rename the class variable so its something like var mob = mob_scene.instance()
. That way, you avoid having issues because the variables have the same name.
The second issue, and the one the debugger is showing, is that the mob
variable is a PathFollow2D, not a PackedScene, so it cannot be instanced. How are you defining mob
?
You will need to have it defined with something like var mob = load("res://path_to_mob_scene_here.tscn)
or var mob = preload("res://path_to_mob_scene_here.tscn)
for it to be a packed scene that you can instance. Without loading/preloading the scene, you will not have a PackedScene in the variable, and therefore you will be unable to instance it. I would suggest double checking how the mob
variable is defined (and maybe rename it to something like mob_scene
) to make sure its using load
or preload
.
I don't quite understand, can you explain to me in another way?
- Edited
the variable mob
on line 22 in your script is PathFollow2D node, so it won't have that instance
function which is owned by PackedScenes
. What most likely is that you assigned a PathFollow2D node to it somewhere in your code.
Try this. Where mob
is initialized (the one that's not in the function), use static typing to figure out where the invalid assignment is happening.
var mob: PackedScene = preload(path_to_scene)
You should be getting an error next time where the mob variable is being assigned a value that's not a PackedScene
.
- Edited
tell me this, why?
You need to replace Path_to_scene
with the resource path to the mob scene. For your project, based on what I can see of the file system in the screen shots, using "res://mob.tscn"
instead of Path_to_scene
on line 3 should fix the issue.
Correct. path_to_scene
is just a placeholder in my code example. You were supposed to put in the actual path in there.
then it would be: var mob = path_to_scene?
@alex0264 said: then it would be: var mob = path_to_scene?
Not quite. It would be something like var mob : PackedScene = preload("res://mob.tscn")
, as that is where the scene is located. the path_to_scene
is just a placeholder for "res://mob.tscn"
, which is the path of the scene you want to instance.
then I have to put that into a scene apart from the main one? for example that of the mob
@alex0264 said: then I have to put that into a scene apart from the main one? for example that of the mob
Nope, you already have a scene called mob.tscn
in your project (you can see it in the file browser in the Godot editor in the screenshots). All you should need to do is change the links of code in main.gd
to var mob : PackedScene = preload(“res://mob.tscn”)
.
Let me help break this down for you.
var mob
is a way to declare a new variable called mob
for you to use in your code. If the statement's made outside of a function, then it can be accessed anywhere; even outside of the script.
:PackedScene
tells the script that this variable should only hold a PackedScene
, and no over kind of data. If for example you try to set the variable to 1
, an int, then it won't work.
=
is obviously how you assign a value to a variable.
preload
is a function that allows you to load a resource before the game even starts running. The path inside of the function represents the location of the resource. In this case, it should be given the location of the mob resource in your project's filesystem.
Should I stay like this?
As long as it works, but question. Why do you upload the same image twice?
i dont know
- Edited
I still get the error
- Edited
Upon inspecting your post's structure, I see that there are two image links 
. You just gotta delete one of them. You can edit a post by clicking the gear icon on the top right of it.
Do you use the Add images
button at the bottom or the little image icon at the top of the editor? Or just drag and drop an image into the post?