How do I set text to the filename of an image file?

0 favourites
  • 8 posts
From the Asset Store
Elevate your games with our AI Chatbot Integration! Seamlessly connect your Construct 3 projects with GPT-4 for dynamic,
  • I am loading random images into a sprite using load from url (but just using the c3 project files folder).

    How do I set the text in a text box to de dervied from the filename prefix of the image file?

    Use:

    1) I have two images in the files folder - dog.png and cat.png

    2) A button is clicked and cat.png is loaded into the sprite via load from url event

    3) the text in a textbox is therefore set to say cat

    Many thanks for any assistance!

  • Why don't you make it with revert way?

    - You have the name of the png.

    - Have variable call nameOfImg.

    - Load Image from URL with the path is nameOfImg&".png"

    - Then you display the nameOfImg in text.

    - And the nameOfImg can be random like choose("dog", "cat").

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Why don't you make it with revert way?

    - You have the name of the png.

    - Have variable call nameOfImg.

    - Load Image from URL with the path is nameOfImg&".png"

    - Then you display the nameOfImg in text.

    - And the nameOfImg can be random like choose("dog", "cat").

    How does it know what image to load though? I don't want the instruction for what image to load to be derived from text that I have to type. It can't use choose.

    I want it to load images from the project files folder that have unique filenames (not a common base followed by a number sequence) - either randomly or alphabetically, and then to derive the text in the textbox from the filename.

    I know this is possible in plain js. I didn't do it, but the programmer I was working with did. Just hoping it can be done it C3 also.

    Thanks for any assistance.

  • I would really like to learn how to accomplish this if anyone has any thoughts. It seems like it must be possible. The filename and therefore the prefix is passed through when it's loaded from the url, so there must be a way to store it and use it. This again is the crux.

  • If I understood correctly, this will load a random file(png) from project files into a sprite, then set a text box to the choosen file name.

    If you want to get rid of the .png on the end use set text to replace(Variable1,".png","")

  • If I understood correctly, this will load a random file(png) from project files into a sprite, then set a text box to the choosen file name.

    If you want to get rid of the .png on the end use set text to replace(Variable1,".png","")

    Thanks for this sizcoz, it addresses part of what I am after. The other part though is to load images randomly from the files folder, without having to list all the filenames in a choose action. I have hundreds (thousands actually) of png files that are already prepared with nice simple descriptive filenames. If I have to type all of them into a choose action it defeats a lot of the purpose. Please see this post.

    construct.net/en/forum/construct-3/how-do-i-8/load-random-non-sequential-164100

    Thank you though, this is still helpful.

  • You will still need to make a list of all image files. You can use this method in Windows to prepare the list of filenames:

    exceltip.com/import-and-export-in-vba/how-to-extract-a-list-of-all-the-files-and-folders-within-a-windows-os-folder-onto-excel.html

    Then insert this list into an array in C3 and pick a random element from the array. Or you can put it into a text variable and use tokenat and tokencount to extract random filename from it.

  • Thanks, I did a little reading and it seems like a server side request has to be made to get the filenames. It seems to be fairly easy with PHP, and some posts said it can be done with AJAX. I was thinking that since the files folder is part of Construct and the request would not be cross-domain, there might be a way to do it that capitalizes on the somewhat proprietary design of Construct, but it looks like not.

    In any case, I took the same approach that you've suggested. I used Irfanview, which has a "save filenames to txt file" option, then a couple search replace operations to format it. But it looks like this excel method is another good option. Thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)