I always declare my variables before I use them, even though I don't have to.
Not declaring them is bad practice.
For example you don't want to accidentally run r++; before you set r=0; or in flash var i:Number = 0;.
It also creates a kind of a variable lookup table:
foobar = 5; //The number of aliens to start with
name = "SuperAwesomeDude"; //Your Name
temp = null; //temporary variable
Anyways, it is good practice.
Most languages require it (GM probally is set up to check if the variable is defined whenever you use a new variable, that slows down execution time)
Mist (PS Sorry for the rant)
True, and for variables referenced frequently I do the same, but for variables used for split second tasks it seems an unnecessary effort. Here's two examples from Tides of Fáden:
P1LVL = 1
P1EXP = 500
P1HP = 50
P1maxHP = 50
P1ATK = 0
P1DEF = 0
P1gold = 0
P1weapon$ = ""
P1shield$ = ""
P1boots$ = ""
P1item1$ = ""
P1item2$ = ""
P1item3$ = ""
P1item4$ = ""
P1item5$ = ""
P1item6$ = ""
P1item7$ = ""
P1item8$ = ""
P1item9$ = ""
P1name$ = USERASK Name your character.
IF P1name$ = "" THEN GOTOCARD 249
replacecheck = 1
namecheck = FILEEXISTS $P1name$$
IF namecheck = 1 THEN replacecheck = YESNOALERT A character with that name already exists. Do you want to replace that character with a new one?
IF replacecheck = 0 THEN GOTOCARD 249
In the first example I predefined the values of numerous variables even though their values were 0 because they're important elements throughout the entire game. In the second example however, variables such as "replacecheck" and "namecheck" are only useful to this particular routine, (which is small) then they're no longer needed in the game. Having to predefine vars like that seems a waste of time.
Once you get used to programming in C, declaring everything is second nature... I usually just declare variable that I will most likely need to use in the game opening script.
Heres an example of using parameters/ returns in SC... it was developed by Plaza Redd and is extremely useful.
Method Name: ChangeField$
Parameters: source$, separator$, fieldNumber, output$
Return Type: String
Code:
LET tempstr$ = ""
IF COUNTFIELDS(source$, separator$) < fieldNumber THEN
STOPALERT "There are not enough fields in that string!"
ELSE
IF fieldNumber = 1 THEN
LET tempstr$ = output$
FOR x = 2 TO COUNTFIELDS(source$, separator$)
LET tempstr$ = tempstr$ + separator$ + NTHFIELD$(source$, separator$, x)
NEXT
ELSE
IF fieldNumber = COUNTFIELDS(source$, separator$) THEN
FOR x = 1 TO COUNTFIELDS(source$, separator$)-1
LET tempstr$ = tempstr$ + NTHFIELD$(source$, separator$, x) + separator$
NEXT
LET tempstr$ = tempstr$ + output$
ELSE
FOR x = 1 to fieldNumber-1
LET tempstr$ = NTHFIELD$(source$, separator$, x) + separator$
NEXT
LET tempstr$ = tempstr$ + output$
FOR x = fieldNumber+1 TO COUNTFIELDS(source$, separator$)
LET tempstr$ = tempstr$ + separator$ + NTHFIELD$(source$, separator$, x)
NEXT
END IF
END IF
END IF
RETURN tempstr$
Example Code:
LET string$ = "Apple:Banana:Peach:Tomato"
LET string$ = ChangeField$(string$, ":", 4, "Orange")
Vb.net is just like Rb, though you have functions and subroutines.
Ex Code:
'Beginning of code:
Public Class Form1()
Dim GameOver As Boolean = False
Dim YouWin As Boolean
Dim AlianHp As Integer = 40 '40 Hitpoints
'Not going to show all code so...
Private Sub AlianHit() 'This sub is called when Alian is hit by bullet
AlianHp -= 5
Call CheckAlianHp(AlainHp) 'Calls Subroutine at bottom.
If GameOver = true then
If YouWin = false then MessageBox.Show("You Lose!")
If YouWin = false then MessageBox.Show("You WIN!")
Application.Exit
End if
End Sub
Public Sub CheckAlianHp(ByVal Hp As Integer)
If Hp <= 0 then
GameOver = True
YouWin = False
else
GameOver = False
end If
End Sub
End Class
That's just an example of variable and sub/method code. Not actual code because I didn't add in collision checking, writing sprites to screen, or anything else.
-Gandolf
Interesting language.
The only thing that I don't like about it is how it lacks clear structures.
That allows for simple shorthand but I find it a bit hard to read, for example:
If GameOver = true then
If YouWin = false then MessageBox.Show("You Lose!")
If YouWin = false then MessageBox.Show("You WIN!")
is in your code
That is a bit hard to understand.
I would always write:
If (GameOver == true) {
if (YouWin == false) {
MessageBox.Show("You Lose!");
}
if (YouWin == true) {
MessageBox.Show("You WIN!");
}
}
Just to make it more organized (I doubt that that is proper code :P)
Mist
If You are good at C, why do you still use GM/SC :-\\
It's one thing to understand C syntax, but quite another to program a game with it.
LET tempstr$ = ""
// initalizes a empty string to work with
IF COUNTFIELDS(source$, separator$) < fieldNumber THEN
STOPALERT "There are not enough fields in that string!"
// if the number of fields seperated by separator$ in source$ are smaller than fieldNumber, then display the message
ELSE
IF fieldNumber = 1 THEN
LET tempstr$ = output$
FOR x = 2 TO COUNTFIELDS(source$, separator$)
LET tempstr$ = tempstr$ + separator$ + NTHFIELD$(source$, separator$, x)
NEXT
// tempstr$ = tempstr$ + separator$ + the field value that comes before the x (x is given values by the loop so every field value is accounted for) occurance of seperator$ in source$
ELSE
IF fieldNumber = COUNTFIELDS(source$, separator$) THEN
FOR x = 1 TO COUNTFIELDS(source$, separator$)-1
LET tempstr$ = tempstr$ + NTHFIELD$(source$, separator$, x) + separator$
NEXT
// tempstr$ = tempstr$ + (the field value that comes before the x [x is given values by the loop so every field value is accounted for] occurance of seperator$ in source$)+ separator$
LET tempstr$ = tempstr$ + output$
ELSE
FOR x = 1 to fieldNumber-1
LET tempstr$ = NTHFIELD$(source$, separator$, x) + separator$
//tempstr$ = (the field value that comes before the x [x is given values by the loop so every field value is accounted for] occurance of seperator$ in source$)+ separator$
NEXT
LET tempstr$ = tempstr$ + output$
FOR x = fieldNumber+1 TO COUNTFIELDS(source$, separator$)
LET tempstr$ = tempstr$ + separator$ + NTHFIELD$(source$, separator$, x)
// tempstr$ = tempstr$ + separator$ + the field value that comes before the x (x is given values by the loop so every field value is accounted for) occurance of seperator$ in source$
NEXT
END IF
END IF
END IF
RETURN tempstr$
I commented the things that might be hard to understand.