Game Maker's Garage Forum

Game Creation => Code Exchange => Topic started by: Silverwind on February 17, 2010, 07:15:01 PM

Title: [GM] Arrays
Post by: Silverwind on February 17, 2010, 07:15:01 PM
I was puzzling over how to manage treasure chests in the Roguesoft RPG Engine when I came to the regrettable conclusion that I'd have to use arrays. This bothered me, as the only method I could think up involved FILEIO and I didn't want players tampering with saves.

I suddenly remembered an article Al wrote some time ago and hurried online to read it. Sure enough, Al solved the puzzle ages ago, and the only limitation is consistent value length.

I've rewritten his marvelous routines in my own syntax and swapped the value length from 2 to 1. Here they are should anyone find them useful:

Create an array:
Code: [Select]
arrayName$ = "ABC"

Copy the value of an item to the "itemValue$" variable:
Code: [Select]
'--- Get an item's value.
USERASK Which item do you want to get the value of?
item = val userask$
itemValue$ = mid$ array$ item 1

Change an item's value:
Code: [Select]
'--- Select an item in the array.
USERASK Which item will you assign a new value to?
item = val userask$
itemToLeft = item - 1
itemToRight = item + 1

'--- Designate a new value for the item.
newItemValue$ = USERASK Set the item's new value:

'--- Reconstruct the array with the item's new value.
itemsToLeft$ = left$ array$ itemToLeft
IF item =< 1 THEN itemsToLeft$ = ""
itemsToLeft$ = itemsToLeft$ + newItemValue$
itemsToRight$ = mid$ array$ itemToRight
newArrayValue$ = itemsToLeft$ + itemsToRight$
array$ = newArrayValue$

And check out Al's original article for 2 char length item values:


I've updated the above code to fix an incorrect operator. Also, here's a source example: