Can anyone tell me what mistake I made here?

«1

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

    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)

  • alex0264alex0264 Posts: 18Member


    I've already corrected it, but when I run it, I get this, what do I mean or how can I correct it?

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

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

  • alex0264alex0264 Posts: 18Member

    I don't quite understand, can you explain to me in another way?

  • SIsilicon28SIsilicon28 Posts: 744Moderator
    edited July 7

    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.

  • alex0264alex0264 Posts: 18Member
    edited July 7



    tell me this, why?

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

    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.

  • SIsilicon28SIsilicon28 Posts: 744Moderator

    Correct. path_to_scene is just a placeholder in my code example. You were supposed to put in the actual path in there.

  • alex0264alex0264 Posts: 18Member

    then it would be: var mob = path_to_scene?

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

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

  • alex0264alex0264 Posts: 18Member

    then I have to put that into a scene apart from the main one? for example that of the mob

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

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

  • SIsilicon28SIsilicon28 Posts: 744Moderator

    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.
  • alex0264alex0264 Posts: 18Member



    Should I stay like this?

  • SIsilicon28SIsilicon28 Posts: 744Moderator

    As long as it works, but question. Why do you upload the same image twice?

  • alex0264alex0264 Posts: 18Member

    i dont know

  • alex0264alex0264 Posts: 18Member
    edited July 11

    I still get the error

  • SIsilicon28SIsilicon28 Posts: 744Moderator
    edited July 11

    Upon inspecting your post's structure, I see that there are two image links ![](url). You just gotta delete one of them. You can edit a post by clicking the gear icon on the top right of it.

  • MegalomaniakMegalomaniak Posts: 2,738Admin

    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?

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin
    edited July 11

    It might be due to drag-and-drop, since both Imgur and Vanilla use drag-in-drop for image uploading. That would also explain why one link goes to Imgur, and another to the forums server.

    Edit: Unfortunately, its part of the Advance editor and there is no great way to disable it, that I can see anyway. I might be able to modify the Advance editor though, but I haven't looked into it yet. I suppose we could also disable drag-and-drop for the Imgur plugin to resolve the issue of double image uploads.

  • alex0264alex0264 Posts: 18Member

    that I can put in the code of "mob" to make it work?

  • SIsilicon28SIsilicon28 Posts: 744Moderator

    Could you elaborate please?

  • alex0264alex0264 Posts: 18Member

    @SIsilicon28 said:
    Could you elaborate please?

    So how can I make it work, do I have to put something in the mob script or something?

  • SIsilicon28SIsilicon28 Posts: 744Moderator
    edited July 14

    Remember this?

    What I was suggesting to you was a way to figure out the root of this problem. This is not relevant to the mob script itself.
    It was only to add typed scripting to the mob variable and see where in your code you assigned something you shouldn't.

    Ok, I know what to do. Could you please share you entire script? That can help us better to help you. Don't share it in screenshots please. ;)

  • alex0264alex0264 Posts: 18Member
    edited July 18

    how can I do that? I copy it and paste it?

  • MegalomaniakMegalomaniak Posts: 2,738Admin
    edited July 18

    Yes then after pasting it, on the line in front and behind the pasted code you add ~~~ and it gets formatted as a code block.

  • alex0264alex0264 Posts: 18Member
    edited July 18
    func _on_mobtimer_timeout():
        $mobpath/mobspawnlocation.offset = randi()
    # warning-ignore:shadowed_variable
        var mob = mob.instance()
        add_child(mob)
        var direction = $mobpath/mobspawnlocation.rotation + PI / 2
        mob.position = $mobpath/mobspawnlocation.position
        direction += rand_range(-PI / 4, PI / 4)
        mob.rotation = direction
        mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0).rotated(direction) 
    

    (TwistedTwigleg edit: Fixed the code formatting)

  • alex0264alex0264 Posts: 18Member

    that I can change or modify to make it work?

  • MegalomaniakMegalomaniak Posts: 2,738Admin

    That is just a single function from the script, mind copying and pasting the whole thing as per @SIsilicon28 request?

  • alex0264alex0264 Posts: 18Member

    then would it be the function of mobtimer to change something?

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file