ID:148920
 
I'm trying to have a system where I have a pet that has stats decay slowly(Hunger, Happyness, etc) how would I allow my hunger proc and my randommovement proc run at the same time since they use totally different clocks and it will get very complicated if I make any more looped procs in the future
Thanks in advance - Senormofo
Have you tried something like this..?

mob/proc/Bleh()
spawn while(1)
sleep(50)
src << "Blah"
spawn while(1)
sleep(10)
src << "Bleh"
In response to Hanns
Hanns wrote:
Have you tried something like this..?

mob/proc/Bleh()
spawn while(1)
sleep(50)
src << "Blah"
spawn while(1)
sleep(10)
src << "Bleh"

Just to be on the safe side, you should probably make that into a background proc.

mob/proc/Bleh()
set background = 1

//the rest of what you had

That way, it won't hog processor cycles in the infinite loops -- it'll skip ticks if it has to.
In response to Hanns
Hanns wrote:
Have you tried something like this..?

mob/proc/Bleh()
spawn while(1)
sleep(50)
src << "Blah"
spawn while(1)
sleep(10)
src << "Bleh"


Why use sleep? If you're using spawn, might as well use it properly.

mob/proc/Bleh()
spawn(50)
src << "Blah"
spawn(10)
src << "Bleh"


That should accomplish roughly the same results. Spawn(n) waits for n seconds then takes the actions listed under it (indented). Sleep(n) freezes the procedure for n seconds before continuing. Putting sleep() in a spawn() statement will accomplish the same thing, end result wise, but it makes more programming sense to eliminate needless clutter. Doing so makes your code smalle, faster, more efficient, and easier to read.

-<font color = #0000FF>Sapphiremagus</font>
In response to sapphiremagus
Actually, not exactly. Running loops inside a proc that never ends is preferable to calling the same proc over and over again -- running an infinite loop means that the code runs through a jump instruction, while spawning the proc over and over again runs through a function-call, which is much slower (because it has to look up the function's location in the DMB's function table).

Plus, your proc isn't exactly infinite. =P


[edit] I think you misread Hanns' code -- he was spawning "while(1)" statements, which are infinite loops. He wasn't spawning sleeps for several seconds and having the procedure return.
In response to Spuzzum
Isn't what he looking for more like:

mob/monster
     var
          stomach = 100
          mood = 100
     proc
          hunger()
               stomach -= 5
               view() << "I'm getting hungry!"
               spawn(100) hunger()
          happiness()
               mood -= 3
               view() << "I'm becoming more sad!"
               spawn(100) happiness()
In response to Spuzzum
My apologies. Misread = 4am post. No more posting at 4am. Bad for you. Bad for me. Me go bed now. Night.
In response to Orochi p_fish
Yeh Orochi is right I'm looking for a way to run somthing like this...
proc
hunger()
stomach -= 5
view() << "I'm getting hungry!"
spawn(100) hunger()
happiness()
mood -= 3
view() << "I'm becoming more sad!"
spawn(100) happiness()
Ok...there's a few ways to do this.
I'm assuming you're not looking for -exact- symmetry...since that's pretty impossible...

The easiest way is probably to bounce off time with modulo..like this

something like:

mob/proc
first()
usr << "first"
while(world.time%100 != 0)
sleep(-1)
spawn(first)
second()
usr << "second"
while(world.time%100 != 0)
sleep(-1)
spawn(second)


A true time-offset ticker is probably the -best- way to go, and you might want to use my ticker library (which I intend to clean up and put up sometime today, since it seems to be in demand and useful)
In response to Senormofo
New()
spawn() hunger()
spawn() happiness()

I don't see where you need help...
In response to Spuzzum
I thought having those sleep procs in there accomplished that by pausing the proc and letting the CPU attend to other things. I may have made some incorrect assumptions as to how sleep actually works...
In response to Garthor
Garthor wrote:
New()
spawn() hunger()
spawn() happiness()

I don't see where you need help...

Yes, I forgot this part of the code. Thanks for the assist, Garthor.