Skip to content

Instantly share code, notes, and snippets.

@workhorsy
Last active December 25, 2021 15:46
Show Gist options
  • Select an option

  • Save workhorsy/f995e28762c92b2104396c6c5e341491 to your computer and use it in GitHub Desktop.

Select an option

Save workhorsy/f995e28762c92b2104396c6c5e341491 to your computer and use it in GitHub Desktop.
# Here is a sloppy hack to get a Godot resource type without having to load it first
func get_resource_type(file_name : String) -> GDScriptNativeClass:
var f := File.new()
f.open(file_name, File.READ)
var text := f.get_as_text()
#[gd_resource type="ParticlesMaterial" load_steps=3 format=2]
for line in text.split("\n"):
line = line.rstrip("\r")
if line.find("[gd_resource") == 0 and line.find("]") == line.length()-1:
line = line.substr("[gd_resource".length(), line.length()-2).lstrip(" ").rstrip(" ")
#print("!!! line ", line)
var entries = line.split(" ")
for entry in entries:
#print(" !!! entry ", entry)
var pair = entry.split("=")
#print(pair)
if pair[0] == "type":
var value = pair[1].lstrip("\"").rstrip("\"")
match value:
"SpatialMaterial": return SpatialMaterial
"ShaderMaterial": return ShaderMaterial
"ParticlesMaterial": return ParticlesMaterial
"Shader": return Shader
"Gradient": return Gradient
_:
push_warning("Unexpected resource type: %s" % [value])
return null
return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment