• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Our friends from the Johto Times are hosting a favorite Pokémon poll - and we'd love for you to participate! Click here for information on how to vote for your favorites!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Scripting Question] Get event id based on his name.

Telemetius

Tele*
  • 256
    Posts
    10
    Years
    Hello forum.

    So what I'm trying to do is to create a method that returns the id of the event with a certain name.
    For example if I use identificatore("Carl") it should give me the id of the event on the map named carl.
    The game isn't showing me errors but the code won't return me anything either:

    Code:
    def identificatore(value)
      example=[]
      if !value.is_a?(Integer)
        for event in $game_map.events.values
           if event.name[/^#{value}$/]
             example.push(event)
           end
        end
        eventid=get_character(example[0]).id
      end
    return eventid
    end
     
    Last edited:
    I think something simple like this should work fine, unless I'm misunderstanding somehow. I'm not quite sure why you've made one that looks so complicated.
    Code:
    def getIDByName(name)
      return nil unless name.class==String
      for event in $game_map.events.values
        return event.id if event.name==name
      end
    end
    seems to work fine from what I tested.

    this won't work if you have multiple events on one map with the same name, if you want to do that just replace the return command with an array push and return the entire array after the end of the for loop.
     
    I think something simple like this should work fine, unless I'm misunderstanding somehow. I'm not quite sure why you've made one that looks so complicated.
    Code:
    def getIDByName(name)
      return nil unless name.class==String
      for event in $game_map.events.values
        return event.id if event.name==name
      end
    end
    seems to work fine from what I tested.

    this won't work if you have multiple events on one map with the same name, if you want to do that just replace the return command with an array push and return the entire array after the end of the for loop.

    Thank you! It works if I use getIDByName("name").
    And how would I do if I wanted to define the name without " " ?
    I'm asking because the code snippet is part of a larger script.
     
    uh, the "" denotes a string, so I'm pretty sure that's the only way to do it. if you were to remove them it would think the input is a variable/method name and give an error when it can't find it.
     
    uh, the "" denotes a string, so I'm pretty sure that's the only way to do it. if you were to remove them it would think the input is a variable/method name and give an error when it can't find it.

    I saw in the compiler section that Regular expressions can be used to define something by its name, for example the game recognises when an event is named Trainer(X).

    The code I'm talking about is this: event.name[/^\s*Trainer\s+\((\d+)\)\s*$/]
     
    oh, yeah that. I suppose that might work too, but unfortunately I have no clue how regex works so you'll have to look that up yourself or wait for someone else.

    Ok no problem, I'll see what I can find.
     
    Back
    Top