RGSS3 ARRAYS - LAST ELEMENT IS PROPAGATING THROUGH THE ENTIRE ARRAY.
Posts
Pages:
1
1) This is the relevant RGSS method.
2) This is the test map I'm using, as exported by the Tiled editor.
3) This image is how the test map appears in Tiled.
4) This image is how the test map appears in-game. Note how the concrete tile covers the entire map.
***
I'm having an issue. Up until now, my SRPG project has been using a manually-written array for its tile data, and everything has been functioning splendidly.
However, what I'd much prefer to do is use maps directly from text. My tile editor, Tiled, has an export-to-text function (see link 2 above) from which I can pull my arrays. By converting them into an array format and feeding the data into my engine, I can make my maps externally-editable and save myself heaps of trouble when it comes to creating maps for my game.
Everything has tested out to be functioning correctly. The text is loaded properly, the lines are split into string arrays properly, and the string arrays are converted to integers properly. I know conclusively that I can get the data from the text into my engine. However, when I try to put that data into my global arrays, things go awry.
For some reason, the engine is setting every bottom-level array equal to the last line of the text file's array. Previous lines' data is being overwritten. This occurs a second time when setting the @map array to tiles from the temporary array--the last tile overwrites all the others. This results in the last tile (link 3) covering the entire battlefield, because every tile becomes tile 10 in my tileset.
What would cause this array mistake?
Ask for more info if you need it.
2) This is the test map I'm using, as exported by the Tiled editor.
3) This image is how the test map appears in Tiled.
4) This image is how the test map appears in-game. Note how the concrete tile covers the entire map.
***
I'm having an issue. Up until now, my SRPG project has been using a manually-written array for its tile data, and everything has been functioning splendidly.
However, what I'd much prefer to do is use maps directly from text. My tile editor, Tiled, has an export-to-text function (see link 2 above) from which I can pull my arrays. By converting them into an array format and feeding the data into my engine, I can make my maps externally-editable and save myself heaps of trouble when it comes to creating maps for my game.
Everything has tested out to be functioning correctly. The text is loaded properly, the lines are split into string arrays properly, and the string arrays are converted to integers properly. I know conclusively that I can get the data from the text into my engine. However, when I try to put that data into my global arrays, things go awry.
For some reason, the engine is setting every bottom-level array equal to the last line of the text file's array. Previous lines' data is being overwritten. This occurs a second time when setting the @map array to tiles from the temporary array--the last tile overwrites all the others. This results in the last tile (link 3) covering the entire battlefield, because every tile becomes tile 10 in my tileset.
What would cause this array mistake?
Ask for more info if you need it.
RGSS3 isn't something I usually work with, but after looking it up it seems that you're creating multiple pointers to the same array instead of copying the contents.
Try replacing this line of code:
with either this:
or this:
Try replacing this line of code:
# Set the array equal to this temporary line. array[z][x] = temp
with either this:
# Set the array equal to this temporary line. array[z][x] = temp.dup
or this:
# Set the array equal to this temporary line. array[z][x] = temp.clone
I really hoped it was pointers, because I could fix that just as you described, but that's not it apparently.
I took the time to put in this msgbox command. When it displays output:



...Everything seems fine.
And when I export the first level of @map to a text file (that is, the values of @map) I get... this.
I took the time to put in this msgbox command. When it displays output:



...Everything seems fine.
And when I export the first level of @map to a text file (that is, the values of @map) I get... this.


So what's happening is that, when "@map = array" is performed it.. sets every value in "@map" to whatever the current target in "array" is...?
I have no fucking clue why that would occur.
I can take a look at this tomorrow (Wednesday) night. My ruby's rusty and I don't see the logic problem but if you provide a test input file I'll see if I can find anything.
e: Looking at the code it looks like tiled exports map data as CSVs anyways
e: Looking at the code it looks like tiled exports map data as CSVs anyways
Would CSV be easier? I'm not terribly familiar with any of the non-text formats.
Replace
with
ps fuck you ruby
e:
Er, wait. Checking the output just makes everything give the same line instead of the same element. One sec
array = Array.new(20, Array.new(35, Array.new(35, 0)))
array = Array.new(20) { Array.new(35) { Array.new(35) { 0 } } }
ps fuck you ruby
e:
Er, wait. Checking the output just makes everything give the same line instead of the same element. One sec
I scrolled up and changed the initialization of "@map" to the same format and now everything's fixed. You're a livesaver; this has been stumping me for most of the week!
Hahaha, my end is still propagating the last line throughout @map and I haven't been able to figure out why exactly. Well if its fixed on your end I'm happy because augh I am way more comfortable in C# than I am in Ruby. I also should've mentioned I did the same with @map to prevent any confusion you might've had when I said to replace just array, sorry!
e: Glad to help too :)
e: Glad to help too :)
Pages:
1
















