Yet Another Hopeless RPG Programmer

CRSdefiance
07-30-2001, 09:25 PM
Well, maybe I'm not completely hopeless...I DO have others helping me.

Anyway, I have several questions that I need answered (and I will most definitely be back when they pose a threat to the production of this RPG), but right now I have a fairly simple one...for my sake, I HOPE it is simple...

I am creating a sort of mini program that will allow my team to view the effects of a character leveling up, based on a formula that they input. I have already created the variables to be used in the formula, but I have a problem...when the text field has a formula in it rather than a solid number, and I attempt to calculate, I get zero as the answer every time. My idea is that VB6 is reading this as text rather than a user created formula. Can anyone help me?

dcl3500
07-30-2001, 09:34 PM
Maybe not the solution but a suggested course of action. First though you are right VB is reading it as just a string. So what you need to do is tell it how to read it as a formula. Tear the text apart using instr and mid functions using your operators to search for after youv'e done this then put it all back together and evaluate it as the function you wanted in the first place. I think a better method would be to have several textboxes where your users can put the required information in and then put those into your function and evaluate it. That way there is less chance they will enter incorrect data in the first place.

Don

Time is the best teacher; unfortunately it kills all its students.

Thinker
07-30-2001, 09:42 PM
dcl3500's suggestion is the best in the long run, however, if you are in a bind time-wise and have Access you could add a reference to the Access Object Library and use the Eval("stringformula") function.

I think therefore I am... sometimes right. images/icons/wink.gif

CRSdefiance
07-30-2001, 09:52 PM
Yes, I have Access, but there is a problem...you see, I've never had a programming class before...ever, and I'm trying to learn this all from nothing. Could you guys please give me some examples or tell me how to do this? Most of this I can handle, but when you start getting into different functions or using Access, I'm lost!

I did think of using the textboxes thing, but I want the people helping me to be able to input their own formulas based on whatever mood strikes them--I'm not really interested in preventing user mistakes.

dcl3500
07-30-2001, 09:52 PM
Hey Thinker great idea, never would have thought of that one.

RE: Yet Another Hopeless....

If you looked around the web I know I've seen a formula evaluator control somewhere.

Don

Time is the best teacher; unfortunately it kills all its students.

Thinker
07-30-2001, 09:59 PM
I got the idea right here in another post. The eval function works like this...
<pre>
MsgBox Eval("1 + 3")
</pre>
displays 4.

I think therefore I am... sometimes right. images/icons/wink.gif

dcl3500
07-30-2001, 10:00 PM
http://download.cnet.com/downloads/0-14480-100-1565854.html (http://url)

Download this control. I haven't used it. I just found it on cnet.com

Once you have installed it, go to project, components, find it and add it to your project. It will probably look just like a textbox on your form. I'll download it too and then maybe I can help.

Don

Time is the best teacher; unfortunately it kills all its students.

CRSdefiance
07-30-2001, 10:35 PM
Thanks for the link...I downloaded and tried it out...it is really simple to use...but it is still givine me zero as the answer, and not coming up with any errors... As an example, I have put in some random numbers like "1.5 * str" and set the final Strength box equal to that calculation. Str is already defined as the value of the original Strength value, and I have the code updating that value before the calculation takes place.

dcl3500
07-30-2001, 11:18 PM
Private Sub Command1_Click()
With Equator1
.Equation = Text1
.Calculate
Text2 = .Result
End With
End Sub

You need text1, command1, text2, equator1 on a form
then try that code.

Don



Time is the best teacher; unfortunately it kills all its students.

CRSdefiance
07-30-2001, 11:30 PM
Well, that got me a little bit closer. I added text boxes so I could check the lastsolved and solveerror. In the lastsolved box I get the equation, 1.5*str showing that it DID read the equation this time, and in the solveerror I get "0" indicating that it has no error...but I still get the solution as 0.

I'm stumped.

dcl3500
07-30-2001, 11:37 PM
Show me your code.

Don

Time is the best teacher; unfortunately it kills all its students.

CRSdefiance
07-30-2001, 11:56 PM
in general I have:
Dim str as integer

in the frame with the input I have txtStrengthCalc with a default text value of "1.5*str"

in the frame showing total character stats, I have txtStrength with a default text value of "5" for level 1

In the code for cmdCalculate, I have:

