Why are these blocks the same size?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By combatvolcano

*Filed bug report here: ResourceLoader.load not obeying no_cache · Issue #42815 · godotengine/godot · GitHub

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


:bust_in_silhouette: Reply From: Magso

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

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

combatvolcano | 2020-10-15 23:15

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

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.

Magso | 2020-11-09 11:35