[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  VBscript
. . -> [Subject]  Overflow error in gold counter

Overflow error in gold counter

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Sleeve   (31 posts)  [Biography] bio
Date Wed 18 Sep 2002 08:27 PM (UTC)

Amended on Wed 18 Sep 2002 08:29 PM (UTC) by Sleeve

Message
I keep getting an overflow error in my gold counting sub-routine and I can't figure it out. I thought a long variable was supposed to be able to hold up to 2,147,483,647? I only had around 1,000,000 gold on me and I had gained 15,000 gold from a mob, so this sub-routine kicked in:

Sub OnReceiveGold(strTriggerName, strOutput, arrWildCards)
gold = cLng(gold) + cLng(arrWildCards(1))
End Sub


... and I got an overflow but I see no reason why I should have an overflow. If anyone can help me with this I would really appreciate it. Thanks.
[Go to top] top

Posted by Vaejor   (120 posts)  [Biography] bio
Date Reply #1 on Wed 18 Sep 2002 10:45 PM (UTC)
Message
It may help if you additionally include your trigger and the full line that it triggered on so that the entirety of the process can be checked.
[Go to top] top

Posted by Sleeve   (31 posts)  [Biography] bio
Date Reply #2 on Wed 18 Sep 2002 11:25 PM (UTC)
Message
I should've included them originally, sorry about that.

My Trigger:

^You get (.*?) gold coins from (.*?) of (.*?)\.$

The line sent by the mud:

You get 15002 gold coins from the burnt corpse of the troll.


All the trigger does is call the OnReceiveGold sub-routine and as you can see, it simply does this one calculation.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Thu 19 Sep 2002 12:29 AM (UTC)
Message
And where is "gold" defined? Do you have:

dim gold

somewhere in global scope?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Sleeve   (31 posts)  [Biography] bio
Date Reply #4 on Thu 19 Sep 2002 12:39 AM (UTC)
Message
Yeah, I have gold defined as a global variable. Right at the top of my script, before any subroutines, I have dim gold.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Thu 19 Sep 2002 12:44 AM (UTC)

Amended on Thu 19 Sep 2002 12:46 AM (UTC) by Nick Gammon

Message
I cannot reproduce the problem. Trying in immediate mode the following, which approximates what should have happened to you ...


dim gold

gold = empty

'
' get gold up to 1000000
'

for i = 1 to 100
gold = cLng (gold) + cLng (10000)
next

' try adding the extra bit

gold = cLng(gold) + cLng ("15002")

world.note "result = " & gold


I get the result:


result = 1015002

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Sleeve   (31 posts)  [Biography] bio
Date Reply #6 on Thu 19 Sep 2002 01:19 AM (UTC)

Amended on Thu 19 Sep 2002 01:20 AM (UTC) by Sleeve

Message
Thanks Nick, I'll just have to take a harder look at my script and see what else could be causing it. The only thing is that when I initialize the gold variable I have:

gold = cLng(0)


whereas you have:

gold = empty


I've only been using VBScript for a little while so I'm not very familiar with it, am I supposed to initialize variables with empty or is something about the way I initialized mine producing a problem? Thanks for the help.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Thu 19 Sep 2002 02:21 AM (UTC)
Message
gold = cLng(0)

looks great to me.

All I can suggest is adding a "display" to the trigger (the script or the trigger itself).

eg.

Wildcard 1 is %1

Set it to "send to output". Then you might find if a bogy number is making its way through.

If that is the case you could make the trigger a reqular expression and look for numbers only (eg. [0=9]+ )

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Shadowfyr   USA  (1,786 posts)  [Biography] bio
Date Reply #8 on Thu 19 Sep 2002 07:35 PM (UTC)
Message
I hate the fact you can't specifically cast variable to specific types explicitly. I have run into the same problem with one of my scripts and it all comes down to the way VBscript guesses what you need, instead of doing what you tell it to.

another example>

a = "1,2,3,4"
b = "2,3,4,5"
c = split(a,",")
d = split(b,",")

e = c(2) + d(1)

In some cases like above you get an error, despite the fact it should work, but if you do:

e = Cint(c(2)) + Cint(d(1))

you may end up with 3, instead of 6, because the scripting engine gets confused, decides that d(1) is a string and returns 0 or something...

So some rules of thumb (in theory)>
1. If you don't yet have a value to put in a variable to start with, don't try to use cLng(0) or the like to cast it. VBscript will prabably return a long, but realizing that it will fit into a regular integer, will cast the variable to fit the value itself, not the type you intended.

2. Don't use stuff like a = 1 or a = "Hello", etc. without first using dim a. For some reason plugins won't let you do this, even though it is perfectly valid and will work correctly in a normal script. (Is this something resulting from the way you did the plugins Nick?)

3. If you need to use an element in an array, use a temporary variable to store the value for calculations, so it won't get ignored.

4. Also with arrays. A split array is 'apparently' somehow different than one that has been defined directly, thus you can't expand or contract it with 'redim preserve <array>(new size)'. It will resize (maybe), but does not preserve the contents of the array as it should. This means if you need to do such a thing, you literally have to make a new array that is the same size, copy the split array into it, then resize the temporary one. It is generally easier to
avoid this completely 'if possible'.

There may be other quirks as well, but these are the ones I have run into. Almost makes one wish there was an alternative version of basic that ran as a COM based script language. :p
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Thu 19 Sep 2002 10:52 PM (UTC)
Message
Quote:

2. Don't use stuff like a = 1 or a = "Hello", etc. without first using dim a. For some reason plugins won't let you do this, even though it is perfectly valid and will work correctly in a normal script. (Is this something resulting from the way you did the plugins Nick?)


In the constants.vbs file which you probably included is a line:


option explicit


This line makes sure you dimension all variables. I can't see a reference to turning this off in the help, in fact it says it must appear before any other statements.

You could take it out of constants.vbs, but it is probably good practice to leave it there.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Thu 19 Sep 2002 10:53 PM (UTC)
Message
In fact, you are better off adding it to the start of all your scripts. That way you can catch spelling errors where you think you are using one variable but are really using two.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Sleeve   (31 posts)  [Biography] bio
Date Reply #11 on Fri 20 Sep 2002 02:13 AM (UTC)
Message
Thanks for the help Shadowfyr, I'll have to keep an eye out on some of those "quirks" you mentioned about VBScript. :)
[Go to top] top

Posted by Shadowfyr   USA  (1,786 posts)  [Biography] bio
Date Reply #12 on Fri 20 Sep 2002 06:47 AM (UTC)
Message
Ah.. Didn't think of that being in there Nick. That would definitely explain it. lol It also explains why one that I built by hand before the wizard was out actually does use the short cut and actualy works right.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,055 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]