Jump to content


Photo

how to load external files automatically.


  • Please log in to reply
8 replies to this topic

#1 clod14

clod14

    GMC Member

  • GMC Member
  • 412 posts

Posted 31 May 2012 - 01:32 AM

I am going to be making a plane game and while I do know how to manually load external image files. I was wondering if I had a folder named
"Ship Body" filled with 20 ship body parts.
if game maker can load them all instead of me programming them in 1 by 1?


an example of what I want to do, say I have 20 body parts in the folder. but I add 1 more. that means I now have 21.
I have previously coded 20 into gm. but the newest one wont be recognized because it wasn't programmed in.
Im wondering if there's a code that can just add everything automatically, so I dont have to keep programming in new images.
  • 0

#2 Lemon Flavor

Lemon Flavor

    The Helpful Lemon

  • GMC Member
  • 134 posts
  • Version:GM8

Posted 31 May 2012 - 01:38 AM

You could change the image names to be something like image1 to image 21 and use a formula to load them, but you would still have to specify the number of images in the folder.


EDIT: Actually there is a way where you wouldn't have to specify the number of images, but you would still have to name your images numerically.

Edited by Lemon Flavor, 31 May 2012 - 01:39 AM.

  • 0

#3 TeamSteeve

TeamSteeve

    GMC Member

  • GMC Member
  • 883 posts
  • Version:GM:Studio

Posted 31 May 2012 - 01:43 AM

Do you know how to use the file_find_ functions? So handy for this sort of thing! Look them up in the GM documentation.
  • 0

#4 Lemon Flavor

Lemon Flavor

    The Helpful Lemon

  • GMC Member
  • 134 posts
  • Version:GM8

Posted 31 May 2012 - 01:53 AM

fe=".jpg" // Change this to the type of file the images are
for (i=1;i>0;i+=1)
{
sprite_add_sprite("image"+string(i)+fe)
if not file_exists("image"+string(i+1)+fe) i=-1
}
This will work if your pictures are named "image1" to "image(max#)"
  • 2

#5 clod14

clod14

    GMC Member

  • GMC Member
  • 412 posts

Posted 31 May 2012 - 09:28 AM

Well thank you very much Lemon for helping me out! :) greatly appreciated.
I do have one more question, wondering if it can be done. I haven't been able to do it. and 2~3 hours have literally passed.

img = sprite_add(object0.cur_dir+string("img")+string(random)+string(".png"),1,true,true,bbox_bottom/2,bbox_right/2);

the last two arguments are troubling me.
bbox_bottom was meant to be used lets say if the image was 32x32. then bbox_bottom would have 32 pixels (i would think) and then I would divide that by 2, to get 16.
it would work for bbox_right aswell. thus I would get the center offset of the image, but alas I haven't been able to do so.

that was just one way I tried.
another way was byusing sprite_get_width and using (img) as the index, but that did not work so well.
another way was by sprite_get_bbox_right(img) which also resulted in fail.

I have read the GM help Sprites and images
and cant figure it out worth crap :(

and I bet it's the most simplest thing ever. anywhoo its 5:28am so im off to bed. thanks again lemon, hope you can see if you can figure what is wrong in my new code.
  • 0

#6 TeamSteeve

TeamSteeve

    GMC Member

  • GMC Member
  • 883 posts
  • Version:GM:Studio

Posted 31 May 2012 - 10:05 AM

Yeah, I've struggled with this when trying to add sprites previously.
Unfortunately there is no easy way I know of that you can find out the size of a sprite before you've added it and you can't change the origin of a sprite either.
bbox_bottom and bbox_right will only get the bounding box of the sprite for the object you are calling it from, not the sprite you are trying to load into the game.

A couple of ways I've gotten around this before was to put the sprite sizes in a text or ini file, just let 0,0 be the origin and work around it, or I have even (dare I say it) loaded the sprite once, got the size of it then loaded it again with the correct origin.
Never found an easy way to fix this.
Hope you have more luck than I did...
  • 0

#7 clod14

clod14

    GMC Member

  • GMC Member
  • 412 posts

Posted 31 May 2012 - 06:09 PM

Yeah, I've struggled with this when trying to add sprites previously.
Unfortunately there is no easy way I know of that you can find out the size of a sprite before you've added it and you can't change the origin of a sprite either.
bbox_bottom and bbox_right will only get the bounding box of the sprite for the object you are calling it from, not the sprite you are trying to load into the game.

A couple of ways I've gotten around this before was to put the sprite sizes in a text or ini file, just let 0,0 be the origin and work around it, or I have even (dare I say it) loaded the sprite once, got the size of it then loaded it again with the correct origin.
Never found an easy way to fix this.
Hope you have more luck than I did...


That's one way to do it, but I need it to be centered since its a cockpit. :/ and I want people to be able to make their own cockpits.
  • 0

#8 TeamSteeve

TeamSteeve

    GMC Member

  • GMC Member
  • 883 posts
  • Version:GM:Studio

Posted 31 May 2012 - 09:57 PM

Obviously the cockpit would have a maximum size. So if you were getting the user to make a cockpit in-game, then I would make sure it saves it as a certain size every time (even if it does have a bit of transparent space around the edges). If you were getting the user to create their own cockpit using a program outside of GM, then I'd specify the pixel size the image must be.

That's about the best solution I can think up quickly without knowing the specifics of your game.
  • 0

#9 clod14

clod14

    GMC Member

  • GMC Member
  • 412 posts

Posted 01 June 2012 - 02:08 AM

Done figured it out, saweet.
so this is what i did.

Create event
newchar = sprite_add(object0.cur_dir+string("img")+string(random)+string(".png"),1,true,true,0,0);
swidth=sprite_get_width(newchar)
sheight=sprite_get_height(newchar)
sprite_set_offset(newchar,swidth/2,sheight/2)

Basically I load the image with the starting offset of 0 and 0
once the image is loaded, It will get the width and height of said image that was loaded.
And then it will set the offset to center of the said image that was loaded.
works like a charm ;) in case anyone was wondering.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users