Discussion

@was8bit “Me trying to break down your code”

0

tactics_pvp 2020-02-09 14:23 (Edited)

@was8bit

Thank you for adding:
jump, gravity, fall, fail and Checkcall, Checkcell to my study code off of @Timos youtube lesson. I was able to learn an IMMENSE amount - but I still cannot remix it, due to “Syntax Error” assassinating me on every attempt I made.

What I break down below is for the code you posted for me in this thread:
https://lowresnx.inutilis.com/topic.php?id=999#post-6558
(Sorry I don’t know how to copy and paste game code in here like you guys do)

With my keyboard and iPad using iOS Split View feature - - I had NX manual on one side and NX running on the other, while at the same time taking notes on my third open app - notepad floating as a side app.

I will try to make it easy for you to follow along my questions and to gather what I am referring to. Please see my instructions below to see how:

I will place all of my questions inside 3 curly brackets {{{MY QUESTION or THOUGHTS IN HERE}}} These curly brackets will be placed to the right side, of the line of code, I am inferring to. Thanks again @was8bit. Let’s begin :)





GLOBAL FAIL {{{GLOBAL - - I’m paraphrasing the NX manual:: Global makes variables available to SUB programs. Simple variables only. Cannot be used inside a subprogram. Pretty straightforward. It’s a variable global access for the entire program - all aboard the variable train!}}}

{{{FAIL - - This a variable you created for global use, it speaks to the line of code several lines below in the IF STATEMENT that has the line of code stating: IF FAIL THEN FALL=0 also the FAIL appearing in the SUB MOVE code block. However, what is FAIL for? That I do not know}}}

GAMEPAD 1
BG 1
BG COPY 0,0,32,32 TO 0,0

X=64
Y=64

GRAVITY=0.1 {{{Cool! So this is how you create gravity (I feel like a digital god or something now) in my test, I removed the “.1” portion of the variable. By doing so, my character went flying off screen on the Y positive axis when I jumped.

The knowledge I gained here: NX communicates to the cells, that 0.1 is a “loose return” coordinate position, that occurs every time the player activates the jump variable button.

I say “loose return” because NX does not strictly place you back at the same position where you started, when you land - you basically get to land wherever}}}

FALL=0 {{{Now this one is interesting, from my tests, this is the “spawn” location of where your character first does a “fall in spawn” when you launch the game.

In my test, I replaced FALL=0 with FALL=500 and my character, would “fall in spawn” at the center top portion, of the screen... atop a platform that was hovering there.

When I reverted the variable back to FALL=0, my character would “fall in spawn” below that same platform and not above it. Grateful for this lesson. I didn’t even think about such things.. where I would want my character to spawn first time game launches or where to spawn after entering a new level. Cool lesson here}}}


DO
IF UP(0) AND FALL=0 THEN FALL=-2.5 {{{The height of the jump and jump ability. Nice! In my test I replaced “-2.5” with “0” and bye bye jump ability.

I DISCOVERED SOMETHING while testing this line of code... I am wondering if it’s a split view NX bug? or Apple Limitation? I’ll explain...

When in split-view with notepad on one side and NX on the other.

If I am typing on notepad using physical keyboard and the typing cursor is visible, and then I pick up my Xbox One S bluetooth Controller and try to move my character on the NX side of split-view... NX does not register my controller inputs.

Instead I have to do the following for NX to register that I pressing buttons on my controller.

(1) I have to tap “done” on top right side of notepad app, to make the cursor disappear.

(2) Once cursor disappears, I have to tap on the NX screen.

(3) NOW I can start controlling my character in on the NX side of the split-view.

I am curious as to why Apple or NX (whichever is causing these extra steps)

Doesn’t recognize... if I pick up my Xbox controller and start pressing buttons - the character on the NX app should just start moving regardless of what is going on the other side of split-view with Apple Note taking app.

Do you know if this is a Apple Limitation or a NX Bug? }}}

