Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UH little help guys
#1
So my project on the world is something like this

Code: (Select All)
Dim money As Integer
Dim strength As Integer
Dim speed As Integer
Dim babes As Integer
Dim balls As Integer
Dim cash As Integer
Dim directions As Integer


'intro
Screen 0
Color 4, 0
Locate 8, 38
Print "The World"
Locate 8, 38
Print "Is Loading"
_Delay 5

'opening
Cls
Print "You finally got released from Prison at Kulai Headquarters"
Print "you looked at the sky and the people below it, you began"
Print "wondering, that you can become rich and powerfull"
Print "so you began your journey."
_Delay 10
GoSub 10


10 Sub MainPart
    Cls
    Print "Whats your name?"
    Input name$
    Sleep 2
    Cls
    Print "You are outside of Kulai Headquarters,"
    Print "Where will you go?"
    Print "(NORTH)"
    Print "(SOUTH)"
End Sub


Sub Options (directions)
    If x = north Then
        Print "You Found A Market of Stores"
    ElseIf x = south Then
        Print "You went to Kulai Headquarters"
    End If
End Sub

so what i want is after the opening i want it to directly jump to Sub MainPart anyone knows how to do it xD
[Image: giphy.gif]
Reply
#2
Hi. There is a difference between calling a part of a program that is labeled SUB and END SUB and between calling GOSUB and RETURN. A significant difference is also that variables that are not globally shared have no value or the same as in the part of the program between SUB and END SUB, if variable the same name in not declared as SHARED or is not used as parameter in SUB .

Sub example:

Code: (Select All)

'SUB use

Dim Shared PName As String 'try delete me and run it again!

NameIn
Print "Name:"; PName$

Sub NameIn
    Input "input name:"; PName$
End Sub

GoSub example:

Code: (Select All)

'GoSUB use

GoSub NameIn
Print "Name:"; PName$
End

NameIn:
Input "input name:"; PName$
Return

Your code: (here is player name not returned to program, because variable name$ is not used as parameter for SUB or is not declared as shared)

Code: (Select All)

Dim money As Integer
Dim strength As Integer
Dim speed As Integer
Dim babes As Integer
Dim balls As Integer
Dim cash As Integer
Dim directions As Integer


'intro
Screen 0
Color 4, 0
Locate 8, 38
Print "The World"
Locate 8, 38
Print "Is Loading"
_Delay 5

'opening
Cls
Print "You finally got released from Prison at Kulai Headquarters"
Print "you looked at the sky and the people below it, you began"
Print "wondering, that you can become rich and powerfull"
Print "so you began your journey."
_Delay 10
'GoSub 10
MainPart


Sub MainPart
    Cls
    Print "Whats your name?"
    Input name$
    Sleep 2
    Cls
    Print "You are outside of Kulai Headquarters,"
    Print "Where will you go?"
    Print "(NORTH)"
    Print "(SOUTH)"
End Sub


Sub Options (directions)
    If x = north Then
        Print "You Found A Market of Stores"
    ElseIf x = south Then
        Print "You went to Kulai Headquarters"
    End If
End Sub


so here is your program, because NAME is reserved statement in QB, so is here used PlayerName as SUB parameter and this is returned to program:

Code: (Select All)

Dim money As Integer
Dim strength As Integer
Dim speed As Integer
Dim babes As Integer
Dim balls As Integer
Dim cash As Integer
Dim directions As Integer


'intro
Screen 0
Color 4, 0
Locate 8, 38
Print "The World"
Locate 8, 38
Print "Is Loading"
_Delay 5

'opening
Cls
Print "You finally got released from Prison at Kulai Headquarters"
Print "you looked at the sky and the people below it, you began"
Print "wondering, that you can become rich and powerfull"
Print "so you began your journey."
_Delay 10
'GoSub 10
MainPart name$
Print "Player name is:"; name$


