Mine_Iron(turf/T as turf in oview())
set category="Material Commands"
if(src.alreadymining==1)
src <<"You are already mining!"
return
src.alreadymining=1
src.mineiron=0
Mine_For_Iron(T)
return
That's the verb my worker uses to start mining
now for the proc Mine_For_Iron(T)
mob/proc/Mine_For_Iron(turf/T as turf in view(0))
if(istype(T,/turf/Mountains))
if(src.mineiron==0)
src.freeze=1
switch(rand(1,100))
if(1 to 40)
src<<"You fail to find any iron this time."
sleep(src.intelligence+75)
Mine_For_Iron(T)
if(41 to 97)
src.iron+=1
src<<"You found a Iron piece!"
sleep(src.intelligence+75)
Mine_For_Iron(T)
if(98 to 100)
src.iron+=3
src<<"You found 3 Iron!"
sleep(src.intelligence+75)
Mine_For_Iron(T)
else
src<<"You can't mine yet!"
return
else
src<<"You can't mine here!"
return
eventually this leads to a stack which crashes the server...
But I don't see anything wrong
the wait time is almost 10seconds before it cycles through itself again...
ID:261363
Feb 2 2002, 3:37 pm
|
|
In response to English
|
|
omg you're kidding me
using sleep doesn't finish the current proc? omg omg That means I have to go through about 100+ procs and change the sleep to spawn... sigh :) I guess I should have read up on spawn a bit more, I don't really use it. |
In response to Jon Snow
|
|
Spawn()
Args: Delay: The amount of time (in 1/10 seconds) before Statement is executed. Run Statement after a delay. Statement may be a single statement or a code block enclosed in (optional) braces and indented. Sleep() One common use of sleep is to create what is known as a ticker. That is an infinite loop that performs some periodic operation. Pause the current proc (and its callers) for a specified amount of time. If no delay is specified, the shortest unit of time will be used (i.e. one tick). |
In response to Jon Snow
|
|
It finishes the current proc but not right away, it pauses it. Using spawn like this:
Do_Something() spawn(10) world << "Hello Second one second later" world << "Hello First" This will cause Hello First to be printed out then 1 second later Hello Second will be printed out. With sleep it would work like this: Do_Something() world << "Hello first" sleep(10) world << "Hello second 1 second later" In the second example the proc won't finish until sleep has finished where as the first example will allow everything to continue and say "hello" 1 second later. |
In response to English
|
|
ahhh nice
thanks So this cuts down on infinit loop problems/ Since it's starting a whole new proc again and quiting the last? What if I did this spawn(10) Troy() return if it called return first before spawn wouldn't it stop the proc? |
In response to Jon Snow
|
|
Oh the times I've had to go back and recode. I sometimes find a better less code way to do things though as a result of this.
LJR Jon Snow wrote: omg you're kidding me |
In response to Jon Snow
|
|
return will stop the proc but it will still execute the statements under spawn()
Here's a little snippet I just made up that works: mob/proc Test() var/mob/player/M = new(usr.loc) spawn(30) del(M) spawn(20) Test2(M) return Test2(var/mob/M) world << "[M] was sent" Even though the first one returns at the end Test2 will still be called and M will still be deleted. Because del(M) will happen 1 second after Test2(M) is called, M will still be valid (not null). You have to be careful though because things might happen within that amount of time that you didn't predict. Someone might attack and kill the mob that is mining before it is finished for example. If you aren't sure how something will work just make a little test program where you can try things out. |
mineiron will always be 0 in that code so it will keep looping.
One more thing you might want to reconsider is how your getting the turf. You first get the turf with oview() then you send that turf to Mine_For_Iron but search for it again with view(0).
That shouldn't cause an infinite loop but it could be a little redundant.
You also might want to use spawn() instead of sleep() so it will finish the current proc and then call the next.