+1 vote

*Filed bug report here: https://github.com/godotengine/godot/issues/42815

*I tried this modification (in both places), attempting to override the cache, but I still get the same duplicate results! ResourceLoader.load(path, type_hint, no_cache)

var floor_resource = ResourceLoader.load("FloorBlock.tscn","",true)

*this is related to caching, and I'm researching "take_over_path" but I don't know how to implement it.

I have some code on a script on a root tree, simple, I threw it on a "Node" because it doesn't interact with its parent. When the game starts it preloads two blocks,

extends Node 
func _ready():
    var floor_resource = preload("FloorBlock.tscn")
    var floor_tile = floor_resource.instance()
    floor_tile.transform.origin = Vector3(0,1,0)
    add_child(floor_tile) # self is implicit

    var floor_resource2 = preload("FloorBlock.tscn")
    var floor_tile2 = floor_resource2.instance()
    floor_tile2.transform.origin = Vector3(0,5,0)
    add_child(floor_tile2) # self is implicit

And then the blocks have this code;

func _ready():
    var x = rand_range(1,20)
    var z = rand_range(1,20)

    var halfVect = Vector3(x/2, 1, z/2)
    $CollisionShape.shape.extents = halfVect

    var newVect = Vector3(x, 1, z)
    $MeshInstance.mesh.size = newVect

Every single time I get two blocks of the same size. Why? I instantiated them into different variables, from two different calls, and made sure to use randomize() when getting my numbers, so why do they come out the same size?

I can easily see in my output that the random numbers and vector3's are different.

FloorBlock.tscn is nothing but a RigidBody with a MeshInstance (CubeMesh) and CollisionShape (boxShape). The root scene is blank except for the node, script and camera.


in Engine by (58 points)
edited by

1 Answer

+1 vote
Best answer

You're altering the mesh resource rather than the scale of the MeshInstance, you need to duplicate the mesh resource because it is shared across both instances.

var new_res = $MeshInstance.mesh.duplicate()
$MeshInstance.mesh = new_res
$MeshInstance.mesh.size = newVec
by (3,239 points)
selected by

Thank you so much, this solves it and I can try to move on.

What I see this as doing is instantiating a new copy of the current mesh instance and then setting it again to the unmodified copy, then setting the size? Is this just so that a handle can be created? Can you help me understand why this is necessary? I guess I'm not understanding if it is the caching, or how or why the sharing works across instances? I'd expect 'self' to be contained to the instance of the class, which would be in this case the instance of the copy of the script in the instantiated scene? or I'm overthinking it because I'm still learning gdscript

Thanks again

Just came for the duplicate link and saw your reply but no email? :/

As far as Godot is concerned that particular scene uses that one resource even if there's multiple instances of the scene. Only the node classes can be changed independently, resources are there own file.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.