Thread question

Okay, I took someone’s advice in an earlier post I made on how to poison
a character in a test game I wanted to make by making a thread. I’m not
sure if this is correct or not, but this is what I have:

def poison
poisoned = thread.new
poisoned do
Character.hp - (rand(Character.hp * (10 - 5 + 1)/100) +
Character.hp*5/100)
puts ‘Your HP dropped!’ + Character.hp + ‘/’ + Character.maxhp
sleep 20 #thread sleeps for 20 seconds
end
end

Can I do it this way, so “Character.poison” would start the thread? If
this would work, how would I use an “antidote” item to stop the thread?

2008/4/17, Zoe P. [email protected]:

Okay, I took someone’s advice in an earlier post I made on how to poison
a character in a test game I wanted to make by making a thread. I’m not
sure if this is correct or not, but this is what I have:

def poison
poisoned = thread.new

“thread” needs to be spelled uppercase and it needs a block.

poisoned do

What is “poisoned”?

      Character.hp - (rand(Character.hp * (10 - 5 + 1)/100) +

Character.hp*5/100)

You are using a constant for the character? I would guess that there
will be multiple characters in your game.

Also, you are not updating Character.hp (there is no assignment). And
if you do, you have a synchronization issue, i.e. you need mutual
exclusion for the update since I am assuming you will access hp from
other threads as well.

      puts 'Your HP dropped!' + Character.hp + '/' + Character.maxhp
      sleep 20  #thread sleeps for 20 seconds
end

end

Can I do it this way, so “Character.poison” would start the thread? If
this would work, how would I use an “antidote” item to stop the thread?

It won’t work for various reasons mentioned above.

Kind regards

robert

Hi,

Zoe P. wrote:

Okay, I took someone’s advice in an earlier post I made on how to poison
a character in a test game I wanted to make by making a thread. I’m not
sure if this is correct or not, but this is what I have:

def poison
poisoned = thread.new
poisoned do
Character.hp - (rand(Character.hp * (10 - 5 + 1)/100) +
Character.hp*5/100)
puts ‘Your HP dropped!’ + Character.hp + ‘/’ + Character.maxhp
sleep 20 #thread sleeps for 20 seconds
end
end

Can I do it this way, so “Character.poison” would start the thread? If
this would work, how would I use an “antidote” item to stop the thread?

Here is a working code:

def poison
$poisoned = Thread.new {
hp = 100
maxhp = 300
loop {
Thread.stop if Thread.current[“state”]==“stop”
hp -= (rand(hp * (10 - 5 + 1)/100) + hp*5/100)
puts 'Your HP dropped! ’ + hp.to_s + ‘/’ + maxhp.to_s
sleep 1
}
}
end
def antidote
puts “call antidote”
$poisoned[“state”] = “stop”
end
def antiantidote
puts “call antiantidote”
$poisoned[“state”] = “run”
$poisoned.run
end

poison
sleep 10
antidote
sleep 10
antiantidote
sleep 10

Regards,
Park H.

Oops, that’s wrong… it should’ve been:

Character.new(1, ‘Coty’, [40, 40])

I forgot to erase the other two stats that I had at the end of it.

As far as the characters, I saw a template for a “materia system” that
someone made for RPGMaker XP and I used the way they made a “materia”
class as a template for making a character class. I only left the
important stats in there at the moment, hp and maxhp. I didn’t really
intend for there to be more than one character at the moment, to try and
keep it simple. I thought the best way would be to learn how best to
apply it to one character before I started trying to do it with several.

class Character

def initialize(char_id, name, stats = [hp, maxhp])
@char_id, @name, stats =
char_id, name, stats

end

So, when a new character is created, I’m guessing it should look like
this:

Character.new(1, ‘Coty’, [40, 40], 1, 0)

If I went ahead and made a game, this is where the “hp” and “maxhp”
would need to come from, I assume.

I’m glad I can post stuff on here that I know is wrong without having to
worry about people telling me I’m dumb. :-p Thanks. :slight_smile:

Here is a working code:

Why are you using { } for multi line blocks?

