Code:mob
var
Level = 1
HP = 100
str = 5
dex = 5
stam = 5
luck = 5
armr = 1
wep = 1
atk = (str + (dex/2) + wep) * 2 //This line causes compilation problems
Problem description: I want to determine a variable with an equation that uses other variables in it. The equation is for the variable Atk. Here is the equation: Atk = (str+(dex/2)+wep)*2 and then I can determine damage with this. Damage = (Atk*level+(level/2)-def)(x) where x is a random number from 0.5 to 1.2... I have been trying to plug this equation into a simple mob var but it doesn't seem to work. any ideas?
ID:139501
![]() Dec 12 2010, 5:54 pm
|
|
![]() Dec 12 2010, 6:33 pm
|
|
You cannot at compile-time set a variable to some non-constant expression. What you need to do is instead either update the variable whenever you change one of the dependent variables, or just use a function that calculates it on the spot for you whenever you need it.
|
That's what I am trying to do with the equation, I want it to calculate the variable on the spot so that if the player improves their stats, then the equation will be updated due to their stats, simple algebra, but I still don't understand how to plug it in.
|
If you had read what garthor had said carefully, you would not have posted what you did.
Garthor is saying that you cannot use other variables, which can change, to define a value for a variable at start-up. Instead, you have to use a proc that updates this value during run-time. So... mob/var Please note the two verbs are just examples. With the UpdateAttack() proc in place, all you need to do is call it whenever any of the 3 affecting variables are changed. |
Ok, I figured out my own way of doing it. Basically I made a proc that determines the value with the equation. Etc. Etc. Now I have two new problems. 1) my randomizer in the code
Attack(mob/M as mob in oview(1)) oview() << "[usr] attacks [M]!" var/atk = (str+(dex/2)+wep)*2 var/x = rand(0.5,1.2) var/damage = (atk + (Level/2)-def) * x world << "[damage] damage!" M.HP -= damage M.DeathCheck() Isn't working, it either comes out 0, or 25.5 assuming that my variables are str = 5, dex = 5, wep = 5, level = 1, and def = 0. so basically the randomizer isn't being determined and multiplied at all. 2) For some reason when I use the attack verb, I can kill my turf. Example, I am destroying my grass. Any Ideas? |
Garthor wrote:
rand() will only give you integers. If you call as it you've written (i.e., omit the range) then it actually gives you a decimal number from 0 to 1. |