IF DOWN(0) THEN CALL MOVE(X,Y,0,1)
IF LEFT(0) THEN CALL MOVE(X,Y,-1,0)
IF RIGHT(0) THEN CALL MOVE(X,Y,1,0)


FALL=FALL+GRAVITY {{{Removing this variable, automatically sends character flying sky bound and out of the screen. So this variable, keeps the character anchored to the map.

Some confusion on my part here... The GRAVITY variable prevented the player from flying out of the screen when they jump - but this variable keeps the player from flying out of the screen without jumping?

Why is that? Does NX engine automatically send the characters flying out of the screens if this kind of variable is not created? If no, then why create this variable?}}}



IF FAIL THEN FALL=0 {{{When I removed this line of code, two things happened.

(1) prevented character from jumping.

(2) This one is crazy! my character would phase in and out when I would move him with the directional pad. It’s like he would disappear and reappear like a ghost, phasing in and out of reality - I have no idea what is the purpose of this code, but I’ll take a guess.

After some more observation, I noticed my character keeps falling. He would disappear by falling through one of the platforms I had him standing on, other platforms would prevent him from falling through them.

What I am gathering, this FALL code does the opposite of GRAVITY. It’s reason for being in this code is to bring the character back down to the planet after they jump or fall off a platform. I sat there and watched the character as I typed this, which was awhile.

I witnessed my character falling through one of the platforms I placed him on, as he fell, he would reappear from the top of the screen and continue the falling cycle to eternity fall. My question for this FALL code is, does it remove some of the collision? other wise, how was he falling through the platform?}}}


CALL MOVE(X,Y,0,FALL) {{{This one also resembles, the FALL+GRAVITY code. Except, when I removed this code, two things...

(1) Did not let me jump.
(2) I could walk over a whole and not fall. Whoever, if I pressed down while standing on the hole, I would descend down towards it. Basically a scuba diver, slowly descending.

I definitely have no idea why this code is here, it somehow adds to the FALL+GRAVITY code, but I don’t know how it compliments or helps that code.}}}


SPRITE 0,X,Y,TIMER/16 MOD 2 + 1 {{{Removing this code, the sprite disappears entirely. Just removing the 2+1 causes a syntax error. Changing “2+1” to “1+1” no effect, somehow character feels lighter as if higher jump, but not substantially.

Changing “2+1” to “0+1” causes syntax error, like wise for “2+-1” Have no idea what the “TIMER/16 MOD 2+1” does for this line of code. I do understand the sprite is the on the screen character and 0 stands for player 1 while X,Y stands for the cell position variables}}}


WAIT VBL
LOOP

SUB MOVE(X,Y,DX,DY)
XX=X+DX
YY=Y+DY
R=0
CALL CHECKCOLL(XX,YY,R)
IF R THEN
X=XX
Y=YY
FAIL=0
ELSE
FAIL=-1
END IF
END SUB

SUB CHECKCOLL(X,Y,R) {{{This one was interesting. I took heed of your instruction. I checked all four below. When I removed the first three, I was able to jump into a platform above me, and my characters head would go through that platforms first tile, but stopped by the tile above it.

When I removed all 4 codes, I kept falling though everything at lighting speeds. I get this was the FALL variable not being stopped by the collision of the platforms. I still don’t get the exact use of each individual code in this line, though I understand it is the collision for the platforms.

Now I understand what CHECKCELL means. I’m just thrown of by the x/8 “X divided by 8” and “X+7” plus 7 what cells? on which row, which column.. etc. I can’t read it at that level.. yet. }}}

'CHECK ALL CORNERS
R=-1
CALL CHECKCELL(X/8,Y/8,R)
CALL CHECKCELL((X+7)/8,Y/8,R)
CALL CHECKCELL(X/8,(Y+7)/8,R)
CALL CHECKCELL((X+7)/8,(Y+7)/8,R)
END SUB

SUB CHECKCELL(X,Y,R) {{{No matter what I removed here, I would get t he “Error Syntax” pop up, thus I have no idea what this code is about, under than an added layer for the checkcell? to what extent - I am not sure}}}

