0 votes

I'm making an application that randomly generates a set of characters for a game and then displays them in UI. I have an image for each character saved in res://, and the needed image (determined after randomization) is found in the file system and set as the texture for a TextureRect via code. This is an example of what I'm doing:

func add_texture(imagePath : String, objectPath : String):
  var image = Image.new()
  var texture = ImageTexture.new()

  image.load(path)
  texture.create_from_image(image)
  get_node(objectPath).texture = texture

This produces the error in the title. Is there a way to either fix the code or try something else that doesn't produce the error? I've been trying to troubleshoot this for about two hours and haven't found anything that fixes my problem.

Godot version 3.4
in Engine by (88 points)

1 Answer

0 votes
Best answer

You have overcomplicated this.
If I understand Your vision correctly, all You need is one String with chosen name. Choose randomly form array of strings and input resulting randomizedname into :
var randomimage = load("res://" + randomized
name + ".png")
Colorrect.texture = randomimage

Reason for error was that You called other node to load anything. Load or preload can only be executed "personally" by nodes

by (7,743 points)
selected by

This worked perfectly, thank you :) I feel silly for making it so complicated initially though lol

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.