Sincere request for education or at the very least some pointers in the right direction or reading material:
Hi all, I'm very new to RPG Maker but have been working hard at it for a few weeks. I've finally ended up here after a lot of lurking, learning, and trying to solve things myself. There are so many helpful posts! I am a beginner with JS and don't have an enormous comp sci background, so please bear with me when possible. I have really been trying to upgrade and round out my knowledge, though.
Background: I've been trying to create a simple storage system in my game project- turning an event into a container for some items. I am trying to keep everything simple, direct, and explicitly written. I'm avoiding things that seem too advanced because I won't be able to service them well. I settled on using arrays to make these containers. There were a lot of unexpected pitfalls, mainly stemming from turning variables into objects/arrays and then manipulating or copying those arrays in various ways. Once I learned how to view and use the console, and to use console.log() in my scripts to see what was really going on in my operations, things that had seemed to give a satisfactory result were revealed to be doing weird things I didn't understand in the console.
Remember, I am trying -not- to get out of my depth here.
This led me down a whole chain of reading about object references, shallow cloning vs. deep cloning, how to copy arrays without just making another reference to the same object, the spread (...) syntax, the slice method, Object.create(), JSON.parse(JSON.stringify()), etc. etc. Stuff that honestly, I can read about and use, but not understand the full implications of. Things that would make it difficult for me to expand on the foundation I created.
Where I'm at right now and what's causing a problem for me: I am working on a Sort feature for my storage container (they are small, not too many slots) that finds and grabs any "Empty" slots in the container (read: the array) and throws them all toward the back of the array, leaving all your items stacked in a row at the front of the container (the array). After some weird behavior, I made a small Test event just to test the basic idea on a small scale.
Here's the idea/algorithm (beginner level!): You use Control Variable to declare a game variable as a new Array([7, 7, "first"]). Call it $gV181. But we want "first" to be first, and the 7's to follow it at the back. We start a script, and declare an empty "memory array" to hold the 7 values. Using a for loop, we comb through $gV181, and if we find a 7, we push() that into the memory array. Then using splice(), we remove the 7 from $gV181. When the for loop ends, $gV181 is just ["first"], and the memory array is [7, 7]. While using setValue, we concatenate the two arrays, and $gv181 becomes ["first", 7, 7]. Then the script ends, the memory array essentially goes away, and we have $gv181 the way we want it.
Here's the problem- at the end of the script, $gV181 seems to be correct, as predicted, revealed by console.log()- ["first", 7, 7]. But.. I am also logging the memory array and $gV181 throughout the script, and when the script starts, the console logs them as though they have both already been partially manipulated. This is extremely confusing to me. I will show you. I realize the writing may be in some places superfluous but I am trying to keep things explicit and simple:
Within the events sheet, Control Variables: #0181 arrayConcatTest = new Array(7, 7, "first");
then, script:
According to the console, things end up fine, but the logs at the beginning make no sense. If you look at the attached picture, you can see that the two arrays appear to be normal, 3 length and [7, 7, "first"] for Var 181 and just an empty array of length 0 for the other one. However, if you open the dropdown menus, you see that Var 181 is logging as an array of length 1 with element 0 as "first", and the memory array shows as length of 2 with the two elements being the value 7. It is as though they had both been partially manipulated by the for loop, before the final concatenation??
Running the script with the for loop and everything below it commented out printed the logs on the right- the logs I would expect. Length 3, [7, 7, "first"] and length 0, [].
Can someone please talk to me about what's happening in my game? And why? Does the engine try to evaluate the script in some way before running it, as a failsafe, so it doesn't make the engine poo itself or something, and that's messing with the object references before the script is totally run, or something to that effect?
Is this time wizardry? Is this the quantum superposition of variable 181? I can learn how to choreograph move routes at 60 hz. I can read about notetags. I can learn about variable scope. But I think time wizardry is a little bit beyond my grasp.
This concerns me regarding my ability to correctly interpret the logs, and thereby properly debug my game and make it safe to other users so it doesn't break on them, or if it does, I'll know how to fix it. Please help if you can, and remember I'm a beginner so I may not fully understand your references. Thank you so much in advance.
Hi all, I'm very new to RPG Maker but have been working hard at it for a few weeks. I've finally ended up here after a lot of lurking, learning, and trying to solve things myself. There are so many helpful posts! I am a beginner with JS and don't have an enormous comp sci background, so please bear with me when possible. I have really been trying to upgrade and round out my knowledge, though.
Background: I've been trying to create a simple storage system in my game project- turning an event into a container for some items. I am trying to keep everything simple, direct, and explicitly written. I'm avoiding things that seem too advanced because I won't be able to service them well. I settled on using arrays to make these containers. There were a lot of unexpected pitfalls, mainly stemming from turning variables into objects/arrays and then manipulating or copying those arrays in various ways. Once I learned how to view and use the console, and to use console.log() in my scripts to see what was really going on in my operations, things that had seemed to give a satisfactory result were revealed to be doing weird things I didn't understand in the console.
Remember, I am trying -not- to get out of my depth here.
This led me down a whole chain of reading about object references, shallow cloning vs. deep cloning, how to copy arrays without just making another reference to the same object, the spread (...) syntax, the slice method, Object.create(), JSON.parse(JSON.stringify()), etc. etc. Stuff that honestly, I can read about and use, but not understand the full implications of. Things that would make it difficult for me to expand on the foundation I created.
Where I'm at right now and what's causing a problem for me: I am working on a Sort feature for my storage container (they are small, not too many slots) that finds and grabs any "Empty" slots in the container (read: the array) and throws them all toward the back of the array, leaving all your items stacked in a row at the front of the container (the array). After some weird behavior, I made a small Test event just to test the basic idea on a small scale.
Here's the idea/algorithm (beginner level!): You use Control Variable to declare a game variable as a new Array([7, 7, "first"]). Call it $gV181. But we want "first" to be first, and the 7's to follow it at the back. We start a script, and declare an empty "memory array" to hold the 7 values. Using a for loop, we comb through $gV181, and if we find a 7, we push() that into the memory array. Then using splice(), we remove the 7 from $gV181. When the for loop ends, $gV181 is just ["first"], and the memory array is [7, 7]. While using setValue, we concatenate the two arrays, and $gv181 becomes ["first", 7, 7]. Then the script ends, the memory array essentially goes away, and we have $gv181 the way we want it.
Here's the problem- at the end of the script, $gV181 seems to be correct, as predicted, revealed by console.log()- ["first", 7, 7]. But.. I am also logging the memory array and $gV181 throughout the script, and when the script starts, the console logs them as though they have both already been partially manipulated. This is extremely confusing to me. I will show you. I realize the writing may be in some places superfluous but I am trying to keep things explicit and simple:
Within the events sheet, Control Variables: #0181 arrayConcatTest = new Array(7, 7, "first");
then, script:
console.log("Var 181 is currently:");
console.log($gameVariables.value(181));
var memoryArray = new Array();
console.log("var memoryArray is currently:");
console.log(memoryArray);
for (var i = 0; i < $gameVariables.value(181).length; i++) {
if ($gameVariables.value(181)[i] == 7) {
memoryArray.push($gameVariables.value(181)[i]);
$gameVariables.value(181).splice(i, 1);
console.log("Var 181 is currently:");
console.log($gameVariables.value(181));
i = i - 1;
}
}
console.log("Var 181 is currently:");
console.log($gameVariables.value(181));
console.log("var memoryArray is currently:");
console.log(memoryArray);
$gameVariables.setValue(181, $gameVariables.value(181).concat(memoryArray));
console.log("Var 181 is now:");
console.log($gameVariables.value(181));
console.log("var memoryArray is now:");
console.log(memoryArray);
According to the console, things end up fine, but the logs at the beginning make no sense. If you look at the attached picture, you can see that the two arrays appear to be normal, 3 length and [7, 7, "first"] for Var 181 and just an empty array of length 0 for the other one. However, if you open the dropdown menus, you see that Var 181 is logging as an array of length 1 with element 0 as "first", and the memory array shows as length of 2 with the two elements being the value 7. It is as though they had both been partially manipulated by the for loop, before the final concatenation??
Running the script with the for loop and everything below it commented out printed the logs on the right- the logs I would expect. Length 3, [7, 7, "first"] and length 0, [].
Can someone please talk to me about what's happening in my game? And why? Does the engine try to evaluate the script in some way before running it, as a failsafe, so it doesn't make the engine poo itself or something, and that's messing with the object references before the script is totally run, or something to that effect?
Is this time wizardry? Is this the quantum superposition of variable 181? I can learn how to choreograph move routes at 60 hz. I can read about notetags. I can learn about variable scope. But I think time wizardry is a little bit beyond my grasp.
This concerns me regarding my ability to correctly interpret the logs, and thereby properly debug my game and make it safe to other users so it doesn't break on them, or if it does, I'll know how to fix it. Please help if you can, and remember I'm a beginner so I may not fully understand your references. Thank you so much in advance.