Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
  • 0
DarkStorm2Bad

Partial actor name application?

Question

OK, so I realise it's possible to make something happen based on an actor's chosen name using the Conditional Branching option in an event, but is it at all possible to do this based on a string that is part of the actor's name? Like, for example, if the name contains the word "gus". Any help on this would be greatly appreciated.

Share this post


Link to post
Share on other sites

14 answers to this question

Recommended Posts

  • 0

Yes, it's pretty easy.

$game_party.actors[i].name.indexOf(some_string) != -1

Put this in a conditional branch, replacing i with the zero-based index of the actor whose name you want to check and some_string with the string to check for.

Share this post


Link to post
Share on other sites
  • 0

Even easier with a regexp.

$game_party.actors[<actor_id>] =~ /gus/

This will return true if the chosen actor's name contains the string "gus", false if it doesn't. The strong point of Regexps is that you can test for very sophisticated conditions, like the special tags you may put in messages - they use regexps to see if there's any combination of backslash+any chosen letter+open bracket+any numbers+close bracket, and if so, capture the number for future use.

Share this post


Link to post
Share on other sites
  • 0

MoonPearl's suggestion is the way to go. Your exact RegEx for the name "gus" to place in your scripted conditional branch would be:

 

($game_party.actors[iNDEX].name =~ /[Gg][uu][ss]/) != nil

 

This would return true if the word "gus" is in the name at any point, ignoring capitalization.

Alternatively you could use:

 

($game_party.actors[iNDEX].name.downcase =~ /gus/) != nil

Share this post


Link to post
Share on other sites
  • 0

I finally got around to testing the codes, and unfortunately MoonPearl's regexp method provided no results (am I supposed to place it in the Script option of Conditional Branch?), and the other methods I tried gave me this error message:

 

 

NoMethodError occurred while running script.

 

undefined method 'name# for nil:NilClass

Share this post


Link to post
Share on other sites
  • 0

Conditional Branch: [script] "$game_party.actors[<actor_id>].name =~ /gus/

(...commands)

Branch End

Don't forget to replace <actor_id> with the desired ID.

Edited by Moonpearl

Share this post


Link to post
Share on other sites
  • 0

That code's giving me the same error message as the others. It's quite frustrating, as the error message doesn't give me a script line, like I've seen when I tried to google "undefined method", so I can't see what may be going wrong.

Share this post


Link to post
Share on other sites
  • 0

That code's giving me the same error message as the others. It's quite frustrating, as the error message doesn't give me a script line, like I've seen when I tried to google "undefined method", so I can't see what may be going wrong.

It's normal, however it tells you whether it's an syntax error or an error while running script, and this is important.

Share this post


Link to post
Share on other sites
  • 0

Your index is either too small or too large: post a screenie of your code.

Share this post


Link to post
Share on other sites
  • 0

Its the INDEX of the actor in the party, not the ID of the actor. First actor in party = 0, second actor = 1, etc, etc.

 

If you want to use by actor ID, replace $game_party.actors with $game_actor[iNDEX].

Share this post


Link to post
Share on other sites
  • 0

SUCCESS! I went through all of the codes stated here, taking new messages into account, and it was

 

($game_party.actors[0].name.downcase =~ /gus/) != nil

 

that ended up working for me. Thanks for all of your help, everyone! :)

Share this post


Link to post
Share on other sites
  • 0

True. Its a habit of mine, and I like doing it personally.

Same as I use parenthesis for method calls surrounding the arguments, use a "return" at the end of methods instead of just writing the value, etc., etc. In my personal opinion, it makes it a littler clearer what is actually going on for the next person who reads the code. Without it, someone who does not know may think the value returns a boolean, when it actually doesn't, although Ruby allows nil to be used the same as false in most situations. I actually don't like that Ruby allows for such a thing, as it is a bad habit if one ever plans to move beyond Ruby into other languages. Most, if not all (at least common ones), other languages don't allow such things.

Share this post


Link to post
Share on other sites
  • 0

I agree with the return stuff which I also do, but on the contrary for the matter at hand it feels like a pretty awkward syntax, asking whether what looks like a condition but isn't really one equals nil or not. The =~ operator means "is there a match?", that's enough, no need to extend to "is there a response to is there a match?" in my opinion.

Share this post


Link to post
Share on other sites
  • 0

=~ does not mean "is there a match", it returns the position of a match if there is one, or nil if there is not. Either way, there isn't really a right or wrong, its just coding style.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...