thread question

Z

Zoe Phoenix

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?
 
R

Robert Klemme

2008/4/17 said:
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
 
H

Heesob Park

Hi,

Zoe said:
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 Heesob
 
Z

Zoe Phoenix

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

Zoe Phoenix

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

Simon Krahnke

* Heesob Park said:
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
 
P

Phillip Gawlowski

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

Simon Krahnke 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 Gawlowski
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-----
 
P

Phillip Gawlowski

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

Park Heesob wrote:

| Because it works. it saves typing.

That's what shortcuts are for. :p

| 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. :)

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

- --
Phillip Gawlowski
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-----
 
P

Phillip Gawlowski

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

Park Heesob 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 Gawlowski
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-----
 
P

Park Heesob

DQotLS0tLSBPcmlnaW5hbCBNZXNzYWdlIC0tLS0tIA0KRnJvbTogIlBoaWxsaXAgR2F3bG93c2tp
IiA8Y21kamFja3J5YW5AZ29vZ2xlbWFpbC5jb20+DQpUbzogInJ1YnktdGFsayBNTCIgPHJ1Ynkt
dGFsa0BydWJ5LWxhbmcub3JnPg0KU2VudDogU3VuZGF5LCBBcHJpbCAyMCwgMjAwOCAxMjozMiBB
TQ0KU3ViamVjdDogUmU6IHRocmVhZCBxdWVzdGlvbg0KDQoNCj4gLS0tLS1CRUdJTiBQR1AgU0lH
TkVEIE1FU1NBR0UtLS0tLQ0KPiBIYXNoOiBTSEExDQo+IA0KPiBQYXJrIEhlZXNvYiB3cm90ZToN
Cj4gDQo+IHwgV2h5IG5vdCBtYWtlIGl0ICpvZmZpY2lhbCogY29kZSBjb252ZW50aW9ucyBmb3Ig
UnVieT8NCj4gDQo+IFdoeSAqbWFrZSogaXQgb2ZmaWNpYWw/IFdobydzIHRvIGVuZm9yY2UgaXQ/
IEJ5IHdoaWNoIGF1dGhvcml0eT8gV2hhdA0KPiB1c2Ugd291bGQgaXQgYmUgaWYgaXQgd2VyZSBv
ZmZpY2lhbD8NCj4gDQpUaGVuIHdoeSBqYXZhIGhhcyBodHRwOi8vamF2YS5zdW4uY29tL2RvY3Mv
Y29kZWNvbnYvaW5kZXguaHRtbCA/DQpXaXRoIHlvdXIgZGVmaW5pdGlvbiwgSSBndWVzcyBhbGwg
ZG9jdW1lbnRzIGluICBodHRwOi8vd3d3LnJ1YnktbGFuZy5vcmcvZW4vZG9jdW1lbnRhdGlvbi8N
CmFyZSB1bm9mZmljaWFsIGFsc28uDQpJbiBteSBkZWZpbml0aW9uLCBvZmZpY2lhbCBtZWFucyBs
aXN0aW5nIGluICBodHRwOi8vd3d3LnJ1YnktbGFuZy5vcmcvZW4vZG9jdW1lbnRhdGlvbi8uDQoN
ClJlZ2FyZHMsDQpQYXJrIEhlZXNvYg==
 
P

Phillip Gawlowski

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

Park Heesob 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 Gawlowski
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-----
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top