Page 1 of 1

Ziplines.lua - Zip across the stage with ease! V1.1

Posted: Sun Jan 21, 2018 5:08 pm
by Benial
Am I one of them there lunar lua cool kids now?
Image
Ziplines! They zip, but without lines! Simply jump into them and until you hit a wall or jump off, you can glide through the air and climb up trees.
Functions
  • Latch on by jumping into zipline. Get off with a jump or Spinjump
  • Breaks when the player gets off or if you hit a wall.
  • Collide with a Zipline Reflector to change your direction
  • Turn on "Don't Move" to make it only move Vertically
  • Turn on "Friendly" to make it only move Horizontally
  • Use the lunadll.lua file to customise how each setting affects speed (See spoiler below)
lunadll.lua formatting guide: show

Put "Ziplines.speeds = {}" in the code.
Enter values as follows into brackets: X with "Don't Move", Y with "Don't Move", X with "Friendly", Y with "Friendly", Default X, Default Y.

Please note it has not been created with nor tested in Multiplayer. Behaviour may become finicky if you have to drag a friend with you.
Changelog: show
1.1: Code cleaned up and optimised better.
Download here, with example level if you need it.

Re: Ziplines.lua - Zip across the stage with ease!

Posted: Sun Jan 21, 2018 6:14 pm
by Eri7
This looks fantastic , i will sure use it for my project.

Re: Ziplines.lua - Zip across the stage with ease!

Posted: Sun Jan 21, 2018 6:39 pm
by OlieGamerTV
Oh my, that looks so cool! i can see many episodes that could use this.

Re: Ziplines.lua - Zip across the stage with ease!

Posted: Sun Jan 21, 2018 6:49 pm
by erkyp3rky
Benial wrote:Am I one of them there lunar lua cool kids now?
Yes.

Re: Ziplines.lua - Zip across the stage with ease!

Posted: Thu Jan 25, 2018 3:25 pm
by The0x539
Few things about the code, in decreasing order of importance:
Local Variables: show

Code: Select all

v.touching = false
v.colliding = false
v.clipping = false
v.dismount = false
Use local variables for things like this. Declare (and initialize, if appropriate, like here) variables with those names, touching, colliding, etc, and just read them from that. Don't put them in v when you're neither storing them for future ticks (not that that would work, there's another way to do that but it's not important now) or using them as vanilla fields (like working with v.x, which you do).
pairs and ipairs: show

Code: Select all

for k,v in pairs (NPC.get(214,player.section)) do
There are two main functions for iterating over tables. pairs, which you use, is suited for tables like

Code: Select all

t = {[3] = 5, foo = true, ["some other key idk"] = "fizz"}
t[3] is 5, t["foo"] is true, and so on.
Here, pairs is guaranteed to get every entry in the table. However, some tables look more like

Code: Select all

t = {1,false,34,"abc"}
Note how no keys are specified, so the keys are sequential. t[1] is 1, t[2] is false, t[3] is "abc". NPC.get, and all the other "get" functions SMBX has, return tables that look like this. Here, ipairs is more useful. It's guaranteed to go through all of the entries in order, and more importantly it's faster.
Event Naming: show

Code: Select all

registerEvent(Ziplines,"onTick","eachFrame")
Unless you have a reason not to, just name the function onTick. As a bonus, if the function name is the same as the event name, you don't need to use the third argument.
Boolean Expressions: show

Code: Select all

someExpr == true
anotherExpr == false
This is very rarely necessary. You can just put someExpr in whatever, and unless it's false or nil, it will be considered true. Similarly, it's generally better to express (not anotherExpr).
Other Small Nitpicks: show
  • It's Lua or lua, never LUA.
  • I wouldn't recommend capitalizing a Lua API name unless it's meant to be a class in an object-oriented kinda thing, which this isn't.
  • The NPC class has friendly and dontMove fields, which you can use instead of memory offsets 0x46 and 0x48.
  • What's the purpose of colliding = true in onInitAPI?
  • Avoid playSFX, always use Audio.PlaySFX
  • v.direction = -v.direction

Re: Ziplines.lua - Zip across the stage with ease!

Posted: Thu Jan 25, 2018 3:58 pm
by Benial
The0x539 wrote:Few things about the code, in decreasing order of importance:
I expected I'd have to clean this up at some point. I'll get on with that and reupload a cleaner version.
(Wait, I used normal playSFX?)