C=CELL.C(X,Y)
IF C<>0 THEN
R=0
END IF
END SUB

After you process all of this and are able to respond. I do have a request for something simple I would like to learn.

Can you add to this code jump with the “a button” of the xbox one s controller.

I just want to be able to mess around with jump button and I will slow add features into it myself.

I tried to adding jump button to this code, but I couldn’t do it, I kept getting syntax errors. I went to the online manual but it doesn’t provide examples where I person at my beginner would understand understand.

I will try this time around to mess with the code. It’s just every time I do, other than the stuff I broke down above, I get a “Syntax Error” so I can’t get anything done on my own as far as remixing the app because “Syntax Error” keeps stopping me.

This stuff is really tough to understand.


was8bit 2020-02-09 15:27 (Edited)

Hiya :D

Yep, i learn the same way too, tinkering around with code and see what the tinkering does ;)

... Rather than reverse explain from your results to the code, let me explain from MY thinking that lead me to the code...

So, FIRST, gravity... it is a tricky thing to understand.. when i first took physics class in school i could NOT understand acceleration.. i could understand the math for constant speed, so if i was driving 60miles/hour and the trip was 120 miles long, it would take me 2hours to get there...

But with gravity, the speed is ALWAYS CHANGING, so it requires different math to solve....

I eventually had a teacher who helped me understand it all... lets round off to simplify that acceleration if gravity is 10meters/second... here is a chart of the SPEED of a ball when you drop it (zero time is you holding it...) and total distance traveled or position
TIME ... SPEED ... POSITION
0 ... 0 ... 0
1 ... 10 .... 10
2 ... 20 ... 30
3 ... 30 ... 60
4 ... 40 ... 100
5 ... 50 .... 150
6 ...60 ....210
7 .... 70 ... 280
8 ... 80 ... 360
9 ... 90 .... 450
10 ... 100 ... 550


.... contemplate the chart for awhile.... :)


was8bit 2020-02-09 15:31

So, in my math in the game code, GRAVITY is similar to the gravitational constant, in the game, trial and error yielded a value of 0.1 (remember the 0 in front of .1 or NX gives you an error message)

FALL is the actual speed at any one time...

I use the main DO LOOP as the time cycle, so at each loop, it is another time cycle like in the chart...

If you don't change the down speed each cycle, but instead kept it constand, you would merely and slowly float down

REAL GRAVITY insists that the fall speed keeps getting bigger each time cycle...


was8bit 2020-02-09 15:35

When jumping, should you allow jumping at any time, you would allow the player to "jump" even if you are in mid-air... last time i checked, if you are falling, you cannot magically "jump" upwards while you are still in mid air..

So i only allow jumping when you are not falling, or when your current fall speed=0


was8bit 2020-02-09 15:41

Now, there is a secret thing in NX (technically not secret as it is mentioned in the help text but it is overlooked)...

When you do IF statements, or IF NOT statements, they are checking to see if a result is TRUE or NOT TRUE...

NX uses a simple math approach... it is considered TRUE if the math yields -1, and it is considered NOT TRUE if the math yields 0

I can prove it.. open up a new NX program and type in....

PRINT 1=1
PRINT 1=2

Now run it, and you will see...

-1
0

This because NX converts 1=1 into -1 (TRUE) and 1=2 into (0) FALSE


was8bit 2020-02-09 15:44

So when you see this in the code....

IF R THEN

It is shorthand for

IF R=-1

In original lowres you could actually use

IF R=TRUE

Which is more readable, but TRUE FALSE was dropped for NX


was8bit 2020-02-09 15:47

Here is another test for you..

Type this into a new blank game..

GAMEPAD 1
DO
PRINT UP(0);
WAIT VBL
LOOP

And run it... it prints 0 when you are not pushing UP arrow (false) and prints -1 when you ARE pushing UP arrow (true)


was8bit 2020-02-09 15:52

