I have a project that will eventually have hundreds of clothing objects, and I'm trying to allow my game to populate a menu with buttons representing random clothing objects, that when pressed, will instance and parent that object to a character.<br /><br />When I run the code below, I get the error message:<br />"Expected string constant as 'preload' argument."<br /><br />var subfolder = str(##code for choosing a subfolder##)<br />var scn = str(##code for choosing a scene file within that folder##)<br /><br />var model = str("res://assets/character/clothing/" + (subfolder) + (scn))<br />var newobject = preload(model).instance()<br />get_node("armature").add_child( newobject )<br /><br />When I 'print(model)', I get the intended result.  Is there a way to trick the engine into accepting my 'model' variable as a constant?  Or is there another/better way to accomplish what I am trying to do?<br /><br />Thanks

[quote author=Delirus link=topic=14486.msg14617#msg14617 date=1459920755]"Expected string constant as 'preload' argument."[/quote]As the message said, [b]preload[/b] expects a constant string argument. That's because [b]preload[/b] is run when you start your project and [i]preloads[/i] all the listed resources in the memory.In your case, since you are building the string at runtime based on other variables, it can't be consider a constant ([b]const[/b]), so you need to use [b]load[/b] instead of [b]preload[/b] to load the resource in memory only when called for.

Thank you for being kind enough to answer a question that I now realize was covered in the official scripting tutorial.  It had been a few weeks since I had gone over it, and I had been mostly copying and pasting elements from my earlier scripting experiments.Googling the error message came up with nothing, so hopefully now this thread can offer a service to the future forgetful. (if Google ever catches up and places the new forum higher on its index list)

10 days later

[quote author=Delirus link=topic=14486.msg15568#msg15568 date=1460086263]Googling the error message came up with nothing, so hopefully now this thread can offer a service to the future forgetful. (if Google ever catches up and places the new forum higher on its index list)[/quote]Suggestion on that note - edit the first post and put [Solved] in front of the title of the thread, so people will know it's got the answer they're looking for!

7 years later