Shoting system specific

I curently do a puzzle game and almost all in map was event.


All  method that I see about shooting system is not relevant in my case. 


So I want to tell you exacly what I want to do and I hope you can tell me how to do it with line script.


Basicly when I got ball, I shoot one at time. They have a parallele event that look if I press a key.


When pressed, the projectile event place to the player location and appear(working).


The projectile shoot in the direction of where the player look at. (working).


And now what I have difficulty with...


I put movement in a loop, and I want that if the ball(set to pass trought) pass over an event that make this verification:


- If the event is set to "under hero" then the ball just continue. 


- If the event is on same level of the hero then, He will trigger the D switch of this event automaticly.


Basicly I need to be able to detect if the event is on the same location then another event and activate a local switch on this event.


Everyplace where this event will shoot will have D switch on all event, so supose to be fine.


I tryed to put paralele event with a switch on event, but on the puzzle they have usualy 50 events, so 50 parallele process will be a nightmare...


I why I try it this way...


Thansk for your help, with that done, a great part of my projet will work.
I thought I already answered that quesion in the other thread, I must not have been clear enough I guess.


You are right that 50 parallel processes is bad, and you should not do it that way.


What you need is a single parallel process maybe a common event maybe just an event on the map.


So inside that parallel process you will check if any of the map event coordinates match those of the ball.


Here is one way of doing that:


var i, a, b, c, d, e, ballId
ballId = 0 //Fill this with your ball event Id
e = $gameMap._events //e will contain the array of all map events
i = 1; a = e[ballId]._x; b = e[ballId]._y //we set a to event [ballId] x coordinate and b to its y coordinate

while (i<e.length) { //we run a loop for the size of the array of the map events.
c = e._x ; d = e._y; //we set c to event x coordinate and d to its y coordinate
if (a === c && b === d) {alert(i)} //We check if both sets of coordinates matches
//replace the alert with whatever you want to do when an event coordinate matches those of the ball
i++;
}

//Checking an event priority type can be done with:
e._priorityType === x
//With x being 0 for below, 1 for same, 2 for above


Hope I helped.
array = $gameMap.eventsXy(x, y);


This pulls an array of all the events in at a specific location.  Might be more efficient than the above code.  (Or it might do exactly the same thing) 
@Internetomancer I didn't even know that function existed! Good find. (And I looked at the Game_Map file too before writing my little code...)


You'd still need to loop inside the created array to do whatever it is you want to do when events overlap though (well, most of the time there should be only 2 events, but you should write it as a loop just in case you have 3 or more events overlapping). 


And I could'nt find how exactly this method works, but I'd guess it does exactly what I did and compares the x and y position of every event on the map to the provided coordinates. So if I had to bet I'd bet that doing it my way is faster because it saves you a loop. Anyway, I learned something, nice!


If you're fishing for speed you could do a reverse while loop (it will save a lot of calculations), but this becomes a bit off topic.
I will try that, but if I put in a parallele event a script:



array = $gameMap.eventsXy(x, y);

what I do next? If I know they will never have more then one event at same place?(exceptionnaly 3) what I need to do next?


If I want to triger a local switch for this event? I know they have some plugging for activate switch, the probleme is the how to?


I jsut did't see it in my mind, Im in the common event called by a parralele event(the projectile) and I just can't see it in my mind.


The part


//replace the alert with whatever you want to do when an event coordinate matches those of the ball


cause me trouble because what I need to put now? Can I really put that must code in the smallbox for putting script?
Yes you can and it's not really that much code 🙂


I don't know what you want to do. For a self switch you can put $gameSelfSwitches.setValue([mapId,eventId,"name"],value)


You replace mapId by the id of the map, eventId by i, "name" by "A", "B",... and value by true or false.
- Accidental double post. Please ignore -
DId I can use that with  array = $gameMap.eventsXy(x, y); ?


Shorter code will be more easy for start. But i is not there so...


What I got:


var i, a, b, c, d, e, p, ballId


p = this.character(0);


ballId = p.eventid


e = $gameMap.eventsXy(p.x, p.y);


i = 1; a = e[ballId]._x; b = e[ballId]._y


while (i<$e.length) {


c = e._x ; d = e._y;





if (a === c && b === d) {alert(i)}





if (e
._priorityType != 1){





$gameSelfSwitches.setValue([32,i,"A"],true)





}





 





did't work yet.
Well that's because you didn't replace the alert with that code, just put it after ^^.


It should be like this:


var i, a, b, c, d, e, ballId
ballId = 0
e = $gameMap._events
i = 1; a = e[ballId]._x; b = e[ballId]._y

while (i<e.length) {
c = e._x ; d = e._y;
if (a === c && b === d) {
if (e._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,name],true)} // this will set the self switch
//to true if the priority is not "same as charachter".
}
i++;
}


Also, you need to do a check with "!==" not "!=".
Well that's because you didn't replace the alert with that code, just put it after ^^.


It should be like this:



var i, a, b, c, d, e, ballId
ballId = 0
e = $gameMap._events
i = 1; a = e[ballId]._x; b = e[ballId]._y

while (i<$e.length) {
c = e._x ; d = e._y;
if (a === c && b === d) {
if (e._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,name],true)} // this will set the self switch
//to true if the priority is not "same as charachter".
}
i++;
}


Also, you need to do a check with "!==" not "!=".





your code look good, but I planing to put everything in a comon event. What missing: how get the current mapID. Also I overpass the max script laught windows size. ALso I got error $e is not defined. I remove the $ and now go to the next line with a _x not exist for undefined.
Ah right, damn small script box.