Private Sub cmdCalculate_Click()
str = Val(txtStrength.Text)

With Equator1
Equator1.Equation = txtStrengthCalc
Equator1.Calculate
txtStrength = Equator1.Result
End With

dcl3500
07-31-2001, 12:11 AM
Ok try this

Private Sub cmdCalculate_Click()
With Equator1
.Equation = txtStrengthCalc
.Calculate
txtStrength = .Result
End With

End Sub

You didn't do anything with this so I didn't put it back in
str = Val(txtStrength.Text)
and remember when you use the with statement you don't need the (in this case) equator1.whatever. That's why you used the with in the first place.

I tested it with one of the examples in the readme.txt and it worked.

Let me know if it works for you. Tommorow though it's 1:00AM my time and I have to go do this for a living tommorrow images/icons/smile.gif

Don

Time is the best teacher; unfortunately it kills all its students.

BillSoo
07-31-2001, 12:24 AM
As a guess, I'd say you are getting 0 because str is 0.

The equation evaluator control probably has no idea what str is. So it assigns it a value of 0.

Incidentally, I *think* str is a reserved word in VB.



"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

Gamer X
07-31-2001, 08:31 AM
Yes, STR and STR$ are reserved in Visual Basic, it is a function that converts an Integer to a String.

Game on,
Gamer X


Smile, it makes people wonder what you're up to. :)

CRSdefiance
07-31-2001, 05:11 PM
Well, so far I've run it through and gotten zero..again. My original idea was that a value like "int", "vit", "hp" etc could be plugged into a formula in a textbox, then worked out like it had been programmed all along. If I use only numbers in the formula, it works out fine, but if I try to call on the variable, it won't work. I'll do some more testing, but it still isn't looking too good...

BillSoo
07-31-2001, 05:28 PM
I just took a look at the Equator control you downloaded. It does not appear to support variables.

However you can still get around this by use of the Replace function.

Suppose you have a bunch of variables like:

dim iStr as integer, iInt as integer, iDex as integer

and iStr=56, iInt = 77 and iDex=82

(note that STR and INT are reserved words in VB so iStr and iInt are used instead)

Further suppose your user typed in something like
"STR * 3 + DEX + INT/2"
which is stored in sEqn (a string variable)

If you passed that to Equator, it would have no idea what STR, DEX and INT were supposed to be. However, before passing it, you preprocess it using the REPLACE function.

sEqn = Replace(sEqn,"STR",cstr(iStr)) 'replace all occurrances of STR with the value of iSTr
sEqn = Replace(sEqn,"DEX",cstr(iDex))
sEqn = Replace(sEqn,"INT",cstr(iInt))

Now sEqn will contain "56*3 + 82 + 77/2" which Equator CAN handle...




"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

CRSdefiance
07-31-2001, 08:37 PM
Replace function...right...I think that might be another thing I haven't learned yet... First of all, going back to an earlier post, what does the "with" thing do, what does "replace" do, and can you please put it like dcl3500 did in his example? I'm not just trying to get everyone else to solve my problems, but I really want to learn what some of these things do or how they can be used so I will know to use them in the future.

Thinker
07-31-2001, 08:49 PM
I'll take a crack at putting them together.
Assuming you only need to change out the str variable with its value it would be like this
<pre>
Private Sub cmdCalculate_Click()
With Equator1
'txtStrengthCalc = UCase(txtStrengthCalc) 'Might need this line
.Equation = = Replace(txtStrengthCalc,"STR",cstr(iStr))
.Calculate
txtStrength = .Result
End With
End Sub
</pre>
What the With Block does is provide a shorthand way of
referencing the properties and methods of an object, in this
case the Equator Control object. When you use the With
Block you don't have to use the object variable, just the
.Property or .Method


I think therefore I am... sometimes right. images/icons/wink.gif

CRSdefiance
07-31-2001, 09:17 PM
WOO HOO!!!!! Bless you ALL!

I fixed a few mistakes (extra apostraphe and equal sign) and it finally worked! I think I can already feel my headache going away...lol

I have some more questions, but I will save those for when the time comes up if I can't work my way through them! Thanks guys...

TommyHM111
08-07-2001, 04:11 PM
I know how you feel ive never taking a programming class yet but ive read over 5 books and ive been programming for about 3 years now ... and im stil a newbie......

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum