Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Array variables

code:
<<set $array = ["foo", "bar", "quux"]>>
<<set $i = 2>>

<<print $array>>
<<print $i>>
<<print $array[$i]>>
output:
foo,bar,quux
2
<<print>> bad expression: Can't find variable: $i
I assume this is because of the changes made to work with jQuery and its use of $, or something like that, but I use arrays and array index variables in a lot of my twine stuff and this change breaks large parts of my code. :(

Is there a way to make this work again?

Comments

  • This works now, great. But I am experimenting with a bit more in depth arrays, I want to be able to make a map.
    I have an example below just using words to make them unique just modifying the example i found from
    http://stackoverflow.com/questions/4943633/creating-and-parsing-a-3d-array-in-javascript on that page scroll down a little bit to find this.
    var myArr = new Array();
    myArr[0] = new Array("Val", "Va");
    myArr[1] = new Array("Val", "yo");
    myArr[2] = new Array("Val", "Val");

    alert(myArr[1][1]); // Alerts 'yo'
    Then modified it to work in Twine, after a few failed attempts to get it right i got it working (in sugarcane) with;
    <<set $myArr =[]>>
    <<set
    $NewArray1 =["Plate", "Fate"];
    $NewArray2 =["Valet", "Yo"];
    $NewArray3 =["Wall", "Pal"]>>

    <<set $myArr[0] = $NewArray1>>
    <<set $myArr[1] = $NewArray2>>
    <<set $myArr[2] = $NewArray3>>

    <<print $myArr[1][1]>>
    Since array() isn't a built in twine function I had to declare NewArray with a number for each one.

    Going a little more bigger..perhaps this could even be more simplified I don't know?
    <<set $myArr =[]>>
    <<set
    $NewArray0 =["Plate", "Fate", "Sate", "Nate"];
    $NewArray1 =["Valet", "Yo", "Bowl", "Phone"];
    $NewArray2 =["Wall", "Pal", "Nail", "Book"];
    $NewArray3 =["Sell", "Tell", "Chair", "Table"];
    $NewArray4 =["Ball", "Pail", "Keys", "Cup"];
    >>

    <<set
    $myArr[0] = $NewArray0;
    $myArr[1] = $NewArray1;
    $myArr[2] = $NewArray2;
    $myArr[3] = $NewArray3;
    $myArr[4] = $NewArray4
    >>

    <<print $myArr[4][3]>>
    The above outputs "cup".

    Now how do I list all of those string arrays into one variable $Wordlist, I know how to do this for a simple array but what about something for the above example a 3d array? or is this really still a 2d array? (according to some comments on the stackoverflow forum page listed at the top this type of array is still 2d.)

    Remember this is mainly for a map, okay probably in hindsight it would of been better for this example to have used "loc1", "loc2" instead of words, but if you imagine the width is the length of the arrays across which is 4 and this can be X and there being five rows being Y. I would like to track where the player is in that map so to speak with x,y and if the player chooses a navigation direction his/her position is updated on this map. Is there a better way to do this?

    I also would like to print on the screen the map the same way the table of arrays looks in the passage code where I set them 4 across, 5 down? this would be a similar as to putting them them all into one variable.

    Okay my brain is starting to close doors on me now.  :o

    Thanks for any suggestions.
  • Dazakiwi38 wrote:

    Then modified it to work in Twine, after a few failed attempts to get it right i got it working (in sugarcane) with;



    Since array() isn't a built in twine function I had to declare NewArray with a number for each one.


    Array is a JavaScript built-in, which means that it's a Twine built-in too.  If you tried it and had an issue, check your spelling (JavaScript is case sensitive).

    This works as expected:

    <<set $myArr = new Array()>>
    <<set
    $myArr[0] = new Array("Plate", "Fate");
    $myArr[1] = new Array("Valet", "Yo");
    $myArr[2] = new Array("Wall", "Pal");
    >>
    <<print $myArr[1][1]>>
    /% Prints: Yo %/
    That said, if you're just going to append arrays to the base array, then I'd suggest either this:

    <<set $myArr = []>>
    <<set
    $myArr.push(["Plate", "Fate"]),
    $myArr.push(["Valet", "Yo"]),
    $myArr.push(["Wall", "Pal"])
    >>
    Or simply this:

    <<set
    $myArr =
    [
    ["Plate", "Fate"],
    ["Valet", "Yo"],
    ["Wall", "Pal"]
    ]
    >>

    Dazakiwi38 wrote:

    Now how do I list all of those string arrays into one variable $Wordlist, I know how to do this for a simple array but what about something for the above example a 3d array? or is this really still a 2d array? (according to some comments on the stackoverflow forum page listed at the top this type of array is still 2d.)


    1. Your examples are of 2D arrays, definitely not 3D arrays.  (To nitpick, technically, your examples are neither.  JavaScript only supports 1d arrays, so what you really have are nested 1D arrays, simulating a 2D array.)

    // 1D array
    [ "a", "b" ]

    // 2D array
    [
    [ "a" ],
    [ "b" ]
    ]
    2. Assuming you meant, how do merge your 2D array into a 1D array, then you could do so like this:

    <<set $Wordlist = []>>
    <<set
    for (var i = 0; i < $myArr.length; i++)
    {
    $Wordlist = $Wordlist.concat($myArr[i]);
    }
    >>
    If you wanted a string, you could simply .join() the 1D array at that point.


    Dazakiwi38 wrote:

    I would like to track where the player is in that map so to speak with x,y and if the player chooses a navigation direction his/her position is updated on this map. Is there a better way to do this?


    What you're doing is a fairly standard way of handling a 2D map.


    Dazakiwi38 wrote:

    I also would like to print on the screen the map the same way the table of arrays looks in the passage code where I set them 4 across, 5 down?


    You could always programmatically create an HTML table from the 2D array.
  • Thanks MadExile for your reply and clearing a few things up, very informative. :)

    Btw what is the general rule of using () or [] when concerning variables? Do they both do the same thing?

    I noticed in your example of appending arrays eg
    $myArr.push(["Plate", "Fate"]),
    you placed the variables inside square brackets with () on the outside and the set array =[] defined at the top of that example with square brackets.
    Just wanting to clarify this in my head.

    Thanks again.


  • Dazakiwi38 wrote:

    Btw what is the general rule of using () or [] when concerning variables? Do they both do the same thing?


    Break down of the following line of code:
    $myArr.push(["Plate", "Fate"]),
    1. $myArr is variable which contain an array set early via "<<set $myArr = new Array()>>"

    2. The curved braces () indicate that push is a function, the preceding full stop indicates it is a member function of the array object stored in the $myArr variable.

    3. The square brackets [] indicate an array literal, which in the example has two elements "Plate" and "Fate". This array is being passed as a parameter to the push function.

    4. End result: the push  function is being used to add the ["Plate", "Fate"] array literal to the end of array stored within the $myArr variable.

    I hope that makes it a little clearer.
  • Yes that makes it more clearer greyelf thanks for that.

    So as a general rule () is for functions and [] for arrays? got it.

    [Quote]2. Assuming you meant, how do merge your 2D array into a 1D array, then you could do so like this:
    Code: [Select]

    <<set $Wordlist = []>>
    <<set
    for (var i = 0; i < $myArr.length; i++)
    {
    $Wordlist = $Wordlist.concat($myArr[i]);
    }
    >>


    If you wanted a string, you could simply .join() the 1D array at that point.[/Quote]
    $myArr[0] = new Array("Loc1", "Loc2", "Loc3", "Loc4");
    $myArr[1] = new Array("Loc5", "Loc6", "Loc7", "Loc8");
    $myArr[2] = new Array("Loc9", "Loc10", "Loc11", "Loc12");
    $myArr[3] = new Array("Loc13", "Loc14", "Loc15", "Loc16");
    $myArr[4] = new Array("Loc17", "Loc18", "Loc19", "Loc20");
    I want to be able to print my map out on the screen 4 x 5

    This is getting a little complex, because we have 5 arrays which the has 4 variables within.
    I want to be able to output the first myArr[0] then new line then next array myArr[1] etc

    How would you do this?
  • Dazakiwi38 wrote:

    I want to be able to print my map out on the screen 4 x 5

    This is getting a little complex, because we have 5 arrays which the has 4 variables within.
    I want to be able to output the first myArr[0] then new line then next array myArr[1] etc

    How would you do this?


    It's not really that difficult, you simply have to iterate over the array's axes.

    As an example, to produce a 45 table, you could do something like this:

    (function () {
    "use strict";

    version.extensions["renderMapMacro"] = { major: 1, minor: 0, revision: 0 };
    macros["renderMap"] = {
    handler: function (place, macroName, params, parser)
    {
    var map = state.history[0].variables["myArr"]
    , table = document.createElement("table")
    , tbody = document.createElement("tbody");
    for (var y = 0; y < map.length; y++)
    {
    var tr = document.createElement("tr");
    for (var x = 0; x < map[y].length; x++)
    {
    var td = document.createElement("td");
    insertText(td, map[y][x]);
    tr.appendChild(td);
    }
    tbody.appendChild(tr);
    }
    table.appendChild(tbody);
    place.appendChild(table);
    }
    };

    }());
    That's just a skeleton, but you should get the idea.
  • TheMadExile wrote:

    It's not really that difficult, you simply have to iterate over the array's axes.

    As an example, to produce a 45 table, you could do something like this:

    (function () {
    "use strict";

    version.extensions["renderMapMacro"] = { major: 1, minor: 0, revision: 0 };
    macros["renderMap"] = {
    handler: function (place, macroName, params, parser)
    {
    var map = state.history[0].variables["myArr"]
    , table = document.createElement("table")
    , tbody = document.createElement("tbody");
    for (var y = 0; y < map.length; y++)
    {
    var tr = document.createElement("tr");
    for (var x = 0; x < map[y].length; x++)
    {
    var td = document.createElement("td");
    insertText(td, map[y][x]);
    tr.appendChild(td);
    }
    tbody.appendChild(tr);
    }
    table.appendChild(tbody);
    place.appendChild(table);
    }
    };

    }());
    That's just a skeleton, but you should get the idea.


    Thanks again for your response. However, now I getting over my head lol. But that's okay that is how one learns. 
    So this function script creates a custom macro you named RenderMap? how is this called/used in a passage that I want to output the map on the screen? Once I get this working I can tinker with the script and learn how it works which is sometimes the best way to learn. Thanks for your help.



  • Dazakiwi38 wrote:

    So this function script creates a custom macro you named RenderMap? how is this called/used in a passage that I want to output the map on the screen?


    Yes, though it's named renderMap (case is important).

    The current skeleton takes no arguments, as the $variable name ($myArr) is hard-coded, so you'd use it like so:
    <<renderMap>>
    You will, obviously, want/need to tinker with it.
  • TheMadExile wrote:

    Dazakiwi38 wrote:

    So this function script creates a custom macro you named RenderMap? how is this called/used in a passage that I want to output the map on the screen?


    Yes, though it's named renderMap (case is important).

    The current skeleton takes no arguments, as the $variable name ($myArr) is hard-coded, so you'd use it like so:
    <<renderMap>>
    You will, obviously, want/need to tinker with it.


    Cool thanks for that, it works marvelously! 

  • I probably could/should have embedded some commentary in that.  Let me know if you have any questions.
  • TheMadExile wrote:

    I probably could/should have embedded some commentary in that.  Let me know if you have any questions.


    A commentary would be very handy and also for other newbies who might want to use this themselves.

    I do have one question, how would I make the current "myArr" variable red or green, so that on the map output it will
    be colour indicated as to where the player is on the map. Aside from not knowing how exactly, i would guess you would need a condition inside the loop to instance once for the colour change when current location is matched and then cancel out and the loop continues runs the normal colour for the rest of the array length?

    This is what I have so far with amending the map to have a crude table like look.
    (function () {
    "use strict";

    version.extensions["renderMapMacro"] = { major: 1, minor: 0, revision: 0 };
    macros["renderMap"] = {
    handler: function (place, macroName, params, parser)
    {
    var map = state.history[0].variables["myArr"]
    , table = document.createElement("table")
    , tbody = document.createElement("tbody");

    for (var y = 0; y < map.length; y++)
    {

    var tr = document.createElement("tr");
    for (var x = 0; x < map[y].length; x++)
    {
    var front = document.createElement("front");
    insertText(front, " ");
    tr.appendChild(front);
    var td = document.createElement("td");
    insertText(td, map[y][x]);
    tr.appendChild(td);

    }
    tbody.appendChild(tr);
    }
    table.appendChild(tbody);
    place.appendChild(table);
    }
    };

    }());
    The out come is combining this =---------= in the passage that calls the function...nothing fancy but results in...


    [URL=http://s157.photobucket.com/user/Daza_07/media/CrudeMap.jpg.html][IMG]http://i157.photobucket.com/albums/t60/Daza_07/CrudeMap.jpg[/img][/URL]

    This is just a proof of concept and the location names would be more specific to reflect the type of map it is, such as an over world map or it might be a single location like a building or city block etc.  Thinking about it, once I know how to add colours to some of them i could have say blue for locations that indicate they are connected to a level above or below it.  If it is possible I guess you could make the locations clickable links if you wanted to have like a star-travel map to travel to those locations or this is could be used for a visual inventory.
  • Dazakiwi38 wrote:

    I do have one question, how would I make the current "myArr" variable red or green, so that on the map output it will
    be colour indicated as to where the player is on the map. Aside from not knowing how exactly, i would guess you would need a condition inside the loop to instance once for the colour change when current location is matched and then cancel out and the loop continues runs the normal colour for the rest of the array length?


    How are you recording the player's location?  Regardless, assuming that you have a $variable for the player's location, you'd have a conditional test inside the X-loop which would check the player's location against the current map location, styling the current map cell if they match (there's no need to cancel anything, the styling code only fires when the condition is true).

    As to how to style the player's location and/or style locations in general, I'd use simply have it add appropriate classes to each cell and style them via CSS.

    On a different topic, I see that you're creating non-standard elements (i.e. document.createElement("front")).  As a general rule, I would suggest not doing that.  You're also inserting it into the table record element, instead of the associated table data cell element, definitely don't do that.

    Let me know how you're recording the player's location and I can provide an updated example.
  • TheMadExile wrote:

    Dazakiwi38 wrote:

    I do have one question, how would I make the current "myArr" variable red or green, so that on the map output it will
    be colour indicated as to where the player is on the map. Aside from not knowing how exactly, i would guess you would need a condition inside the loop to instance once for the colour change when current location is matched and then cancel out and the loop continues runs the normal colour for the rest of the array length?


    How are you recording the player's location?  Regardless, assuming that you have a $variable for the player's location, you'd have a conditional test inside the X-loop which would check the player's location against the current map location, styling the current map cell if they match (there's no need to cancel anything, the styling code only fires when the condition is true).

    As to how to style the player's location and/or style locations in general, I'd use simply have it add appropriate classes to each cell and style them via CSS.

    On a different topic, I see that you're creating non-standard elements (i.e. document.createElement("front")).  As a general rule, I would suggest not doing that.  You're also inserting it into the table record element, instead of the associated table data cell element, definitely don't do that.

    Let me know how you're recording the player's location and I can provide an updated example.


    Yeah elements, tables is what I need to read up on. I definitely want to be able to make my own functions in the future so at least now I got a starting point.

    I was thinking of uploading the demo anyways. So you can see exactly how I am doing it, although the last position is still stored in $myArr it only updates when the player chooses a new direction.

    Demo Version 1:
    https://www.mediafire.com/?nkovbeabnjo75a3
  • Okay, good, you're using X/Y-coordinates, that makes this easy.  Although, you've got your axes backwards (if this is supposed to be a standard Cartesian coordinate system, which is what I assumed, X is horizontal and Y is vertical, not that it really matters which you go with).

    Anyway, I fixed the symbol snafu you had going on and wrapped it in a span so it could be targeted separately.  I also updated the code to refer to the map in terms of rows and columns, so you can use whatever orientation of X/Y you want (I did make it match your current setup though, so you don't have to change anything).  Additionally, the map table has an ID of map and the cell where the player is located will contain the class player.  Oh, and comments happened.

    I'm attaching an updated version of your demo, which includes the new code as well as a stylesheet showing some basic styling.

    I'll also include the new code here as well (for no real reason, lol):

    (function () {
    "use strict";

    version.extensions["renderMapMacro"] = { major: 1, minor: 0, revision: 0 };
    macros["renderMap"] = {
    handler: function (place, macroName, params, parser)
    {
    var map = state.history[0].variables["myArr"] // create a reference to $myArr
    , pcRow = state.history[0].variables["X"] // create a reference to $X
    , pcCol = state.history[0].variables["Y"] // create a reference to $Y
    , table = document.createElement("table") // create the table element
    , tbody = document.createElement("tbody"); // create the table body element

    // add the ID "map" to the table
    table.id = "map";

    // iterate over the map's rows
    for (var row = 0; row < map.length; row++)
    {
    var tr = document.createElement("tr"); // create a table row element

    // iterate over the map's columns
    for (var col = 0; col < map[row].length; col++)
    {
    var symbol = document.createElement("span") // create a span element to hold the symbol(?)
    , td = document.createElement("td"); // create a table data cell element

    // append the symbol(?) to the span
    insertText(symbol, " ");
    // append the span to the cell
    td.appendChild(symbol);

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col]);
    // if the current coordinates match the player location, add the "player" class to the cell
    if (row === pcRow &amp;&amp; col === pcCol) { td.classList.add("player"); }
    // append the cell to the row
    tr.appendChild(td);

    }
    // append the row to the table body
    tbody.appendChild(tr);
    }
    // append the table body to the table
    table.appendChild(tbody);
    // append the table to the passage
    place.appendChild(table);
    }
    };

    }());
  • TheMadExile wrote:

    Okay, good, you're using X/Y-coordinates, that makes this easy.  Although, you've got your axes backwards (if this is supposed to be a standard Cartesian coordinate system, which is what I assumed, X is horizontal and Y is vertical, not that it really matters which you go with).

    Anyway, I fixed the symbol snafu you had going on and wrapped it in a span so it could be targeted separately.  I also updated the code to refer to the map in terms of rows and columns, so you can use whatever orientation of X/Y you want (I did make it match your current setup though, so you don't have to change anything).  Additionally, the map table has an ID of map and the cell where the player is located will contain the class player.  Oh, and comments happened.

    I'm attaching an updated version of your demo, which includes the new code as well as a stylesheet showing some basic styling.


    Thanks for all your help MadExile and updating the demo. It looks much better with the update to the function script and stylesheet, both has
    plenty to mine into and learn more, along with the comments you added.

    As for the axes coordinates I got my wires crossed for sure  ???, but as you say it doesn't really matter lol.


  • TheMadExile wrote:

    Okay, good, you're using X/Y-coordinates, that makes this easy.  Although, you've got your axes backwards (if this is supposed to be a standard Cartesian coordinate system, which is what I assumed, X is horizontal and Y is vertical, not that it really matters which you go with).

    Anyway, I fixed the symbol snafu you had going on and wrapped it in a span so it could be targeted separately.  I also updated the code to refer to the map in terms of rows and columns, so you can use whatever orientation of X/Y you want (I did make it match your current setup though, so you don't have to change anything).  Additionally, the map table has an ID of map and the cell where the player is located will contain the class player.  Oh, and comments happened.

    I'm attaching an updated version of your demo, which includes the new code as well as a stylesheet showing some basic styling.

    I'll also include the new code here as well (for no real reason, lol):

    (function () {
    "use strict";

    version.extensions["renderMapMacro"] = { major: 1, minor: 0, revision: 0 };
    macros["renderMap"] = {
    handler: function (place, macroName, params, parser)
    {
    var map = state.history[0].variables["myArr"] // create a reference to $myArr
    , pcRow = state.history[0].variables["X"] // create a reference to $X
    , pcCol = state.history[0].variables["Y"] // create a reference to $Y
    , table = document.createElement("table") // create the table element
    , tbody = document.createElement("tbody"); // create the table body element

    // add the ID "map" to the table
    table.id = "map";

    // iterate over the map's rows
    for (var row = 0; row < map.length; row++)
    {
    var tr = document.createElement("tr"); // create a table row element

    // iterate over the map's columns
    for (var col = 0; col < map[row].length; col++)
    {
    var symbol = document.createElement("span") // create a span element to hold the symbol(?)
    , td = document.createElement("td"); // create a table data cell element

    // append the symbol(?) to the span
    insertText(symbol, " ");
    // append the span to the cell
    td.appendChild(symbol);

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col]);
    // if the current coordinates match the player location, add the "player" class to the cell
    if (row === pcRow &amp;&amp; col === pcCol) { td.classList.add("player"); }
    // append the cell to the row
    tr.appendChild(td);

    }
    // append the row to the table body
    tbody.appendChild(tr);
    }
    // append the table body to the table
    table.appendChild(tbody);
    // append the table to the passage
    place.appendChild(table);
    }
    };

    }());


    I am in the process of converting my map demo as per this thread to sugarcube and I've made progress fixing some of the errors thrown at me so far, its now having a problem with the macroscript MadExile wrote, but its beyond me how to fix it. 
    This is the error
    error: cannot execute macro <<renderMap>>: cannot read property 'length' of undefined
    What do I need to change in the macro script to get this working again?

    Thanks.
  • Dazakiwi38 wrote:

    What do I need to change in the macro script to get this working again?


    You needed to change state.history[0].variables to state.active.variables.

    I made that change as well as changing the macro over to SugarCube's native macro format.  Let me know if you have an issue with it.

    macros.add("renderMap", {
    version: { major: 2, minor: 0, revision: 0 },
    handler: function ()
    {
    var map = state.active.variables["myArr"] // create a reference to $myArr
    , pcRow = state.active.variables["X"] // create a reference to $X
    , pcCol = state.active.variables["Y"] // create a reference to $Y
    , table = document.createElement("table") // create the table element
    , tbody = document.createElement("tbody"); // create the table body element

    // add the ID "map" to the table
    table.id = "map";

    // iterate over the map's rows
    for (var row = 0; row < map.length; row++)
    {
    var tr = document.createElement("tr"); // create a table row element

    // iterate over the map's columns
    for (var col = 0; col < map[row].length; col++)
    {
    var symbol = document.createElement("span") // create a span element to hold the symbol(?)
    , td = document.createElement("td"); // create a table data cell element

    // append the symbol(?) to the span
    insertText(symbol, " ");
    // append the span to the cell
    td.appendChild(symbol);

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col]);
    // if the current coordinates match the player location, add the "player" class to the cell
    if (row === pcRow &amp;&amp; col === pcCol) { td.classList.add("player"); }
    // append the cell to the row
    tr.appendChild(td);
    }
    // append the row to the table body
    tbody.appendChild(tr);
    }
    // append the table body to the table
    table.appendChild(tbody);
    // append the table to the passage
    this.output.appendChild(table);
    }
    });
  • Thanks MadExile that fixed it and it works with no problems. I have now successfully converted over my map demo. :)

  • macros.add("renderMap", {
    version: { major: 2, minor: 0, revision: 0 },
    handler: function ()
    {
    var map = state.active.variables["myArr"] // create a reference to $myArr
    , pcRow = state.active.variables["X"] // create a reference to $X
    , pcCol = state.active.variables["Y"] // create a reference to $Y
    , table = document.createElement("table") // create the table element
    , tbody = document.createElement("tbody"); // create the table body element

    // add the ID "map" to the table
    table.id = "map";

    // iterate over the map's rows
    for (var row = 0; row < map.length; row++)
    {
    var tr = document.createElement("tr"); // create a table row element

    // iterate over the map's columns
    for (var col = 0; col < map[row].length; col++)
    {
    var symbol = document.createElement("span") // create a span element to hold the symbol(?)
    , td = document.createElement("td"); // create a table data cell element

    // append the symbol(?) to the span
    insertText(symbol, " ");
    // append the span to the cell
    td.appendChild(symbol);

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col]);
    // if the current coordinates match the player location, add the "player" class to the cell
    if (row === pcRow &amp;&amp; col === pcCol) { td.classList.add("player"); }
    // append the cell to the row
    tr.appendChild(td);
    }
    // append the row to the table body
    tbody.appendChild(tr);
    }
    // append the table body to the table
    table.appendChild(tbody);
    // append the table to the passage
    this.output.appendChild(table);
    }
    });
    I have a new question. I am trying to streamline how my map system works, and the script above stores an object property called .LocName that is stored in the array Map system, such as below. But this array map system isn't just for the visual map script, but also for tracking where the player is and accessing the properties of that location. So storing a single property value at the time was the only way i knew of making it work. And then later comparing the location data to the object parent it belongs to. But now I am sure there has to be a more efficient way to do this.
    eg.

    Map1::
    <<set
    $myArr[0] = new Array($Loc01.LocName, $Loc02.LocName, $Loc03.LocName, $Loc04.LocName);
    $myArr[1] = new Array($Loc05.LocName, $Loc06.LocName, $Loc07.LocName, $Loc08.LocName);
    $myArr[2] = new Array($Loc09.LocName, $Loc10.LocName, $Loc11.LocName, $Loc12.LocName);
    $myArr[3] = new Array($Loc13.LocName, $Loc14.LocName, $Loc15.LocName, $Loc16.LocName);
    $myArr[4] = new Array($Loc17.LocName, $Loc18.LocName, $Loc19.LocName, $Loc20.LocName);
    $X=4;
    $Y=2;
    >>
    So as said, I had to do a long list of IF condition checks to compare and set the current location object to a current position variable in order to keep the map of where the player is in sync with the visual map. Its a convoluted system yes.  :o
    But sometimes one has to learn the hard way.
    <<if $myArr[$X][$Y] eq $Loc20.LocName>>
    <<set $CurrentPos to $Loc20>>

    It would be much easier if the mapper script was given the Object directly instead, and just grab the LocName property from it within the script to display the name of the location on the map output. This way my main map array system will only store the parent objects and i can access their properties at any time in the game.

    So i want to have in the main map array to be like this storing parent location objects.

    Map1::<<set
    $myArr[0] = new Array($Loc01, $Loc02, $Loc03, $Loc04);
    $myArr[1] = new Array($Loc05, $Loc06, $Loc07, $Loc08);
    $myArr[2] = new Array($Loc09, $Loc10, $Loc11, $Loc12);
    $myArr[3] = new Array($Loc13, $Loc14, $Loc15, $Loc16);
    $myArr[4] = new Array($Loc17, $Loc18, $Loc19, $Loc20);
    $X=4;
    $Y=2;
    >>
    So how in the mapper script can I grab the property .LocName from the parent object stored in myArr and place it in the variable called MAP? or maybe the script has to be further altered to make this work properly? I tried a couple of things, but failed.

    I hope this post makes sense?  ???

  • Dazakiwi38 wrote:
    So how in the mapper script can I grab the property .LocName from the parent object stored in myArr and place it in the variable called MAP?


    You don't need to place anything in map.  The variable map is a reference to $myArr, so you'd access the properties of the objects stored in $myArr through map.

    Assuming you simply want &lt;&lt;renderMap&gt;&gt; to work with the new structure of $myArr.  Then you'd fix it like this:

    FIND:

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col]);
    REPLACE WITH:

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col].LocName);
  • TheMadExile wrote:

    Dazakiwi38 wrote:
    So how in the mapper script can I grab the property .LocName from the parent object stored in myArr and place it in the variable called MAP?


    You don't need to place anything in map.  The variable map is a reference to $myArr, so you'd access the properties of the objects stored in $myArr through map.

    Assuming you simply want &lt;&lt;renderMap&gt;&gt; to work with the new structure of $myArr.  Then you'd fix it like this:

    FIND:

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col]);
    REPLACE WITH:

    // append the map value for the current coordinate to the cell
    insertText(td, map[row][col].LocName);


    That was what i had meant, i didn't word that very well.

    Thanks again, that was the solution I was after.  Brilliant! :)
Sign In or Register to comment.