Home Button News Button About & FAQ Button Members Button In.Dev Button Play Button Forum Button Resources Button Links Button Contact Button
Main Logo
Resources
 

ARTICLE ARTICLE
Written by Chronic Chronic
Posted on 2/May/2007
External Resources

This tutorial covers the basics of using the add and replace functions that allows changing resources at run time. These functions require you to be using a registered version of Game Maker 6.0 or higher.

Like always, am going to keep things generic because, after all i don't know what type of game you're making or how you will using these functions. So, i'll just be explaining how to import a sprite, background and sound at run time, and how to replace them.

To give some sort of idea how this could be used in a game, lets assume we collect an item and it takes us to an alternative zone, sort of like seen in Zelda: Link to the past when you use the mirror to enter Dark World.

Below is a list of the resources i will be using:




Player Sprites




Bush, Rock and Potion




Sounds




Backgrounds



If you are following along this tutorial, make sure to save your project at least once so when you come to use the functions to add resources, Game Maker knows where to start looking from for your resources.

In my project setup, my folder structure is as follows:


[root]
  |---- GM6
  |---- [data]
           |---- Images and Sounds


I'm using folders not only for clarity when you view the games folder, but also as an example on how to load resources from folders. You can store yours how ever you wish, but remember to change the scripts that follow to reflect your setup.

With your project file open, add a new object and name it "init_resources". This object will be used to initially load the resources.

Add a new room, the name of it doesn't matter for this tutorial so leave it as room0 if you wish. Add the init_resources object some where in to this room. Nothing else should be placed in to this room.

In the object add a create event and a code action and write the following

:
{
  global.spr_player = sprite_add(working_directory + "dataimage_001.gif",1,true,true,false,true,0,0);  
  room_goto_next();
}


You should read the Game Maker help file for information about the arguments of the sprite_add() function. The values i have set are the same that would be set if you add a sprite through the regular method... except for the imgnumb argument this is.

The working_directory variable isn't really needed here, however, i've got it incase the game is ran from a shortcut and it has no working directory set, this may cause problems with the game trying to load resources from the location of the shortcut file... its better to be safe than sorry.

Lets now set up the player object to ensure everything is working. Add a new object, and name it "obj_player". Add a create event with the following script:

:
{
  sprite_index = global.spr_player;
}


This will set the sprite for the object to the one we loaded with init_resources. For testing it would be a good idea to have a simple movement script. Add the script below in to a step event.

:
{
  if (keyboard_check(vk_left) && place_free(x-5,y)) x -= 5;
  if (keyboard_check(vk_right) && place_free(x+5,y)) x += 5;
  if (keyboard_check(vk_up) && place_free(x,y-5)) y -= 5;
  if (keyboard_check(vk_down) && place_free(x,y+5)) y += 5;
}


Add a new room and place the player object in to it. This will be our game room. You should test the game at this point to make sure everything is working fine, if you see the player then things are working so far.

we now need something to interact with, so lets add in the Bush and Potion sprites.

Open the script in the init_resources object and add the following lines before the room_goto_next() function.

:
global.spr_block = sprite_add(working_directory + "dataimage_003.gif",1,false,true,false,true,0,0);
global.spr_potion = sprite_add(working_directory + "dataimage_005.gif",1,true,true,false,true,0,0);


Note: In the block sprite the "precise" argument is this time false, this is because its an irregular shape making it easy for the player to get hung up on it

Add two new objects and name them "obj_block" and "obj_potion". Add create events to them both and give them the same script as the player object except for the global variable, change this occordingly to match the object. You should also set obj_block to solid so the player cannot pass through it.

Add a few blocks to the game room, remember to space them out so thier sprite won't overlap other objects when playing the game. Because all of the sprites used in this tutorial are 32x32 i found it helpfull to change the rooms grid setting. Once you are happy with the blocks, add in one potion object.

We now need to add script to the potion to make it load in the alternative world sprites and then make the objects use them. This is done in the same way as before.

:
{
  sprite_replace(global.spr_player,working_directory + "dataimage_002.gif",1,false,true,false,true,0,0);
  sprite_replace(global.spr_block,working_directory + "dataimage_004.gif",1,true,true,false,true,0,0);

  instance_destroy();
}


Thats all the sprites taken care of, now lets go back over things and add in the sounds and backgrounds.

Edit the init_resources again, and add in the following lines before the room_goto_next() function.

:
global.background = background_add(working_directory + "dataimage_006.bmp",false,false,false);
global.sound = sound_add(working_directory + "datasound_001.wav",0,true);


Like with the sprite_add() lines, the values for these are set to how a background/sound would be set to if added normaly. Remember to check the help file for more information on each argument.

Note: add_background cannot load gif files, only jpg and bmp.

Now comes the point where we have to decide where to set the background to be shown. In my opinion the best place to set it would be in the rooms creation code as this is only ran once. To get to this open the room up for editing and click settings, this will open a window much like the code action window.

:
{
  background_visible[0] = true;
  background_index = global.background;
}


In collision event in obj_potion, add the following before the instance_destroy() function.

:
background_replace(global.background,working_directory + "dataimage_007.bmp",false,false,false);
sound_replace(global.sound,working_directory + "datasound_002.wav",0,true);


Now we just need to put the sound to use. Open up obj_player and add in a key press event for space. In a code action add the following:

:
{
  sound_play(global.sound);
}


That brings me to the end of the tutorial, you can find an example of the completed tutorial below

External Resources - Example
 

Eo Logo   Link to Us | Feeds
2004 - 2018 © Eo
Website by House of Ninzha & Simon Donkers