Discussion

Is this collect?

0

japanese 2020-01-10 09:41

DO
CALL SUB1
CALL SUB2
CALL SUB3
LOOP

SUB1
(move main char)
SUB2
(move enemy)
SUB3
(something…)

like this.
Is this collect?


was8bit 2020-01-10 10:49

Almost....

DO
CALL MOVE_CHAR
CALL MOVE_ENEMY
CALL ANOTHER_SUB
LOOP

SUB MOVE_CHAR
(move main char)
END SUB

SUB MOVE_ENEMY
(move enemy)
END SUB

SUB ANOTHER_SUB
(something…)
END SUB


was8bit 2020-01-10 11:00 (Edited)

(Use of parenthesis....)


IF you do not need to give the SUB information (usually because it is using GLOBAL defined variables) then the CALL SUBNAME and SUB SUBNAME should be plain, without parenthesis...

BUT, if you need to include either variables or raw values like

CALL MOVE_PLAYER (PLAYERX,PLAYERY,-1,0)

Then you just MUST use parenthesis and the number of data must match

SUB MOVE_PLAYER(PX,PY,MX,MY)
...
END SUB

SO, if the SUB is defined with parenethesis needing 4 data, the CALL SUB must also use parenthesis and must input 4 data

The CALL SUB can use any variable used by the main program, or raw data like 23 or "JOE"... but the defined SUB SUBNAME ..the variable types must match the inputed data, like NumberIn or StringIn$....





was8bit 2020-01-10 11:07 (Edited)

(Use of variables and data... may or may not change...)

Example:

CALL MOVEPLAYER(PLAYERX,PLAYERY,-1,0)


SUB MOVEPLAYER(X,Y,MX,MY)
X=X+MX
Y=Y+MY
END SUB

..... notice that the names of the variables dont have to be the same... all variables used inside a sub (should they acidentally be the same as other variables in the main program) will NOT interfer with variables in the same program... all variables inside a SUB are private and secret to inside the SUB only..

So, for example, if PLAYERX is inputed as 5, then inside the sub, inputed X is now 5... but the SUB changes X, it become X=5-1=4.... since the variable PLAYERX was used in the CALL, and since the matching X variable has been changed, the SUB DOES change PLAYERX to match the new value, and PLAYERX in the main program is updated to be 4

If your main program also used X,Y... the inside SUB x,y will not change any X,Y used in your main program


was8bit 2020-01-10 11:13 (Edited)

(How to reuse SUB with different variables....)

If your enemies move identically to your player, you can actually reuse the SUB with different inputed variables... like...

SUB MOVETANK(X,Y,MX,MY)
X=X+MX
Y=Y+MY
END SUB

Then reuse this sub for different things, like

IF RIGHT(0)THEN CALL MOVETANK(PLAYERX,PLAYERY,-1,0)

or

IF RND(10)=0 THEN CALL MOVETANK(ENEMYX,ENEMYY,-1,0)

Both calls input their own variables,,the same sub receives the data, processes the data, and returns data based on what variables where used in the CALL

So when PLAYERX,PLAYERY is used, these variables get updated
When ENEMYX,ENEMYY is used, these variables get updated


was8bit 2020-01-10 11:28 (Edited)

(Comparing GOSUB with SUB....)

When you use GOSUB LABELNAME then LABELNAME: RETURN then all variables used are the same variables, it is really no different than if you just typed code in directly.. the only use for GOSUB /RETURN is if you are typing in identical code several times, and to save typing the identical code many times you just type it once inside a LABEL: RETURN and use GOSUB LABEL

SUB is much more powerful... you can
1) reuse the same code to update different inputed variables
2) not worry that variables used inside your SUB will accidentally change an identical variables used elsewhere in your program
3) all variables used inside a SUB are temporary and once a SUB has finished it discards all values used inside the SUB (nothing inside a SUB is remembered)

IF you need a variable accessable both inside a SUB and outside a SUB, .... OR... you need a variable special to a SUB to not loose its value from one CALL to another CALL (like a setting) then you must use GLOBAL variables, defined at the beginning of your program...


was8bit 2020-01-10 11:38

(Correct) ;)


was8bit 2020-01-10 11:45

... I know this is a lot of information, and I know I can ramble... so, please ask any questions to clarify anything :)


japanese 2020-01-10 13:35

Thanks, was8bit!
Anyway, I will try it :)


was8bit 2020-01-10 15:23

Cool :) happy to help :)


Log in to reply.