Sub MainPart (PlayerName As String)
    Cls
    Print "Whats your name?"
    Input PlayerName$
    Sleep 2
    Cls
    Print "You are outside of Kulai Headquarters,"
    Print "Where will you go?"
    Print "(NORTH)"
    Print "(SOUTH)"
End Sub


Sub Options (directions)
    If x = north Then
        Print "You Found A Market of Stores"
    ElseIf x = south Then
        Print "You went to Kulai Headquarters"
    End If
End Sub

or next option how do it without parameter in SUB:

Code: (Select All)

Dim money As Integer
Dim strength As Integer
Dim speed As Integer
Dim babes As Integer
Dim balls As Integer
Dim cash As Integer
Dim directions As Integer

Dim Shared PlayerName As String

'intro
Screen 0
Color 4, 0
Locate 8, 38
Print "The World"
Locate 8, 38
Print "Is Loading"
_Delay 5

'opening
Cls
Print "You finally got released from Prison at Kulai Headquarters"
Print "you looked at the sky and the people below it, you began"
Print "wondering, that you can become rich and powerfull"
Print "so you began your journey."
_Delay 10
'GoSub 10
MainPart
Print "Player name is:"; PlayerName$


Sub MainPart
    Cls
    Print "Whats your name?"
    Input PlayerName$
    Sleep 2
    Cls
    Print "You are outside of Kulai Headquarters,"
    Print "Where will you go?"
    Print "(NORTH)"
    Print "(SOUTH)"
End Sub


Sub Options (directions)
    If x = north Then
        Print "You Found A Market of Stores"
    ElseIf x = south Then
        Print "You went to Kulai Headquarters"
    End If
End Sub

Just for the rest of the program: Options as it is written cannot work, because the parameter at the SUB input is called directions, but then in the sub it refers to the variable X, which, because it is not globally shared, will always 0 and the directions variable is not used in sub.


Reply
#3
(08-31-2024, 05:32 AM)Petr Wrote: Hi. There is a difference between calling a part of a program that is labeled SUB and END SUB and between calling GOSUB and RETURN. A significant difference is also that variables that are not globally shared have no value or the same as in the part of the program between SUB and END SUB, if variable the same name in not declared as SHARED or is not used as parameter in SUB .

Sub example:

Code: (Select All)

'SUB use

Dim Shared PName As String 'try delete me and run it again!

NameIn
Print "Name:"; PName$

Sub NameIn
    Input "input name:"; PName$
End Sub

GoSub example:

Code: (Select All)

'GoSUB use

GoSub NameIn
Print "Name:"; PName$
End

NameIn:
Input "input name:"; PName$
Return

Your code:

Code: (Select All)

Dim money As Integer
Dim strength As Integer
Dim speed As Integer
Dim babes As Integer
Dim balls As Integer
Dim cash As Integer
Dim directions As Integer


'intro
Screen 0
Color 4, 0
Locate 8, 38
Print "The World"
Locate 8, 38
Print "Is Loading"
_Delay 5

'opening
Cls
Print "You finally got released from Prison at Kulai Headquarters"
Print "you looked at the sky and the people below it, you began"
Print "wondering, that you can become rich and powerfull"
Print "so you began your journey."
_Delay 10
'GoSub 10
MainPart


Sub MainPart
    Cls
    Print "Whats your name?"
    Input name$
    Sleep 2
    Cls
    Print "You are outside of Kulai Headquarters,"
    Print "Where will you go?"
    Print "(NORTH)"
    Print "(SOUTH)"
End Sub


Sub Options (directions)
    If x = north Then
        Print "You Found A Market of Stores"
    ElseIf x = south Then
        Print "You went to Kulai Headquarters"
    End If
End Sub

ah i got it, thanks bro
[Image: giphy.gif]
Reply
#4
My previous post upgraded


Reply
#5
(08-31-2024, 05:51 AM)Petr Wrote: My previous post upgraded

No worries bro, i got it, Sub actually helps me alot organizing the codes Big Grin Heart
[Image: giphy.gif]
Reply




Users browsing this thread: 3 Guest(s)