# How to execute a method every few seconds

**URL:** <https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706>\
**Category:** Ruby\
**Created:** [April 14, 2008, 10:06am UTC](https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706 "2008-04-14T10:06:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [April 14, 2008, 10:06am UTC](https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706/1 "2008-04-14T10:06:48Z")

</div>

I’m playing around with making a small test game with what little Ruby  
code I’ve learned so far and I feel like I’m doing good for only having  
been learning the past couple of weeks or so. I’ve mostly been figuring  
things out as I go, but I need a little help.

Say I wanted a character to be afflicted with a “poison” status that  
removes 5% of their current HP. I know it should be a method that occurs  
every few seconds (or just every turn) and that it should loop until  
something is used to cure it or the battle ends, but I’m not sure how to  
do this. I’d like to make it repeat every, say, 15 seconds or so, but I  
guess it doesn’t really matter how often, since this is just for  
practice. The following code is incomplete (and possibly incorrect), but  
please advise:

def poison  
#loop begins… help?  
character.hp - (character.hp \* 0.03)  
puts ‘Your HP dropped!’ + Character[1].hp’/’+Character[1].maxhp  
end #loop ends if battle ends or if antidote used  
end

If anyone would like to school me on how I would apply an “antidote”  
item to remove the effect, feel free to teach. :-p

Before anybody says anything, I’m just doing this for practice… I have  
no intention of trying to make a full game, lol. Just practicing with  
bits and pieces of one. :-p

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [April 14, 2008, 10:18am UTC](https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706/2 "2008-04-14T10:18:00Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Zoe P. wrote:  
| I’m playing around with making a small test game with what little Ruby  
| code I’ve learned so far and I feel like I’m doing good for only having  
| been learning the past couple of weeks or so. I’ve mostly been figuring  
| things out as I go, but I need a little help.  
|  
| Say I wanted a character to be afflicted with a “poison” status that  
| removes 5% of their current HP. I know it should be a method that occurs  
| every few seconds (or just every turn) and that it should loop until  
| something is used to cure it or the battle ends, but I’m not sure how to  
| do this. I’d like to make it repeat every, say, 15 seconds or so, but I  
| guess it doesn’t really matter how often, since this is just for  
| practice. The following code is incomplete (and possibly incorrect), but  
| please advise:

On every update (tick in your game, be it screen redrawing, or turn,  
whatever), you should have a method updating the game state. In that  
method, you can execute the relevant updating code.

Quick and dirty example:

while game do  
~ update\_enemies  
~ update\_environment  
~ update\_effects  
~ update\_playerstate # You’d check for poison in this method  
~ draw\_screen  
~ get\_input  
end

If it shouldn’t happen always, you could check for a counter (the modulo  
operate could come in handy here ;).

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

~ - You know you’ve been hacking too long when…  
…your SO asks you where you want to eat on a friday night and you want  
to:  
cat yellowpages | grep pizza | grep carryout | more  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgDEwMACgkQbtAgaoJTgL/toQCbBoLWIgw6Mg6Ykr6Qt7526x28  
12cAnRMuzf+ygMyaRz5W12TGmMDgv4xy  
=jD1Z  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [April 14, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706/3 "2008-04-14T17:15:54Z")

</div>

On Mon, 14 Apr 2008 03:06:50 -0500, Zoe P. wrote:

> guess it doesn’t really matter how often, since this is just for  
> practice. The following code is incomplete (and possibly incorrect), but  
> please advise:
> 
> def poison  
> #loop begins.. help?  
> character.hp - (character.hp \* 0.03)  
> puts ‘Your HP dropped!’ + Character[1].hp’/'+Character[1].maxhp  
> end #loop ends if battle ends or if antidote used  
> end

You could use a thread

Thread.new do  
until antidote?  
character.hp -= whatever  
puts ‘Your HP dropped…’  
sleep 5  
end  
end

but as Phillip mentioned, it’s probably better to look at how your game  
is organized as a whole, because lots of little threads doing stuff like  
this is going to get confusing, buggy, and dangerous quickly.

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [April 14, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706/4 "2008-04-14T17:54:59Z")

</div>

On 14 Apr 2008, at 09:17, Phillip G. wrote:

> | things out as I go, but I need a little help.  
> | practice. The following code is incomplete (and possibly  
> ~ update\_enemies  
> ~ update\_environment  
> ~ update\_effects  
> ~ update\_playerstate # You’d check for poison in this method  
> ~ draw\_screen  
> ~ get\_input  
> end

Can I make a very very strong suggestion that the design de-couples  
game-world logic from both rendering and all other I/O forms. The  
logic bound operations in domain time cannot be regulated with regard  
to the dynamic complexity of a modern logic set (running in ‘real  
time’ aka. as fast as you can complete that loop), and even more so in  
a very high level language like ruby.

Don’t take my word for it either: [gaffer.org](http://gaffer.org).

Failure to do so often leaves one with a game that requires very  
specific relative timings on inputs if rapid tapping style input is  
required, and under optic-flow systems causes a coupling between  
twitch flow specifics that turns out to not only feel horrible, but  
also causes damage to the player (eye strain, and rsi from input usage  
styles). Specific frequencies obviously vary from engine to engine,  
however, there have been real failures caused directly by this paradigm.

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [April 15, 2008, 4:35am UTC](https://www.ruby-forum.com/t/how-to-execute-a-method-every-few-seconds/134706/5 "2008-04-15T04:35:44Z")

</div>

yeah, I can see what you’re saying, Ken, I was just trying to get an  
idea of how basically to do something like that in a short, simple test  
“game”. Not really so much a game as just a test to see if the code  
functions the way it’s supposed to. I’m nowhere near ready to create a  
full game yet. :-p

I’m just trying to get used to some of the concepts a game would  
require, such as adding items to inventory, taking them out, using items  
on characters to remove status effects, etc. I’m trying to keep it on a  
simple, one character/enemy level right now. I want to understand how to  
do these basic things on a small, one character scale before I get into  
trying to do anything larger, even on just a text-based level.

Simple battle with 1 enemy with only ‘attack’ and ‘item’ as options,  
attacks always succeed (so I don’t have to worry about programming the  
possibility of them missing the target) no special skills, except the  
enemy can inflict ‘poison’ on the character. I’m slowly figuring things  
out as I go.
