Improving player movability condition

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
How can this method be improved? It's clearly checking too many different conditions.

Code:
class Game_Player  def movable?    return false if moving?    return false if @move_route_forcing || @followers.gathering?    return false if @vehicle_getting_on || @vehicle_getting_off    return false if $game_message.busy? || $game_message.visible    return false if vehicle && !vehicle.movable?    return true  end end
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
It's clearly checking too many different conditions.
Its only checking too much if certain steps are not required. Otherwise its checking exactly what it needs to in order to perform the desired function. :)

Not checked, but i would think that the vehicle method returns data for the vehicle object, which will likely have logic incorporated for checking if getting on and off, so you could perhaps ditch the 'return false if @vehicle_getting_on || @vehicle_getting_off', but who knows ^_^
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
How is checking whether the message box is showing or not related to checking whether you're getting in a vehicle?
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
lol, they are completely unrelated - naturally, however, in this situation they are both restrictions the engine dev's felt where necessary. Personally I wouldn't mind being able to move while im supposed to be reading an important cutscene message :p
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
however, in this situation they are both restrictions the engine dev's felt where necessary.
Which is OK, but requiring a set of conditions to be met in order to proceed with an action doesn't mean you have to write them in one method.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
oh for sure..

According to my own benchmarking tests though, more methods = slower code (in most cases). So for example, if you separated each of those checks into its own method, like this...

class Game_Player  def movable?    any_moving?  end  def any_moving?    moving? || movable_check2? || movable_check3? || movable_check4? || movable_check5?  end  def movable_check2?    @move_route_forcing || @followers.gathering?  end  def movable_check3?    @vehicle_getting_on || @vehicle_getting_off  end  def movable_check4?    $game_message.busy? || $game_message.visible  end  def movable_check5?    vehicle && !vehicle.movable?  endend Could potentially result in code that runs slower :)

Also - checking a variable value within a method is always faster than calling a method that returns the same value.  Its one of the many 'perks' \s of the ruby code interpreter :p

Edit:

That being said, perhaps 'speed' isnt the priority, perhaps its flexibility. In which case, having the separated methods would be much more advantageous. :)
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I would trade a few milliseconds* of processing for infinitely more flexible code to work with.


*untested, just a random figure
 

Lemur

Crazed Ruby Hacker
Veteran
Joined
Dec 1, 2014
Messages
106
Reaction score
124
First Language
English
Primarily Uses
How can this method be improved? It's clearly checking too many different conditions.

class Game_Player def movable? return false if moving? return false if @move_route_forcing || @followers.gathering? return false if @vehicle_getting_on || @vehicle_getting_off return false if $game_message.busy? || $game_message.visible return false if vehicle && !vehicle.movable? return true end end
Code:
[  moving?,  @move_route_forcing,  @followers.gathering?,  @vehicle_getting_on,  $game_message.busy?,  $game_message.visible,  vehicle && !vehicle.moveable?].none?
Though I would add a concept of movable to each of the objects, that way you can just ask each one of them in turn depending on what objects your character is tied to.
 
Last edited by a moderator:

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
[  moving?,  @move_route_forcing,  @followers.gathering?,  @vehicle_getting_on,  $game_message.busy?,  $game_message.visible,  vehicle && !vehicle.moveable?].none?Though I would add a concept of movable to each of the objects, that way you can just ask each one of them in turn depending on what objects your character is tied to.
Why make the method about three times slower and less readable?
 

Lemur

Crazed Ruby Hacker
Veteran
Joined
Dec 1, 2014
Messages
106
Reaction score
124
First Language
English
Primarily Uses
Because you can do something like this later:

player.moveables = []player.moveables << (whatever)# Ex objects as hashes:# {name: 'ship', active: false, moveable?: (single method to determine moveability)} player.moveables.select(&:active).none?(&:moveable?)Tell me honestly, is speed that large of a concern to you? You're wrong, but is it really?

foo = -> { puts 'foo' }bar = -> { puts 'bar' }baz = -> { puts 'baz' } Benchmark.measure { 100000.times { [foo,bar,baz].none? { |proc| proc.call() } } }.real# => 1.002677 Benchmark.measure { 100000.times { foo.call() || bar.call() || baz.call() ? false : true } }.real# => 0.917967Please, if you're going to try and make a point on speed, post the benchmarks instead of spreading blatantly false information.
 
Last edited by a moderator:

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
Because you can do something like this later:

player.moveables = []player.moveables << (whatever)# Ex objects as hashes:# {name: 'ship', active: false, moveable?: (single method to determine moveability)} player.moveables.select(&:active).none?(&:moveable?)
Nice, but even less readable for non programmers. And most RPG Maker users are not programmers and don't have interest in becoming one. As I mentioned in the other thread, people that may change one or two things in the scripts to fix or adjust something for their games, would appreciate readability and "captain obvious style" code commenting.

Tell me honestly, is speed that large of a concern to you? You're wrong, but is it really?

foo = -> { puts 'foo' }bar = -> { puts 'bar' }baz = -> { puts 'baz' } Benchmark.measure { 100000.times { [foo,bar,baz].none? { |proc| proc.call() } } }.real# => 1.002677 Benchmark.measure { 100000.times { foo.call() || bar.call() || baz.call() ? false : true } }.real# => 0.917967Please, if you're going to try and make a point on speed, post the benchmarks instead of spreading blatantly false information.
I was wrong in the speed difference, yes. Still, it is slower and less readable.



class Game_Player < Game_Character def movable2? [moving?, @move_route_forcing, @followers.gathering?, @vehicle_getting_on, $game_message.busy?, $game_message.visible, vehicle && !vehicle.moveable?].none? endend Script Call Event Command:

eval %Q(require 'benchmark/ips'Benchmark.ips do |x| x.config:)time => 5, :warmup => 2) x.report("rtp movable") { $game_player.movable? } x.report("lemur movable") { $game_player.movable2? } x.compare!end) 
Speed is a concern for every RPG Maker user, the RGSS Player is quite slow for what it does.
 

Lemur

Crazed Ruby Hacker
Veteran
Joined
Dec 1, 2014
Messages
106
Reaction score
124
First Language
English
Primarily Uses
If I'm being honest, the entirety of RGSS 3 needs to have a lot of cleanup to it. It's too dependent on procedural code practices and tends to leave a lot of mess around. That's common for people just starting Ruby, especially coming from C type backgrounds.

What I'm suggesting is using a far more OO approach of Tell, Don't Ask. It does a huge amount for cleaning up code, and is a very good rule to live by for object systems much like this. FP and OO are good paradigms to mix for this type of thing, Procedural ends up in a mess for data driven objects like this.
 

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
If I'm being honest, the entirety of RGSS 3 needs to have a lot of cleanup to it. It's too dependent on procedural code practices and tends to leave a lot of mess around. That's common for people just starting Ruby, especially coming from C type backgrounds.
Yes, absolutely. GDI+ for drawing on top of that.

What I'm suggesting is using a far more OO approach of Tell, Don't Ask. It does a huge amount for cleaning up code, and is a very good rule to live by for object systems much like this. FP and OO are good paradigms to mix for this type of thing, Procedural ends up in a mess for data driven objects like this.
I agree with that, I hope the next iteration of the program improves the Player and the standard library.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,853
Messages
1,016,990
Members
137,562
Latest member
tamedeathman
Top