Question Implementing a SPECIAL system.

Surreal Killa

Member
Joined
Apr 30, 2009
Messages
11
Location
Melbourne, Victoria, Australia.
Programming Experience
Beginner
If you're not familiar:

VB.NET:
Dim SPECIAL As Acronym = "Strength, Perception, Endurance, Charisma, Intelligence, Agility, and Luck."

The game I'm working on is going to use this style of system.

Here's a screenshot:

screenczm.png


What I need to do is:

1. All SPECIAL values have to be between 1 and 10. [DONE]
2. When adding and subtracting from any SPECIAL; update remaining points accordingly.
3. If remaining points = 0, then you cannot increase any SPECIAL.
4. If remaining points <> 0, then you cannot continue until all are spent.

I'm up to task 2, but having some difficulty.

I managed to figure out how to do it for one. For example:

VB.NET:
Sub Numeric_Up_Down_Strength_Value_Changed() Handles Numeric_Up_Down_Strength.ValueChanged
        Label_Remaining_Value.Text = CStr(10 - Numeric_Up_Down_Strength.Value)
End Sub

But I'm having trouble figuring out how to make it work for all. I've had some ideas but upon trying to implement them I've been having data type conversion errors.
 
Last edited:
You need to handle the Enter event of all the NUDs. You need to get the current value of the NUD and store it in a member variable. You then need to calculate the Maximum for that NUD. You would do that by adding the total remaining points to its current value, then select the lesser of that sum and 10. E.g. if there are only 2 remaining points and the NUDs value is currently 5 then you set the Maximum to 7 (2 + 5 < 10), but if the there were 15 remaining points then you'd set it to 10 (15 + 5 > 10).

You'd also handle the ValueChanged event of all the NUDS. When the value changes you would subtract the previous current value (which you stored in a member variable as above) from the current value and subtract the difference from the remaining points. You would then update your member variable with the current value.
 
Thank you for your help. I'm having trouble doing what you said. Would it be possible of you to show me an example code? Also, I'm not really familiar with the term member variable. Is this a global variable that can be modified by any sub? By which I mean: if I defined X, and gave it a value of 0, then altered the value of X to be 1 in sub1, then in sub2 X would still hold the value of 1, not 0. If that is the case; I'm not sure how to do that. Thank you for your help.
 
Local variables are declared inside a method; member variables are declared outside a method.

Have a go at what I suggested, step by step. You don't have to do it all in one go. For instance, the first thing I said was:
You need to handle the Enter event of all the NUDs.
Can you do that? Are you familiar with creating event handlers?
 
I think I can do member variables then. Do you mean like if I was inside a Sub and declared a variable then it would be a local variable, and if I was outside the Sub and declared a variable then it would be a member variable? If so, I can do that. I just wasn't sure if the changes I made in each Sub were supposed to be carried over to the next. As for the enter event, I've done that part.
 
The idea of a member variable is that its value can be retrieved or set from any method and its value is retained for the life of the object it's a member of, unlike local variables that only exist for the duration of a method call. As soon as the method completes its local variables cease to exist. They are then recreated with their inital values again next time you call the method.
As for the enter event, I've done that part.
Excellent. The next part is:
You need to get the current value of the NUD and store it in a member variable.
Now that you know what member variables are you can do that part. Note that you need another member variable for the number of points remaining. Once you've got that happening you should post your code so we can make sure you're on the right track.
 
I'm still not 100% on the member variable. But is this correct?

VB.NET:
    Dim RemainingValue As Decimal
    Dim CurrentValue As Decimal
    Dim MaximumValue As Decimal

    Sub Numeric_Up_Down_Strength_Enter() Handles Numeric_Up_Down_Strength.Enter
        With Numeric_Up_Down_Strength
            CurrentValue = .Value
        End With
    End Sub

Also it won't let me convert a string to integer or decimal, which is strange since I've never had this problem before.

VB.NET:
Dim RemainingValue As Integer = CInt(Label_Remaining_Value.Text)
Or
VB.NET:
Dim RemainingValue As Decimal = CDec(Label_Remaining_Value.Text)

Results in no errors, but upon running it crashes saying converting string to integer/decimal not allowed.

EDIT:

Actually no, earlier it was telling me the data type conversion was the problem when I was trying something else, but now it's saying:

An unhandled exception of type 'System.InvalidOperationException' occurred in Game.exe

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

For some reason I think it just doesn't like my number being a label. :confused:
 
Last edited:
Why would you need to convert the Text of the Label to a number? You've already got the number. The Text of the Label gets set from that number in the first place so why would you need to convert it back?

Also, there's no point using a With block to refer to one property.
 
True about the with block, but I was anticipating inserting more code there. As for the number, the number is of the data type string, because it is a label. Option Strict disallows this.

Some number = some text = won't work because it's text, so it needs to be changed to a number.
 
How do I make a variable that can be updated? For example:

VB.NET:
Dim Variable As Type = Value

Sub Button1_Clicked() Handles Button1.Clicked
Variable = NewValue
End Sub

Sub NewSub()
'The value of Variable will be 'Value' inside this sub. But I want it to be 'NewValue'...
End Sub
 
Back
Top