Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Storing a picture's filename (numbers) in a variable

Hugo_S

Member

Hi guys,

I would like to store a picture's filename (numbers) in a variable.

For example:
I have a picture called "2456256.png" in my project. How can I store its filename in a variable?

Would it be also possible to detect numbers in a filename and take only them? For example "sword_2824.png"
Or taking always the last 4 numbers of any filename?

Thanks for help in advance!  :smile:

Hugo
 

khmp

Sponsor

Ok first question storing a filename in a variable. Yes it is possible. Use the event command "Script Call..."
Code:
$game_variables[n] = '2456256.png'
Where 'n' is the $game_variables you would like to use.

Not sure what you mean with the second question. Take only them? Regardless string manipulation is quite powerful and its easy to accomplish in Ruby so I will answer yes to this as well.
 

Hugo_S

Member

Thanks for the real quick reply! :)

I just tried it out. If I look in the F9 variable sheet overview (in test mode), then the variable has the value "2456256.png" and when I manipulate this value I get an error message: "cannot convert Fixnum into String".

Answering your first question:
So for example I would like to do the following: I have a picture called "sword_918.png". I would like to store the number 918 in a variable and after this I would like to calculate with this number.
 

khmp

Sponsor

Code:
$game_variables[n] = 'sword_918.png'[/\d+/]
print $game_variables[n] # this print is just to show it works.

The most important feature being the brackets containing "/\d+/". This basically says that from whatever string is calling the brackets method to pull the first contiguous string of numbers that are grouped together. But thats any number. Not just the ones at the end. So let's say you had the following string:
Code:
number = '1sword_918.png'[/\d+/]
print number # 1

So be careful with file naming. If you always have a separating character from the name and the number meaning. That underscore. You can use another method to retrieve the number.
Code:
$game_variables[n] = 
  'sword_918.png'.delete!(".png").split('_').last
print $game_variables[n]

Of if you have different file types:
Code:
$game_variables[n] = 
  'sword_918.png'.split('_').last.split('.').first
print $game_variables[n]

All the prints are just extraneous work that you can use for testing purposes.

Good luck with it Hugo_S! :thumb:
 

Hugo_S

Member

Great! Thank you very much!
I have to work a little bit with it and keep on reading tutorials.
I come back to this thread if I have suceeded in getting worked what I plan to do :)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top