MV about creating game objects

hi^^

i am working on a plugin and i would like to know more about creating game objects..

in this post there is a info how to build your own object:
=> #18
-----------

So if i use this : ( i need to use this anyway )
JavaScript:
      function Self_Switches() {
               this.initialize.apply(this, arguments);
      }
      // i mean this part below here!
      $selfSwitches = Object.create(Self_Switches.prototype);
      Self_Switches.prototype.constructor = Self_Switches;

do i still need to use this?:
JavaScript:
var old_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
   old_createGameObjects.call(this);
   // i mean this part below here!
   $selfSwitches = new Self_Switches();
};
sidenote: i allready use "new Self_Switches()", in my plugin @ Game_Event.prototype.initialize
Spoiler: sidenote info
JavaScript:
      // add SelfSwitches data storage to event
      var _Game_event_ini = Game_Event.prototype.initialize;
      Game_Event.prototype.initialize = function(mapId, eventId) {
          _Game_event_ini.call(this, mapId, eventId);
           // i mean this here below!
           this._SelfSwitches = new Self_Switches(mapId, eventId);
      };


or would that even be a bad idea to use both?
( i am not talking about any other functions from post #18 , because they are used anyway )

any info that let me understand this better might help thx!

edit:
in other words , my question is ..

is "DataManager.createGameObjects" only used to initialize the game Object? or is this needed for anything else aswell??
new obj() and Object.create(obj.prototype) are broadly similar. The former calls the objects constructor function though.

As far as I can tell without testing anything, I don't think you'll encounter any problems. $selfSwitches will ultimately be assigned by $selfSwitches = new Self_Switches() as the createGameObjects function will be called after you assign $selfSwitches using Object.create.

Unless you are purposefully trying to avoid calling the objects constructor function or there is some other reason for using Object.create that isn't clear from your question, I would let this happen and get rid of the Object.create line.

EDIT (after I missed your edit while getting tea): createGameObjects just creates new game objects, using new so calling the objects constructor function and as such the initialize function.
new obj() and Object.create(obj.prototype) are broadly similar. The former calls the objects constructor function though.

As far as I can tell without testing anything, I don't think you'll encounter any problems. $selfSwitches will ultimately be assigned by $selfSwitches = new Self_Switches() as the createGameObjects function will be called after you assign $selfSwitches using Object.create.

Unless you are purposefully trying to avoid calling the objects constructor function or there is some other reason for using Object.create that isn't clear from your question, I would let this happen and get rid of the Object.create line.

EDIT (after I missed your edit while getting tea): createGameObjects just creates new game objects, using new so calling the objects constructor function and as such the initialize function.
thx thats what i thought, i just wanted to be sure,..

so in my case DataManager.createGameObjects is not required ,because that happens everytime the Game_Event.prototype.initialize is triggered..


sideNote:
I only want to safe and store the "$selfSwitches" Object, because it contains some data at "$selfSwitches._data"
And i want to be able to use the Object global, to call the plugin functions..

(I will post my plugin as soon i am done so the purpose can be understood better)
=> also in my Plugin i will rename "$selfSwitches" to something shorter ..



edit .
plugin is finished, related thread is waiting for approval,..


this Plugin adds Self Switch storage to Game_Event

Spoiler: example imgs
THIS EXAMPLE DEMO USES 3 MAPS ,BUT ANY AMOUNT OF MAPS SHOULD WORK
Screenshot_2.png

----------------------------------------------------------------
Screenshot_1.png
well i figured out a problem which i have on this plugin, cause of the main object.
(i have the same problem in another plugin but nvm this one is the better example)

When i call any "Self_Switches"- function with my global Var "$ESS" , the function works but, the "this"-data inside the function changes:

sideNote: thats the current setup
"$ESS" is the function calller
"$ESS.BOX" is the var data storage container which should not be availleble by "event._SelfWitches" (= Self_Switches functions)

Img below shows it: ( i called the same function with 2 different ways)
Spoiler: Img
in this example the correct "this"- data is the one from "event._SelfSwitch"

but if using the "$ESS.BOX" as function caller, i get the "this"- data of "$ESS.BOX", transported into the "Self_Switch" function
Screenshot_4.png

- i know thats not a bug, but how can i avoid this?

-> i want the function-class name to be "Self_Switches"

-> and i want the $ESS global Var to be the caller for all "Self_Switches"-functions,..
(ESS should not contain the "Self_Switches"_"this"-data, .. thats why ESS ist not a "new Self_Switch"-object)

=> but i want the "Self_Swichtes" "this"-data to stay its own
=> while i want to use the Global var $ESS as caller & as Variable storage box, which doesnt change the "this"- data of called functions


Can i Store the var Container in "$ESS" while storing the Self_Switch function in "$ESS.BOX"? would something like that help?
( that would be the opposite of the current setup )
- i know thats not a bug, but how can i avoid this?
It looks like you're trying to apply the same function to an array (actually a matrix) as to a specific variable.

I think you should make BOX its own data type and write its own list function that will iterate through the matrix and display the contents in the way you want.


=> while i want to use the Global var $ESS as caller & as Variable storare box, which doesnt change the "this"- data of called functions
The problem here (as best I understand you) is that you're not even storing Self_Switch objects in your BOX matrix.

Well, you kind of are...you're saving the current Self_Switch as $ESS.BOX[mapId][eventId] on line 189, but then you proceed to save the switch values separately as text with...some kind of hash table? Maybe I just don't get what you're trying to do with lines 190-193.

My understanding is that JavaScript doesn't have multi-dimensional arrays, so when you do something like: if ($ESS.BOX[mapId][eventId] === undefined) $ESS.BOX[mapId][eventId] = [];
you're creating an array in that index. I'm not sure how trying to use array functions such as length will work with arrays that have undefined elements.

I would suggest changing your BOX creation to something like
Code:
$ESS.BOX[mapId].push(this);

Then each element of BOX will be an array of Self_Switch objects and you can use the inherent JavaScript array functions to navigate, manipulate, and display them. e.g.:
Code:
$ESS.BOX[mapId].forEach(event => console.log(event.eventId, event.A, event.B, event.C, event.D);

And then any method you call from one of those elements will have the correct this.
Spoiler
i think i didnt explained well enough what $ESS.BOX is..

selfswitches are only true/false data,

i use the Box variables array structure which i builded as kind of data info list.

i can use this list to tell other functions which selfswitch is false or true, that way i can even change the selfSwitchdata..

why i am doing this? i want the "Self_Switch" ( = new Self_Switch) function class, to be added directly to events.

while the $ESS should be the global object that is safed, and $ESS.BOX is the multi-array-storage of my "mapID,eventID,selfswitchValue data" ..

also i want this global safed object ($ESS) to be able to call the functions "Self_Switch.prototype.example = function() {};"

However, i tried another approach by using : ("dopan" is only a placeholder , i didnt want to use the word "call" cause it could have its owm JS data idk)
JavaScript:
 $ESS.dopan = Object.create(Self_Switches.prototype);
instead of $ESS = Object.create(Self_Switches.prototype);

what i try to archieve now is to have the function called by another instance of $ESS which is called "dopan" in this case,..
that way i hope to seperate the different "this.-datas", but now i get :
"$ESS" is not defined
Spoiler: img error
In this case it doesnt matter if "var $ESS = Object;" or "var $ESS = []; "
Screenshot_5.png
------

Edit

nevermind i found a walkaround to use "dopan" as placeholder & connect that to "Self_Switches"..
But the $gameMap.event(12)._SelfSwitch.list();
can still call the list..
also my solution is very clumsi and no final solution..

so i think i have to rebuild the plugin, make 2 different function chains, where "Self_Switches" have only the minimum "Ini function", while all other functions are connected to $ESS..

that would mean having "Self_Switches" && "ESS_BOX" as seperated objects
new Self_Switch()
new ESS_BOX()

similar to what ATT_Turan suggested
.. i guess i should have done that in the first place.. thx 4 the help


sidenote:
the box itself is perfect, i just made it without using push ect,..

adding an array at undefined is just related to mapId, eventId ect, because "selfSwitch_ini" will be called with each "event ini", its already the needed loop..

my problem was, that i did not understand, that i need 2 function code chains and 2 seperated object Types, if i want to avoid the "this.data"-conflict & avoid the "$gameMap.event(id)._SelfSwitch" beeing able to call the plugin functions, or be connected to data that is supposed to belong to $ESS.BOX..


edit2

done, and it was less work than every other solution which i tried^^

-> link

Original thread: https://forums.rpgmakerweb.com/threads/about-creating-game-objects.152793/