how to execute a method every few seconds

Z

Zoe Phoenix

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
 
P

Phillip Gawlowski

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

Zoe Phoenix 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 Gawlowski
Twitter: 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

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

Ken Bloom

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

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.
 
J

James Tucker

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

Zoe Phoenix 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

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.

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.
 
Z

Zoe Phoenix

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top