def poison
$poisoned = Thread.new {

Argh, global variables.

@poisoned = Thread new {
hp = 100
maxhp = 300
loop {
  Thread.stop if Thread.current["state"]=="stop"

You can just call return instead of Thread.stop.

      hp -= (rand(hp * (10 - 5 + 1)/100) + hp*5/100)
        hp = (hp * (0.95 + 0.02 * rand)).round
      puts 'Your HP dropped! ' + hp.to_s + '/' + maxhp.to_s
        puts "Your HP dropped to #{hp} of #{maxhp}!"
      sleep 1
    }

}
end
def antidote
raise “no thread!” unless @poisoned

puts “call antidote”
@poisoned[“state”] = “stop”
end

mfg, simon … l

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

Simon K. wrote:

| Why are you using { } for multi line blocks?

Three reasons:

  1. Because he can.
  2. It is aesthetically more pleasing to him than do … end
  3. curly braces bind tighter than the do … end notation.

That the Ruby community favors do…end for multi-line blocks is more a
convention than a necessity (of course it helps to adopt Ruby
conventions when writing code that other Rubyists are likely to see, but
otherwise, it hardly matters).


Phillip G.
Twitter: twitter.com/cynicalryan

Rule of Open-Source Programming #1:

Don’t whine unless you are going to implement it yourself.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgJ2zcACgkQbtAgaoJTgL8GFQCfRYKV0VEUGjAsrpHCz7mh+25M
HPcAoJA+/Ns5+wthWMcA5qZGzbxe4yqX
=d3vB
-----END PGP SIGNATURE-----

Hi,
----- Original Message -----
From: “Simon K.” [email protected]
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” [email protected]
Sent: Saturday, April 19, 2008 8:35 PM
Subject: Re: thread question

Here is a working code:

Why are you using { } for multi line blocks?

Because it works. it saves typing.

def poison
$poisoned = Thread.new {

Argh, global variables.

I know using global variables is undesirable.
I just want to show a simple working code not a perfect code.

I guess you want to adhere to the ruby code convention or style
guidelines.
I know an unofficial style guideline
http://www.caliban.org/ruby/rubyguide.shtml#style
and http://www.rubygarden.org/ruby?RubyStyleGuide is broken.
Where is the official Code Conventions for the Ruby?

I’ll try to follow it if there is an official convention.

mfg, simon … l

Regards,
Park H.

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

Park H. wrote:

| Because it works. it saves typing.

That’s what shortcuts are for. :stuck_out_tongue:

| I guess you want to adhere to the ruby code convention or style
guidelines.
| I know an unofficial style guideline
http://www.caliban.org/ruby/rubyguide.shtml#style
| and http://www.rubygarden.org/ruby?RubyStyleGuide is broken.
| Where is the official Code Conventions for the Ruby?

There is no “official” set of conventions for Ruby. It’s a community
standard, that makes it easier to produce code that can be read by the
largest amount of people with the minimal amount of thinking.

If you prefer one way, go for it. But you’ll notice sooner or later,
that the Ruby community’s conventions create a layout rather pleasing to
the eye, that you can return to later and get back into easier, to boot.
:slight_smile:

P.S. The caliban.org styleguide is pretty much current, AFAIK.


Phillip G.
Twitter: twitter.com/cynicalryan

Rule of Open-Source Programming #6:

The user is always right unless proven otherwise by the developer.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgKBXgACgkQbtAgaoJTgL+QOQCglD6/Zodduh72ReNKynQEdADY
zuoAnjw8I2CnsLK7CjWxzYFKzK/q2UNx
=iRxr
-----END PGP SIGNATURE-----

----- Original Message -----
From: “Phillip G.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Saturday, April 19, 2008 11:45 PM
Subject: Re: thread question

Why not make it official code conventions for Ruby?
I really want to see the link for the code conventions for ruby and
the ruby language specification in
http://www.ruby-lang.org/en/documentation/

Regards,
Park H.

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

Park H. wrote:

| Why not make it official code conventions for Ruby?

Why make it official? Who’s to enforce it? By which authority? What
use would it be if it were official?


Phillip G.
Twitter: twitter.com/cynicalryan

~ Wormwood : Calvin, how about you?
~ Calvin : Hard to say ma’am. I think my cerebellum just fused.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgKEKgACgkQbtAgaoJTgL+OygCgpp1rn1OoOlm2XKWIwCNUPaWh
hOwAnibuD8sCZneUOUVPepIfC8BSJiKi
=yBKY
-----END PGP SIGNATURE-----

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

Park H. wrote:

| Then why java has http://java.sun.com/docs/codeconv/index.html ?

To quote from that page:
“This Code Conventions for the Java Programming Language document
contains the standard conventions that we at Sun follow and recommend
that others follow.”

The conventions are used by Sun, and are recommended for others. That
doesn’t make them part of, say, the Java specs.

| With your definition, I guess all documents in
http://www.ruby-lang.org/en/documentation/
| are unofficial also.

False conclusion. I never made any claims on anything being official or
not besides the styleguide.

That is a Hasty Generalization.


Phillip G.
Twitter: twitter.com/cynicalryan

Don’t stop at one bug.
~ - The Elements of Programming Style (Kernighan & Plaugher)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgKI+gACgkQbtAgaoJTgL9MSwCcDqB8nNceNy1IaUlE4MoLaCN9
+8QAn299H2UK22OIw3Cn7vaeMzQ1lL+M
=TmRv
-----END PGP SIGNATURE-----

----- Original Message -----
From: “Phillip G.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Sunday, April 20, 2008 12:32 AM
Subject: Re: thread question

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

Park H. wrote:

| Why not make it official code conventions for Ruby?

Why make it official? Who’s to enforce it? By which authority? What
use would it be if it were official?

Then why java has http://java.sun.com/docs/codeconv/index.html ?
With your definition, I guess all documents in
http://www.ruby-lang.org/en/documentation/
are unofficial also.
In my definition, official means listing in
http://www.ruby-lang.org/en/documentation/.

Regards,
Park H.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs