Need help with DO LOOP WHILE

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
jolene
Posts: 2
Joined: Wed Jun 04, 2008 4:41 pm

Need help with DO LOOP WHILE

Post by jolene »

I have this function:

Code: Select all

Function CHEAPTE(glory,te,palace,tech,libs,discte,discpalace,disctech)
  CHEAPTE = -1
  DO
    CHEAPTE = CHEAPTE+1
  LOOP WHILE CPPTE(glory,te+CHEAPTE,palace,tech,1,discte) < CPPPALACE(glory,te+CHEAPTE,palace,tech,1,discpalace)
End Function
It turns out to be an infinite loop. I have used the CPPTE and CPPPALACE and a manual incrementer to simulate the CHEAPTE and I know that the 2 functions will evetually return values that will stop the loop - meaning the loop as I envision it. I can only imagine that the code is malformed in some way (te+CHEAPTE?). I have tried using a temp value and incrementing the temp rather than using te+CHEAPTE with no effect. If the starting values are such that CPPTE is already greater than CPPPALACE, the loop never executes and I get a value of 0 returned as expected. I've tried using WHILE WEND also, with no luck.

Yes, this is game related. Yes, I have successfully been creating functions and loops previously.
OOo 2.3.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Need help with DO LOOP WHILE

Post by Villeroy »

Sigh! :roll:

Code: Select all

DO
    CHEAPTE = CHEAPTE+1
    x1 = CPPTE(glory,te+CHEAPTE,palace,tech,1,discte)
    x2 = CPPPALACE(glory,te+CHEAPTE,palace,tech,1,discpalace)
  LOOP WHILE x1 < x2
Put the cursor in the first row of that function and hit F9 to add a stop mark. Then trigger the function switch back to the IDE and hit F8 to walk through step by step. Add x1 and x2 among other variables of interest to the watch window and watch them change (or not).
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
jolene
Posts: 2
Joined: Wed Jun 04, 2008 4:41 pm

Re: Need help with DO LOOP WHILE

Post by jolene »

The problem seems to be that when the value "te" goes out via a call to CPPTE, it comes back incremented. So "x1" and "x2" as in your example are never calculated with the same value for "te" so there;s not a proper comparison.
OOo 2.3.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Need help with DO LOOP WHILE

Post by Villeroy »

Pass a copy of "te" or pass it by value:

Code: Select all

Sub test
x = 0
y = increase(x)
REM x remains 0 y=1
print x, y
End Sub

REM take a copy of the passed argument rather than reference
Function increase(byVal x)
REM increase a copy of x by 1
x = x +1
increase = x
End Function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply