Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] How can I add up and subtract speed to the hero?

Grimbel

Sponsor

Hello. Sorry if my English is not very good... I am Spanish, and I understand English quite well, but is difficult to me speak it. I will try to express my doubt as well as I can. If you speak Spanish: he puesto la traducción al español debajo.


Not so long ago I achieved the hero to walk as fast or slow as I want with this miniscript:

Code:
def velocidad(v)
   @move_speed = v
end

If I want to change the hero's speed, I just have to use the option "llamar script" ("call script" in English, I think), and put this: $game_player.velocidad(x) . I can put the numer I want where the "x" is, even decimal numbers. It is really easy and useful. I made a tutorial in Spanish to show how to do it. You can see it here if you want, and you can download the example too.

The problem is that I can replace the speed, but I can not add up or subtract it from a "base speed". I will put an example:

The "base speed" of the hero is "3.5". When he walks over the grass, I want the speed to be "0.4" less than before, so it should be "3.1". I can replace the speed to obtain the number I want. If you wear a pair of magic boots, your speed increases "0.8". That means your speed is "4.3". But... what happens if you wear your boots and you walk over the grass at the same time? You will have a "fight" with both speeds, "3.1" and "4.3". What I want is the speeds to be added up and subtracted, not to be replaced. If you subract "0.4" when the hero walks over the grass, and you add up "0.8" when he wear his boots, the speed will be "3.9", and that is what I want...

Do you understand it? Can anyone help me?

Thanks.

----------------------------------------------------------------------------------------------------------------------------------------

Hace no mucho tiempo logré que el héroe caminara tan rápido o lento como yo quisiera gracias a este miniscript.

Code:
def velocidad(v)
   @move_speed = v
end

Si quiero cambiar la velocidad del héroe, sólo tengo que usar la opción "llamar script" y poner esto: $game_player.velocidad(x) . Puedo poner el número que quiera donde está la "x", incluso si son números decimales. Es muy fácil y útil. Hice un tutorial en español para enseñar cómo hacerlo. Puedes verlo aquí si quieres, y puedes también descargar el ejemplo.

El problema es que puedo sustituir la velocidad, pero no puedo sumar o restarla de una "velocidad base". Pondré un ejemplo:

La "velocidad base" del héroe es "3.5". Cuando camina sobre la hierba, quiero que la velocidad sea "0.4" menos que antes, así que debería ser "3.1". Puedo sustituir la velocidad para obtener el número que yo quiera. Si te pones unas botas mágicas tu velocidad incrementa "0.8". Eso significa que tu velocidad es "4.3". Pero... ¿qué pasa si llevas las botas y caminas sobre la hierba al mismo tiempo? Tendrás una "pelea" entre las dos velocidades "3.1" y "4.3". Lo que quiero es que las velocidades sean sumadas y restadas, no sustituidas. Si restas "0.4" cuando el héroe camina sobre la hierba, y sumas "0.8" cuando lleva las botas, la velocidad será "3.9", y eso es lo que yo quiero...

¿Se entiende lo que digo? ¿Alguien puede ayudarme?

Gracias
 

khmp

Sponsor

I assume you already have this next snippet but it needs to be "attr_accessor" and not just "attr_writer"
Code:
class Game_Player
  attr_accessor :move_speed
end

Then you can use "Llamar Script...":
Code:
$game_player.move_speed += 0.1
$game_player.move_speed -= 0.1

You would still need some extra work to check for a minimum and maximum speed. Por examplo:
Code:
if $game_player.move_speed > 2
  $game_player.move_speed -= 0.1
end
O
Code:
if $game_player.move_speed < 6
  $game_player.move_speed += 0.1
end
 
Instead of...
Code:
def velocidad(v)
   @move_speed = v
end
change it to this...
Code:
def move_speed=(v)
  @move_speed = v
end
And just like khmp said you probably want to check for min and man speeds, so it should be like this
Code:
def move_speed=(v)
  @move_speed = v < 2.0 ? 2.0 : v > 6.0 ? 6.0 : v
end
I hope you understand that line. In case you dont Ill put its equivalent
Code:
if v < 2.0
  @move_speed = 2.0
elsif v > 6.0
  @move_speed = 6.0
else
  @move_speed = v
end

Now you will be able to add or substract
Example
Code:
player.move_speed -= 0.4
player.move_speed = 10		# Will assign 6, since 10 is too big

Cya
 

Grimbel

Sponsor

Thank you very much! This is just what I needed. Just one more thing. Is it posible to keep the speed number in a variable? I know variables doesn't keep decimal numbers, but may be they can be keeped if they are multiplied by 10... I don't know. Anyway I think I can do it with events... so it is not an important thing.
Everything works perfectly: when the character wears the boots he walks faster, and when he doesn't wear them he walks in a normal speed. But when he rides a horse the boots shouldn't take effect, so I replace the character's speed by the horse's speed, and then the character's speed loses... when the character gets off the horse, I don't know what speed I should give to him... Do you know what I mean?  I would like to give him a different speed depending on the number of the variable that kept his speed before he rided the horse.

I hope you understand that line. In case you dont Ill put its equivalent

Thanks, I understand much better the equivalent.
 

khmp

Sponsor

Guachipem":dveex553 said:
Thank you very much! This is just what I needed. Just one more thing. Is it posible to keep the speed number in a variable? I know variables doesn't keep decimal numbers, but may be they can be keeped if they are multiplied by 10... I don't know. Anyway I think I can do it with events... so it is not an important thing.
Everything works perfectly: when the character wears the boots he walks faster, and when he doesn't wear them he walks in a normal speed. But when he rides a horse the boots shouldn't take effect, so I replace the character's speed by the horse's speed, and then the character's speed loses... when the character gets off the horse, I don't know what speed I should give to him... Do you know what I mean?  I would like to give him a different speed depending on the number of the variable that kept his speed before he rided the horse.

I hope you understand that line. In case you dont Ill put its equivalent

Thanks, I understand much better the equivalent.

The greatest power and drawback about Ruby is dynamic type casting. That just means that in other languages where you need to define what the object is meant for ahead of time in Ruby you don't. This allows objects within Ruby to hold anything and everything. In C for instance you would need to define a type ahead of time.
Code:
int a;
a = 4;
In Ruby:
Code:
a = 4
p a.type # Fixnum
a = 4.5
p a.type # Floating Point

How this relates to you is that you can store anything in a variable. You probably think you are stuck in Fixnum because of the way the Event Commands are limited in changing variables. Add 1, Subtract 1, etc. And you're right even if you have a floating point number in the variable at the time it will change to the operand's type. Or at least attempt to. I just realize this is too much information.

If you want to store anything besides a Fixnum in a $game_variables object you can not use the Event Command "Control Variables". You can only use "Script...":
Code:
$game_variables[1] = 4.5
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top