Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

Working with arrays (MMF)


Airaxen

Recommended Posts

I really wish there was a very friendly database like thing I could use in MMF. But to my knowledge I just have the array.

Before I start ripping my hair out I wanted to ask for some help in how to really interact with the arrays. So I want to ask about the following:

If I want to find something in the array I imagine I would need to use a loop function. But I'm not certain on how to keep track of how many iterations the loop has run for. I want to use the number of iterations of the loop as the driving force to find the right row or something in the database.

I'm assuming that any coordinate can hold either a value or a string, but I just want to make sure of that. Don't want to realize that there's two copies of a coordinate one for strings or values and have that somehow mess me up.

Lastly I wanted to ask how to make an automated way to tell the program to write to an 'empty' row. That is to say to write to the first empty spot regardless of how many rows preceded it.

Well, that should be all, thank you for your time.

Link to comment
Share on other sites

Each "cell" of the array can only hold either a string or a value. A Clickteam Array can be set either to "Value Array" or "Text Array".

I suggest you, that instead of using 2 arrays, one for each mode, just use a text one:

Using the val() function, you can make MMF2 grab the text as a value, so if there's an HP cell with the string "56", you'd normally get "invalid expression." Using val(StrAtXYZ("Array", 0, 0, 0)) you can save up the pain of using two.

About loops, if you for example want to find a "cell" with the string "ABC", you would create a fast loop, and use the Array actions to add 1 to the X index. Once the end of the X dimension is reached, 1 is added to the Y index, and so on.

About the iterations, the CT array supports an "index", in Excel terms that would be the selected "cell". This is useful for comparing the strings/values in the array. You could add a couple of counters and add to them on each loop, though, in case you need to know how many "cells" were searched in total.

NOTE: You'll see that the Array object itself does not have any "Compare" action. For comparing array contents, you use Compare two general values in the Special object.

Assuming you use a 0-based index array and the Z dimension is 1, it would be like this:


+ Condition
--* Action

//Main action to start loop, button click as example
+User clicks "find ABC" button
--*Special: Start "Search" loop -1 times (infinite)

//Adds 1 to the X index if the current "cell" is not ABC
+On Loop "Search"
+current str$( "Array" ) <> "ABC"
--*Array: Add 1 to X index

//stops loop if ABC is found, and does random thing, string as example
+On Loop "Search"
+current str$( "Array" ) = "ABC"
--*Special: Stop Loop "Search"
--*String 1: Set alterable string to "ABC found."

//if the end of the X dimension is reached, and the current cell is not ABC
//add 1 to the Y index and set X to 0
+Array: Index to the X dimension at end
+current str$( "Array" ) <> "ABC"
--*Array: Add 1 to Y index
--*Array: Set X index to 0

//in case ABC isn't found, the Y index will eventually reach the limit.
//This stops the loop in this case
+Array: Index to the Y dimension at end
--*Stop loop "Search"

If you want MMF2 to write on the first empty spot, use the code above, but search just for "", which would be a blank cell. Once you find the cell, you can use the Array action Write string to XYZ, retrieving the index coordinates, or even easier, just use Write to current position, which will store the data in the currently selected cell of the index.

If you use a 3D array (with Z depth), then just add the Z dimension equivalents to the code, like it's with Y.

If you need to, I can make a quick example, or hand you an old array save tutorial I made.

  • Like 1
Link to comment
Share on other sites

Ohh, that's a very helpful explanation. Especially that bit on the val() function. I would really enjoy it if you sent me that array tutorial. I still have trouble grasping the ideas of loops, especially that infinite loop stuff.

And of course some rep to you for the help.

Link to comment
Share on other sites

  • 2 years later...

Greetings from the future!

Alright, I'm playing with arrays in MMF2 for the purpose of experimenting with future optimization... I'd like to know, is there any way to store floating point (decimal) numbers within MMF2's array object? I can't seem to figure out how to do so if it's possible.

The reason I'm raising this dead topic instead of creating a new one is because this helped me to realize how the array object has to be set to integer mode and defaults to... of all things text mode. Screwed up as that is.

I thought about just grabbing an object that'll let you convert to and from floating point to long, but I'd rather not use unnecessary extensions if I can avoid it.

Link to comment
Share on other sites

Excuse my innocence, but what's an array supposed to do anyway? Just store miscellaneous values/strings? Sounds useful, but couldn't one just set up an active with a whole bunch of alterable values like in the Worlds Engine? Sorry if that sounded stupid, but hey... 5-year ditz/noob, as sad as that is.

Link to comment
Share on other sites

You might be able to do it by using... of all things... an array in text mode.

For reference Val() of string is also how you handle floating point numbers in edit boxes and ini files.

So try out text mode: Store your floats as plain text strings, and use Val(StrAtXYZ("Array",0,0,0)) to retrieve. See what you can get with this method.

Note: To store values as strings, use Str$(<value>).