So

IF UP(0) THEN means NX renders UP(0) into a 0 or -1 which for NX means FALSE or TRUE,

IF NOT UP(0) reverses the logic..

This lets you read and understand the logic...

But you might get confused when i manually use this TRUE FALSE or -1 0 trick in the code...


was8bit 2020-02-09 15:56

Here is how Timo uses this logic trick...

SUB CHECKCOLL(X,Y,R)
'CHECK ALL CORNERS
R=-1
CALL CHECKCELL(X/8,Y/8,R)
CALL CHECKCELL((X+7)/8,Y/8,R)
CALL CHECKCELL(X/8,(Y+7)/8,R)
CALL CHECKCELL((X+7)/8,(Y+7)/8,R)
END SUB

SUB CHECKCELL(X,Y,R)
C=CELL.C(X,Y)
IF C<>0 THEN
R=0
END IF
END SUB

At first, R=-1 (or true)

Later, if a sprite movement causes contact with a cell object, it is set to R=0 (or false)


Then I convert R into FAIL...

R=0
CALL CHECKCOLL(XX,YY,R)
IF R THEN
X=XX
Y=YY
FAIL=0
ELSE
FAIL=-1
END IF


The IF R THEN means IF R=TRUE (but you cannot use TRUE FALSE anymore so it is -1 0 now) so it means IF R=-1 THEN


was8bit 2020-02-09 16:01

So the what does THIS mean?

IF FAIL THEN FALL=0

This is the TRUE FALSE trick again... in TRUE FALSE logic is is saying IF FAIL IS TRUE THEN, but in math it is saying IF FAIL=-1 THEN

And in my previous code, if movement would NOT touch a cell, movement is allowed, and the fall will should stopped... i guess it would have been more logical to have rewritten this code bettter, and i think i will ;)


was8bit 2020-02-09 16:05

I have removed FAIL as i see it was uneccessry and abit illogical... i have also changed UP into B button :)

Fixed2.nx | Open in app
2020-02-09 16:05

was8bit 2020-02-09 16:07

I added FALL=0 which makes more sense when you are reading the code...

FALL should really be FALLSPEED but that is more to type ;p


was8bit 2020-02-09 16:13

Oh, one thing to mention.... when things are being moved, the backgounds are actually bigger than the screen side, and movement is WRAPPED automatically around.. so

Screen size 20x16 cells
Background size 32x32 cells

Sprites do not use cell coordinates, they use pixel coordinates.. so each cell is 8x8 pixels.... so screen size in pixels is 160x128 pixels and background size is 256x256 pixels

Sprite coordinates vs + cell coordinates yields funny looking math in Timo's code as he checks all 4 corners of the sprite to any cells near the sprite.. do NOT change Timo's math there as it works as is ;)


was8bit 2020-02-09 16:14

.. and game controllers DO work differently,,yes...


was8bit 2020-02-09 16:20 (Edited)

Hope this is helpful :) post again if needed :)


was8bit 2020-02-09 16:28 (Edited)

... oh, and upon review of this code

IF BUTTON(0,1) AND FALL=0 THEN FALL=-2.5
IF DOWN(0) THEN CALL MOVE(X,Y,0,1)
IF LEFT(0) THEN CALL MOVE(X,Y,-1,0)
IF RIGHT(0) THEN CALL MOVE(X,Y,1,0)
FALL=FALL+GRAVITY
CALL MOVE(X,Y,0,FALL)

I removed from my copy of the game, the IF DOWN(0) THEN CALL MOVE(X,Y,0,1) as downward movement is covered with CALL MOVE(X,Y,0,FALL)


tactics_pvp 2020-02-09 23:51

@was8bit

Look at all that knowledge. I which we had that brain chip already out, so I can download all this wisdom into my brain!

I will take some time to absorb this knowledge. Thank you.

I will respond when I have some new insight.


was8bit 2020-02-10 05:04

Cool :) i will be here... if not immediately, always eventually :)


Log in to reply.