• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • 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*
267
Posts
9
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:

Flowerchild

fleeting assembly
8,709
Posts
13
Years
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.
 

Telemetius

Tele*
267
Posts
9
Years
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.
 

Flowerchild

fleeting assembly
8,709
Posts
13
Years
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.
 

Telemetius

Tele*
267
Posts
9
Years
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*$/]
 

Flowerchild

fleeting assembly
8,709
Posts
13
Years
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.
 
Back
Top