Associative Array Tutorial
by Nivram
Beginner-Should have a basic understanding of MMF.
You will learn the following:
What the Associative Array is.
Saving and Loading, including the character position and items.
Movment of your character using a Simple Custom Movement.
We will be doing a Associative Array Tutorial. The Associative Array extension is required. You should also download the .mfa file so you can follow along, or compare my .mfa file to the one you will be building.
associativearraytutorial.mfa (35kb)
With the Associative array you can save and load character positions, directions, current stats, or just about any value you need. It can, when made global, also share these values over frames and sub-applications, which is really handy for inventories, maps and other things. The beauty of this array is the use of plain english "Keywords", which can be followed by a number or string such as:
NivramsOxygen=480
or
NivramsLevel=duffus
You can also retrieve data of counters, alterable values, global values, characters, enemies, Associative Array, or just about anything.
This makes it simple, along with good comments, to keep track of what is happening within your game or application. Use the Modify/Add a Value or Modify/Add a String to set the value at a Key.
Nivram overlaps Garlic
+Only one action when event loops
Associative Array: Set Value at Key "GARLIC" to 1
Garlic:
Destroy
To retrieve the data (Key) from the Associative Array, Compare Two General Values:
"GARLIC"=1
+Only one action when event loops
Garlic:
destroy
When you save your game using the Associative Array, the Key is saved. When you load the game, the Key GARLIC will be equal to 1. The garlic will be destroyed and Nivram will still have the garlic. If you desire, when the Key GARLIC=1 you can place a garlic icon in an inventory slot somewhere.
So, how do we save our game?
Let's say we have a Save Button and want to save the array in the game directory:
Player clicks on Save Button
Associative Array:
Save Associative Array file apppath$+"nivram.dat"
(you can use any name and file ext you want like: duffus.sav)
So, how do we load our game?
Let's say we have a Load Button and want to load the array from our game directory:
Player clicks on Load Button
Associative Array:
Load Associative Array file apppath$+"nivram.dat"
I also want to let you know that for some value loadings, such as position of the character, you must put in a second event for clicking on the Load Button. The loading of the array needs to be first, before calling up values. If the values, such as character postion, is called before loading the actual array, the character position will not be loaded.
The Associative Array can find an instance of a word or string. So you can load in a vocabulary and see if the word exists without looping. Instead of having numerous List Objects for Shops in an RPG, you could use just one Associative Array. With it you can list the item and price and call it up for any Shop your player enters. Very good if your player changes levels. You load up the Associative Array, then all you have to do is take a couple of items and their prices, and put them into the Shop.
A couple of notes for you:
You can disable the pop up for this extension by unchecking "Remind me later"
Ok, now since you know how the Associative Array works lets make our application. Start MMF and click "New" in the file drop down.
In the Events Editor and for your first code make a Comment:
Associative Array Tutorial by "your name".
Go to the Frame Editor, New Object, and click on Associative Array,then click OK. Move it to the outside of the Frame. We are going to make the Associative Array global, so double click it and check "Make Object Data Global" and Click OK. The reason we want to make it global now is if you want to expand on the application to include more frames or add Sub-applications.
We are going to add two active objects to the frame. One will be the moveable character and the other will be an item that we can collect, maybe a coin. For the character leave it at 32X32 and the color does not matter. For the coin, make it a circle 16X16, and again the color does not matter. Leave the movement "static" and place both actives somewhere in your frame. Rename your square active to "character" and the circle to "coin".
The first thing we will do is make the character move using the Arrow Keys. Go to the Event Editor and make this Group Events:
Movement Setup
Leave the "Active when frame starts" checked.
Let's make four other Group Events:
Move Right
Move Left
Move Up
Move Down
Uncheck "Active when frame starts"
Movement Setup Group Events
Add these events to the Movement Setup Group Events. They are going to be activating and deactivating the different Move Group Events.
Upon pressing Right Arrow
Activate Group Move Right
Deactivate Group Move Left
Deactivate Group Move Up
Deactivate Group Move Down
Character:
Toggle internal flag 1
Set internal flag 2 off
Set internal flag 3 off
Set internal flag 4 off
Upon pressing Left Arrow
Activate Group Move Left
Deactivate Group Move Right
Deactivate Group Move Up
Deactivate Group Move Down
Character:
Toggle internal flag 2
Set internal flag 1 off
Set internal flag 3 off
Set internal flag 4 off
Upon pressing Up Arrow
Activate Group Move Up
Deactivate Group Move Right
Deactivate Group Move Left
Deactivate Group Move Down
Character:
Toggle internal flag 3
Set internal flag 1 off
Set internal flag 2 off
Set internal flag 4 off
Upon pressing Down Arrow
Activate Group Move Down
Deactivate Group Move Right
Deactivate Group Move Left
Deactivate Group Move Up
Character:
Toggle internal flag 4
Set internal flag 1 off
Set internal flag 2 off
Set internal flag 3 off
We toggle the flag so that if the user presses the Right Arrow Key again it stops the character by turning Flag 1 off.The reason we are deactivating unneeded groups and flags is so that the character doesn't get confused, and move in only one direction at a time.
Save your application as AssociativeArrayTutorial2.
Move Right Group Events
The first event in the Move Right Group Events will tell the Character if my Flag 1 is on, move me 1 pixel to the right. If my Flag 1 is off, stop me from moving:
Character internal flag 1 is on
Character:
Set X position to X ("character")+1
The next event will be an expansion event so that if you want to add to your application, the event will be in place. It backs the character out of an obstacle and sets Flag 1 off:
Character overlapping obstacle
Character:
Set X position to X ("character")-1
Turn internal flag 1 off
You don't want your character to walk off the screen, so add this event:
Character is getting closer than 5 pixels from Window's edge
Character:
Set X position to X ("character")-1
Turn internal flag 1 off
A word of warning here. If you use an animated character it may become stuck if it turns, as this will place it inside an obstacle or closer than 5 pixels from the edge of your screen. All you need to do is Always set your animated character to the current active you are using for movement, and at the Start of the frame set your current movable active to invisible.
This event tells the program, if the characters internal flag 1 is off, deactivate Group Event Move Right:
Character internal flag 1 is off
Deactivate group "move right"
This is all the events for Move Right Group Events
Save your application and test it. Your character should start moving right when you press the Right Arrow Key and stop when you press it again.
Move Left Group Events
We will add the events to the Move Left Group Events and they will have your character move to the left.
Character internal flag 2 is on
Character:
Set X position to X ("character")-1
The next event will be an expansion event so that if you want to add to your application, the event will be in place. It backs the character out of an obstacle and sets Flag 2 off:
Character overlapping obstacle
Character:
Set X position to X ("character")+1
Turn internal flag 2 off
You don't want your character to walk off the screen, so add this event:
Character is getting closer than 5 pixels from Window's edge
Character:
Set X position to X ("character")+1
Turn internal flag 2 off
This event tells the program, if the characters internal flag 2 is off, deactivate Move Left Group Events:
Character internal flag 2 is off
Deactivate group "move left"
This is all the events for Move Left Group Events.
Save your application and test it. Your character should start moving left when you press the Left Arrow Key and stop when you press it again.
Move Up Group Events
We will add the events to the Move Up Group Events and they will have your character move to the up.
Character internal flag 3 is on
Character:
Set Y position to Y ("character")-1
The next event will be an expansion event so that if you want to add to your application, the event will be in place. It backs the character out of an obstacle and sets Flag 3 off:
Character overlapping obstacle
Character:
Set Y position to Y ("character")+1
Turn internal flag 3 off
You don't want your character to walk off the screen, so add this event:
Character is getting closer than 5 pixels from Window's edge
Character:
Set Y position to Y ("character")+1
Turn internal flag 3 off
This event tells the program, if the characters internal flag 3 is off, deactivate Move Up Group Events:
Character internal flag 3 is off
Deactivate group "move up"
This is all the events for Move Up Group Events.
Save your application and test it. Your character should start moving up when you press the Up Arrow Key and stop when you press it again.
Move Down Group Events
We will add the events to the Move Down Group Events and they will have your character move to the down.
Character internal flag 4 is on
Character:
Set Y position to Y ("character")+1
The next event will be an expansion event so that if you want to add to your application, the event will be in place. It backs the character out of an obstacle and sets Flag 4 off:
Character overlapping obstacle
Character:
Set Y position to Y ("character")-1
Turn internal flag 4 off
You don't want your character to walk off the screen, so add this event:
Character is getting closer than 5 pixels from Window's edge
Character:
Set Y position to Y ("character")-1
Turn internal flag 4 off
This event tells the program, if the characters internal flag 4 is off, deactivate Move Down Group Events:
Character internal flag 4 is off
Deactivate group "move down"
This is all the events for Move Down Group Events.
Save your application and test it. Your character should start moving down when you press the Down Arrow Key and stop when you press it again.
We should be done with our simple custom movement. Your Character should be able to move in four directions. In the next set of Group Events and Events we will be working with the Associative Array. We will be Saving and Loading the Character's position. You remember the Coin active we added to the frame at the beginning? We will be Saving and Loading values for the Coin. Once the Character picks up the coin we need our Application to remember that it has been collected.
Make a Group Events:
Collect Coin
Collect Coin Group Events
Make a comment:
Get the Coin and set COIN = 1 in the Associative Array
In our first event, when the Character gets the coin we will set a value in the Associative Array:
Character is overlapping Coin
+ Only one action when event loops
Coin:
Destroy
Associative Array:
Set Value at Key "COIN" to 1
The next event will tell the Coin, if the Character has already collected me he can't do it again:
GetValue ("Associative Array", "COIN")=1
+ Only one action when event loops
Coin:
Destroy
These are all the events needed to keep tract of the coin status. Save your Application.
Make a Group Events:
Save
Save Group Events
Make a comment:
Save character position and coin status.
We are going to use the S Key for saving. The first event in the Save Group Events:
Upon pressing "S"
Associative Array:
Set Value at Key "XPOSITION" to X("character")
Set Value at Key "YPOSITION" to Y("character")
Save Associative Array file Apppath$+"example.sav" using method 0
We use Apppath$ so the AA file will be saved in the Application's directory and get in the habit of using all capital letters for your "keys".
In the event above, when the S Key is pressed the Character's X and Y position is saved using made up keys. I used XPOSITION and YPOSITION so that if I come back months later to add to the Application I would know what I would be saving.
Save your Application
Next we will be adding events to Load the saved AA file using the L Key.
Make a Group Events:
Load
Save Group Events
Make a comment:
Load character position and coin status.
In our loading events we will be loading the file via the apppath method so that the Application doesn't have to search too much to find the file, as it will be in the Application directory. You will notice that there are two Pressing L Key events. This is done so that the value will be loaded after the file is loaded. The file has to be loaded in order for values can be retrieved from the AA.
Upon pressing "L"
Associative Array:
Load Associative Array file Apppath$+"example.sav" using method 0
Upon pressing "L"
Character:
Set X position to GetValue( "AssArray Object", "XPOSITION")
Set Y position to GetValue( "AssArray Object", "YPOSITION")
Since the Coin status has alredy been written to the AA we do not need to bother with that. These are all the events. Save your application and test the Application. Collect the Coin, press the S Key and exit the Application. Restart it and press the L key. The Character should be placed where you saved and the Coin should not be on the screen.
© 2008-2012 Marvin Hull
|