Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

get event location/check around event

Status
Not open for further replies.

dolarmak

Villager
Xy$
0.00
Okay, i've started working on this plugin that will check around the event that activates it for a specific event name. I'm using this for a scarecrow event that when the day starts the filed/plant events evaluate if the scare crow is around them and if not there is a chance the plant get destroyed.

now the problem i have is with the script getting the events location. When checking a specific coordinate, the script works fine, but when if asks for the events location I get crazy errors. I was told a line $gameMap.events()[this._eventId].x; should get the x coord for the event, but it doesn't seem to. same code switch x for y to get the Y coord.

anyway, here is what I have so far. On lines 25 and 26 i set static numbers and commented out the dynamic xy. if any one knows whats wrong let me know.
JavaScript:
/*:
* @author Dolarmak
* @plugindesc Checks if a scarecrow event is within 3 tiles of the event
*
* @help
* =============================================================================
* What does it do?
* =============================================================================
*
* Checks if a scarecrow event is within 3 tiles of the event
*
* =============================================================================
*/

var aliasPluginCommand = Game_Interpreter.prototype.pluginCommand;

Game_Interpreter.prototype.pluginCommand = function(command, args)
{
    aliasPluginCommand.call(this, command, args);
   
    if(command == "scarecrow")
    {
        //----- Check if Scarecrow Event is in range -----//
        //$gameMessage.add("checking");
        var sc_event_x = 4; //$gameMap.events()[this._eventId].x;
        var sc_event_y = 4; //$gameMap.events()[this._eventId].y;
        var sc_event_true = false;
       
        for(var sc_event_y_check = (sc_event_y - 3); sc_event_y_check < (sc_event_y + 4); sc_event_y_check = (sc_event_y_check + 1))
        {
            for(var sc_event_x_check = (sc_event_x - 3); sc_event_x_check < (sc_event_x + 4); sc_event_x_check = (sc_event_x_check + 1))
            {
                if ($gameMap.isXyHasEventName(/scarecrow/i,sc_event_x_check,sc_event_y_check) == 1)
                {
                    var sc_event_true = true;
                }
               
            }
        }
       
        //----- End Result ----//   
        if (sc_event_true == true)
        {
            //----Scarecrow is in Range ----//
            //$gameMessage.add("Scarecrow detected");
            $gameSelfSwitches.setValue([this._mapId, this._eventId, 'A'], false);
            $gameSelfSwitches.setValue([this._mapId, this._eventId, 'B'], true);
        }
        else
        {
            //----Scarecrow is not in Range ----//
            //$gameMessage.add("No scarecrow detected");
            $gameSelfSwitches.setValue([this._mapId, this._eventId, 'A'], true);
            $gameSelfSwitches.setValue([this._mapId, this._eventId, 'B'], false);
        }
       
    }
   
}
 

Xilefian

Adventurer
Xy$
0.00
$gameMap.events() returns all events on the map that convert into a true boolean; if there's an event that doesn't convert to a boolean (such as the first one in the array, which is null) then the eventId will be getting you the wrong event (offset by 1 by default, due to first one being null), so you're not getting the right event (and worse than that, you could reference beyond the size of the array with this code).

$gameMap.events() is intended for iterating over valid events, rather than finding the event you need. Don't use it.

If you want an API-safe way to get the map event from your Game_Event then use $gameMap.event( this.eventId() ). If you want a faster, but API unsafe way to get the map event then use $gameMap._events[this._eventId].

Extra stuff (not related to answer);
If you're going to use the API functions to get the map event then store the map event; doing $gameMap.event( this.eventId() ) twice for X and Y is wasted performance and someone may write a Plugin that makes each of those functions take 5 minutes to return.
Either of these would be ideal;
JavaScript:
// API functions - Faster
var event_id = this.eventId();
var map_event = $gameMap.event( event_id );
var sc_event_x = map_event.x;
var sc_event_y = map_event.y;

// Direct - Fastest, less API friendly
var sc_event_x = $gameMap._events[this._eventId].x;
var sc_event_y = $gameMap._events[this._eventId].y;
Are you using the F8 debugger? This seems like something you'd have solved yourself with a break point and stepping into your use of $gameMap.events() to see what event you get out of it.
 

dolarmak

Villager
Xy$
0.00
Thank you, funny enough a friend on another forum answered this only 3 minutes before you did lol but you're 100% right. I appreciate the responce.

$gameMap.events() returns all events on the map that convert into a true boolean; if there's an event that doesn't convert to a boolean (such as the first one in the array, which is null) then the eventId will be getting you the wrong event (offset by 1 by default, due to first one being null), so you're not getting the right event (and worse than that, you could reference beyond the size of the array with this code).

$gameMap.events() is intended for iterating over valid events, rather than finding the event you need. Don't use it.

If you want an API-safe way to get the map event from your Game_Event then use $gameMap.event( this.eventId() ). If you want a faster, but API unsafe way to get the map event then use $gameMap._events[this._eventId].

Extra stuff (not related to answer);
If you're going to use the API functions to get the map event then store the map event; doing $gameMap.event( this.eventId() ) twice for X and Y is wasted performance and someone may write a Plugin that makes each of those functions take 5 minutes to return.
Either of these would be ideal;
JavaScript:
// API functions - Faster
var event_id = this.eventId();
var map_event = $gameMap.event( event_id );
var sc_event_x = map_event.x;
var sc_event_y = map_event.y;

// Direct - Fastest, less API friendly
var sc_event_x = $gameMap._events[this._eventId].x;
var sc_event_y = $gameMap._events[this._eventId].y;
Are you using the F8 debugger? This seems like something you'd have solved yourself with a break point and stepping into your use of $gameMap.events() to see what event you get out of it.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
I'm closing thread due to being addressed & solved if for any reason you would like it re-opened please report the post.
 
Status
Not open for further replies.
Top