ID:158762
Jul 3 2009, 6:02 pm
|
|
How could I make it so that if a selected variable or such as in energy is below 100 than it would slowly regain it's energy back until it reaches 100?
|
In response to Demon_F0rce
|
|
Yes, I am still learningand I am going to examin the examples and the whole code instead of copying.
I will reply back with resaults in a few Edit:: It works, after I changed a few things... but the energy that I declared and set to 100 does not seem to respond. |
In response to IAmNull
|
|
Sounds like your problem is when the loop is being executed. You have to check for and run the loop each time you change the energy. Checking to see if it is currently active is kinda important otherwise you could wind up having multiple instances of it running at once and then you're energy regen is way faster than intended. There's several possible ways to do this, and I prefer a slightly more complex method where health and energy and stuff have their own datums. But here's the super simple way.
A separate proc that will add or remove a specified amount of energy (passed as an argument) to the source mob. This would perform the following steps: 1) change the energy based off of whatever the passed argument is. (note that if you use the += operator, you can pass both positive and negative arguments to both replenish an deplete energy. 2) Check to make sure that the mob is NOT currently recovering energy. (If they are, do nothing) 2a) Set a variable that will be used for the above check (isRecovering = 1) 2b) start the loop 2c) set the variable used for the step 3 check back to false. Example: mob/verb/doSomething() |
In response to Zagreus
|
|
Just noting that, as with most such procs that start an independent, continuous effect (which is a process that takes time), you should spawn() off (spawn(-1) can be used to make it execute as soon as possible, usually immediately) the main process, so the caller(s) do not have to wait for it to finish before they can continue. For example, in your code, doSomething() wouldn't be able to 'Perform action' until the regeneration has completely finished, but it shouldn't have to wait for that. This can be fixed by adding spawn():
mob/proc/updateEnergy(E) The above prevents any callers of updateEnergy() from having to wait until energy regeneration is complete. There is of course the alternative method of using spawn() on the calls themselves, but you shouldn't ever want to wait for this, so doing it in the proc itself is more convenient. |
In response to Kaioken
|
|
Kaioken wrote:
(spawn(-1) can be used to make it execute as soon as possible, usually immediately) Actually, not "usually" immediately. ALWAYS immediately. spawn() executes code outside the spawn() until it hits a sleep, and then it goes to the spawn(). spawn(-1) executes code inside the spawn first, until it hits a sleep, and then returns to the rest of the code. Usually equivalent, except in cases of race conditions. For example, suppose that the "action performed" was simply to immediately output your current energy. If you used spawn(), the output would have been (energy - 15). If you use spawn(-1), the output would have been (energy - 15 + regenAmount). |
In response to Garthor
|
|
Garthor wrote:
Actually, not "usually" immediately. ALWAYS immediately. Hmm, I guess I was accidentally thinking about sleep(-1), which does a backlog check, but spawn(-1) by nature shouldn't, so yeah, I suppose you're right. Though, I already know how spawn() works, so the whole explanation was kinda extraneous, so I skipped it. >_> |
I do ask that you don't just directly copy and instead know what each line does before using it. I would also like to say that, depending on what you want, you may need to add more to the start or finish.