Even though the MMF manual says you should drop two arrays in case you need both value and string management, I always used the text array + Val() expression for everything. I never had any use for a value type MMF2 array, lol.

---------------------------------------------------

@TailsSena: Imagine a Microsoft Excel spreadsheet. Columns, rows, and the places where these intersect are called cells. Arrays are basically that. They're extreeeeeeemely handy for tons of data management. For example an RPG save file, where you could store things in an ordered way, kinda like this:

          0          1          2
0      P1Lv     P1HP     P1MP
1      P2Lv     P2HP     P2MP
2      P3Lv     P3HP     P3MP

Arrays support an "index", which in Excel terms would be the selected cell. You can use expressions for data retrieval, such as instead of manually pointing to 1,0; then 1,1; and then 1,2; to retrieve HP of the characters, you can just use 1,<Player ID>; where PlayerID is an alterable value in the "health bar" object, which allows said bar to display the correct HP.

Using active objects with Alterable Values would be very cumbersome and resource intensive. Remember each active can only hold 26 AVs. For RPGs and other complex games, this won't do, but an array fits the bill. AVs are more useful for engine state storing (such as the case with Worlds), and values that are independent on each copy of an object (such as an enemy with a health AV, if you use AVs for health, hitting one copy of an enemy will reduce health only for that particular copy, and not the others.)

Another advantage is that you can write the array to a file with one simple action. With AVs you would have to make a chain of repetitive events to write each value to an ini file, for example. (Now, if for some reason you want to manually write your array to an ini file, you can use a fastloop to write the current value, increase the index by one, and write the next value. By continuing this loop until the end of the array is reached, you can manually write the array with a very small set of actions. Same goes for "loading" the array back.)

Also, arrays are handy for board game engines (such as chess). Manage spaces through a 2D array, calculate possible movements with ease, and easily represent the graphics you need through the values you store in your array. Pretty handy <3

Link to comment
Share on other sites

That 'works', but it's totally inefficient. The values are converted to a string representing the float value when being put into the array, then when you want to read them, you have to decode them back into a floating point value... it's just not ideal.

EDIT: I did figure out though that you can store the value as binary data within the string and it should be the actual float value at that point... but I have no idea how to recover it at that point.

EDIT2: No wait, that (referring to binary data in strings) really didn't work. It's still storing the integer values instead of the float values.

Link to comment
Share on other sites

@TailsSena: Imagine a Microsoft Excel spreadsheet. Columns, rows, and the places where these intersect are called cells. Arrays are basically that. They're extreeeeeeemely handy for tons of data management. For example an RPG save file, where you could store things in an ordered way, kinda like this:

          0          1          2
0      P1Lv     P1HP     P1MP
1      P2Lv     P2HP     P2MP
2      P3Lv     P3HP     P3MP

Arrays support an "index", which in Excel terms would be the selected cell. You can use expressions for data retrieval, such as instead of manually pointing to 1,0; then 1,1; and then 1,2; to retrieve HP of the characters, you can just use 1,<Player ID>; where PlayerID is an alterable value in the "health bar" object, which allows said bar to display the correct HP.

Using active objects with Alterable Values would be very cumbersome and resource intensive. Remember each active can only hold 26 AVs. For RPGs and other complex games, this won't do, but an array fits the bill. AVs are more useful for engine state storing (such as the case with Worlds), and values that are independent on each copy of an object (such as an enemy with a health AV, if you use AVs for health, hitting one copy of an enemy will reduce health only for that particular copy, and not the others.)

Another advantage is that you can write the array to a file with one simple action. With AVs you would have to make a chain of repetitive events to write each value to an ini file, for example. (Now, if for some reason you want to manually write your array to an ini file, you can use a fastloop to write the current value, increase the index by one, and write the next value. By continuing this loop until the end of the array is reached, you can manually write the array with a very small set of actions. Same goes for "loading" the array back.)

Also, arrays are handy for board game engines (such as chess). Manage spaces through a 2D array, calculate possible movements with ease, and easily represent the graphics you need through the values you store in your array. Pretty handy <3

Aha. Sounds alright. Not sure what I'd need it for at the moment, but looks cool.

Link to comment
Share on other sites

Well, in my case, I was trying to use it to store a set of precalculated values for the sin function (and getting cos using an array of sin is trivial since you can just shift the index by 90 spots)

Theoretically it should have been a considerable boost, but if I have to convert from string to double ever time, it's a lost cause. Very frustrating.

I checked a little while ago to see whether calculating sin on the fly or trying to convert this shit was more expensive, and I ended up finding that using the array and converting from string every time actually made the performance considerably worse.

EDIT: Good news everyone!

I've figured out that you can store the floating point numbers into the integers in an array using an extension (not desirable, but not horrible) and that does it simply by pretending one set of bytes is of the other type. Simple as can be really.

Bad news though...

No particularly discernible performance increase from having a sin table. I guess MMF is probably either using a cheap approximation or generating its own index on the fly. Regardless, I'm not going to be able to squeeze a lot out of this one.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...