how to load external files automatically.
#1
Posted 31 May 2012 - 01:32 AM
"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.
#2
Posted 31 May 2012 - 01:38 AM
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.
#3
Posted 31 May 2012 - 01:43 AM
#4
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#)"
#5
Posted 31 May 2012 - 09:28 AM
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.
#6
Posted 31 May 2012 - 10:05 AM
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...
#7
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.
#8
Posted 31 May 2012 - 09:57 PM
That's about the best solution I can think up quickly without knowing the specifics of your game.
#9
Posted 01 June 2012 - 02:08 AM
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
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











