I want to set automatically a random int when a resource is created and never change it.
That’s pretty much it… But… How to do it?
I already attempted to create an entire import plugin that was actually just a resource file, but the resource file was read-only in inspector, which is not something I want.
The value will not change. It can be changed, though.
How are you using the random value?
Maybe a script like this is more appropriate:
class_name RandomInt
# This is globally accessible via RandomInt like:
# var value = RandomInt.get_value()
static func get_value() -> int:
return 8383838 # <- Your random value here.
This way the value will not change and cannot be changed except when manually entered by you.
Tip: godot’s integer field in the inspector has an expression evaluator. You can write randi() and it will evaluate it.
Note: These values are not “secure”. They can absolutely be modified by a malicious, curious, or bored user/person.
Alternately, you could use the resource’s resource_path and hash it:
extends Resource
var my_random_int:
get:
return resource_path.hash()
This is guaranteed to remain the same so long as the resource_path doesn’t change - in other words, so long as the path of the file in which the resource is saved doesn’t change. Also, be aware that that there is no guarantee that different resources won’t share the same random integer. That’s a general problem with 32 bit random integers.
And, sadly, two different strings can have the same hash, which would be a debugging nightmare to figure out and something surely I would forget to check.