On of my "stupiest" questions....

Cableguy

Expert
Licensed User
Longtime User
Hi all,

I'm trying to come up with a simple calcutation tool for an on-line game called travian...
I'm trying to automatiza the calculation of the time nned to travel from point a to b at a given speed...
For the ones not fammyliarized with the game,,, The units have diferents seepds, measured in squares per hour...

So, since I don't want to repeat the same code over and over, I thought about creating a calculation sub and pass the parameters...
But they are not being passed...
I just can't fugure out...

B4X:
.....
n=VLeg
Calculos(d,ds,fx,n)
Msgbox(TS)
End Sub

Sub Calculos
v=3600/n
Msgbox(v)
TS=(v*d)+((v*fx)*ds)
Format(TS,"n2")
Return(TS)
End Sub

Any thoughts?

I'm also trying to convert seconds to the time format (HH:MM:SS), any one have done that yet?
 

specci48

Well-Known Member
Licensed User
Longtime User
Hi Cableguy,

since we have local variables, you have to define the parameters in the called sub again:

Sub Calculos(d,ds,fx,n)
...
End Sub


Cheers
specci48


PS: There is no stupid question, there are only stupid answers...
 

Cableguy

Expert
Licensed User
Longtime User
Hi Cableguy,

since we have local variables, you have to define the parameters in the called sub again:

Sub Calculos(d,ds,fx,n)
...
End Sub


Cheers
specci48

I knew I was forgetting something....dahm....

Thnks Specci...:sign0188:
 

Cableguy

Expert
Licensed User
Longtime User
Ok, so calculations now occur but the TS var is not returned to the calling sub....Any thoughts????
 

specci48

Well-Known Member
Licensed User
Longtime User
:signOops: ... I forgot to answer to your second question.

If you want to convert seconds to HH:MM:SS you can do something like this.

B4X:
TimeFormat("HH:mm:ss")
Label1.Text = Time(seconds * cTicksPerSecond)

This works for 0 =< seconds < 86400 since 86400 seconds equals 00:00:00 again.


specci48
 

specci48

Well-Known Member
Licensed User
Longtime User
... since we have local variables, you have to assign the returning value to a variable...

TS = Calculos(d,ds,fx,n)
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
... since we have local variables, you have to assign the returning value to a variable...

TS = Calculos(d,ds,fx,n)

That doesn't work since TS is tne result of a mathematical expression....
Still in the help file it reads:
Return

--------------------------------------------------------------------------------

Ends the current sub and returns to the calling sub.
If Return is followed by a value, then the value will be passed to the calling sub.
Syntax: Return [Value]


Example:
Sub Button1_Click
If IsInteger (a) = true Then b=a
End Sub


Sub IsInteger (x)
If x = Int(x) Then Return true Else Return false
End Sub

So the code SHOULD work but it gives an "input in the wrong format" kind of error...

Solved:the paramters returned do not need to be inside brackets.....
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
Ok, back to the same stupid question:

I have in one sub....

TS= some mathematical operation
MsgBox(TS) ' Does show the result from the previous line...
Return TS ' Nothing is passed to the calling sub, WHY????
 

specci48

Well-Known Member
Licensed User
Longtime User
Can you post how the code looks like at the monent?

Edit: Have you implemented post #6 in your calling sub?
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
the two intervinient subs are:

B4X:
Sub Button1_Click
'---------------------------------------------
'Calcula a distancia a percorrer em Quadrados
dx=Abs(textbox1.Text-textbox3.Text)
dy=Abs(textbox2.Text-textbox4.Text)
d=Sqrt((dx*dx)+(dy*dy))
'---------------------------------------------
'---------------------------------------------
'Se a distancia for maior que 30 quadrados
'Faz o calculo incluindo a praça de Torneios
If d>30 Then
ds=d-30
d=30
Else 
ds=0
d=d
End If
fx="1."&num1.Value
Format(fx,"n1")
'---------------------------------------------
n=VLeg
Calculos(d,ds,fx,n)
Format (TS,"n3")
Msgbox(ts)
End Sub

Sub Calculos(d,ds,fx,n)
'---------------------------------------------
'Calcula As velocidades individuais das Tropas
'---------------------------------------------
'1Hora=3600segundos
'vel(seg)=3600/n(quadrados por tipo de unidade)
'Tempo=(vel x d)+((vel x fx)x ds)
v=3600/n
Msgbox(v)
Msgbox(d)
TS=(v*d)+(v*fx*ds)
Msgbox(ts)
Return TS
End Sub
 

specci48

Well-Known Member
Licensed User
Longtime User
Please implement post #6:
B4X:
TS = Calculos(d,ds,fx,n)
in the calling sub!

Here again, the resuilt of the called sub must be stored into the/a local variable of the calling sub...


specci48
 

Cableguy

Expert
Licensed User
Longtime User
Thanks again Specci...been a while since I coded something...So I'm a bit rusted....
 
Top