var i, a, b, c, d, e, ballId, mapId
ballId = 0; mapId = $gameMap.mapId(); e = $gameMap._events;
i = 1; a = e[ballId]._x; b = e[ballId]._y;
while (i<e.length) {
c = e._x ; d = e._y;
if (a === c && b === d) {
if (e._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,name],true)}
}
i++
}


This works, fits inside a single script call, and will automatically update mapId to the current map Id.


You just need to set ballId to your ball eventId and name to your self switch.
Try deleting the comments.  ie. everything after //


You can make multiple script calls, but it forgets any temporary variable names, so you probably don't want to do that.


Otherwise, the only way I know around long scripts is to include it in a plugin.
 


I don't know off hand how to get mapID, but try right clicking and showing the Map properties.  See if it appears at the top fo the screen.  Also, try opening the console (f8) and typing console.log($gameMap) and see if something id related pops up.
what I get with the current script:

Sans tivvvtre.png
I jsut updated your code for try to make it work a little bit. remember that I call common event from a parallele process.


So I put:


var i, a, b, c, d, e, ballId, mapId


ballId = 77; mapId = $gameMap.mapId(); e = $gameMap._events;


i = 1; a = this.character(0).x; b = this.character(0).y;


while (i < $e.length) {


c = e._x ; d = e._y;





if (a === c && b === d) {





if (e
._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,A],true)}





}





i++





}





 





and now I get again these error:


Sans titdfdfdffdre.png
Hum, two things.


First: I made a typo in my code, you should delete the "$" before the e in the while check.


Second: once I made that modification, my code runs perfectly and produces desired results in my project.


My guess would be that one of the plugins you use modifies the properties of the event object.


If that is the case, I sadly can't do anything about it.


Edit: ah, disregard second then, just delete the dollar sign before the e and it should be working just fine. That was a typo on my part, sorry.


Edit of the edit:


var i, a, b, c, d, e, ballId, mapId
ballId = 77; mapId = $gameMap.mapId(); e = $gameMap._events;
i = 1; a = this.character(0).x; b = this.character(0).y;
while (i < e.length) { //delete the $ sign that was there
c = e._x ; d = e._y; //you can't do this, it will just not work. See explanations below
if (a === c && b === d) {
if (e._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,A],true)}
}
i++
}


This can't and won't work.


What my code does is take all the map events and put it into an array so e is [0,event1,event2,...]


Then it checks the properties (in this case the x and y coordinate) of each of the events in the array, and compares them to the ball coordinates.


To do this for each event on the map I set a loop up, that will loop througout the array to read the properties of each object.


To that effect you need to go through each element of the array, hence the e[ i ] command. That tells the code to look for event i and sets c to event i x coordinate and d to event i y coordinate.


We then increase i by one and keep doing the same thing until all elements of the array have been checked.


If you delete it the code can't and won't work.


Looking at your first error message makes me think of a plugin related issue, sadly.
I desactivate all other pluggin and still not work. I still updating your code a little bit because I try to do better for my need.


as you can see, I directly use this for be able to minimize code. the paralle process is on the ball himself, so it's why I can use this.


Maybe the whole trouble come from that....

var i, a, b, c, d, e, mapId


mapId = $gameMap.mapId(); e = $gameMap._events;


i = 1; a = this.character(0).x; b = this.character(0).y;


while (i < e.length) {


c = e._x ; d = e._y;


if (a === c && b === d) {


if (e._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,A],true)}


}


i++


}


 

 By the way, I understand what you mean, but it's jsut a copy past error. ther braket was there in the code, it's just don't appear here for some reason.
With some help I solve some trouble, apparently, because I don't have deleted some event creating a hole in the serie that caused this problem.


curent code don't give any error but did't activate selfswitch of the other event when touch it:


var i, a, b, c, d, e, mapId


mapId = $gameMap.mapId(); e = $gameMap._events;


i = 1; a = this.character(0).x; b = this.character(0).y;


while (i < e.length) {


if (e) {





c = e
._x ; d = e._y;





if (a === c && b === d) {





if (e
._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,"A"],true)}





}





}





i++





}
I don't recognize   this.character(0).x     are you sure that gives you the right x's and y's?
We've been discussing it in PM in french for better communication. It turns out his error came from the fact my code didn't account for possible holes in the event ids. Thus, it tried to check the x and y coordinate of events that didn't exist and crashed the game.


I have a working result: Spoiler for images:


(Here you have a single target event, but I've tested it with 10 events in a row, and with holes in event Ids and it functionned appropriately).

Spoiler
Se7JXSL.png



(The commands transfer_event and event_x event_y come from my plugin, link in my signature, but the code was written without any plugin based commands) Basically it moves the event 1 tile up.


Fireball event.


3nbcER2.png



Character event


rTdmuIk.png



Map 1


2c2pEOm.png



After shooting.



I also didn't know about this.character(0).x but it seems to give the correct result.


For those that want the final code, used in the example above: 

Code:
var i, a, b, c, d, e, ballId, mapId
ballId = 2; mapId = $gameMap.mapId(); e = $gameMap._events; 
i = 1; a = e[ballId]._x; b = e[ballId]._y; 
while (i<e.length) {
  if (e[i] && e[i] !== e[ballId]) { //Check if the event exists, and it is different from the ball
  c = e[i]._x ; d = e[i]._y; 
  if (a === c && b === d) {
      if (e[i]._priorityType !== 1) { $gameSelfSwitches.setValue([mapId,i,"A"],true)}
    }
}
i++
}
Nice!!!


I have a quick question about that-- if there are holes in event IDs, then how do you know you're getting them all?


Like, let's say the array is [0, event1, event2, event6, event8]


Shouldn't it loop through checking event 1 and 2, and then not 3, and then not 4, and then stop since that's the length of the array???

Original thread: https://forums.rpgmakerweb.com/threads/shoting-system-specific.63531/