Page 1 of 32

Need help with lua? - LunaLua General Help

Posted: Sat Mar 30, 2019 7:41 am
by Emral
So you're trying to get into LunaLua but
1) Don't know where to get started?
2) Have a question?

LOOK NO FURTHER THAN THIS THREAD!
Version 2.0

How to get started
LunaLua is SMBX2's programming language, so if you don't have SMBX2 in any capacity, you can download the most recent version from:
http://codehaus.wohlsoft.ru/downloads.html

If you're new to LunaLua, I suggest checking out my lua tutorials. I also recommend taking a look at the generic lua tutorial for a more technical focus on the language.

If you're looking for documentation of any kind, you're going to find it here.
The documentation above is not perfectly up-to-date at the current point in time, however, so for things not documented please take a look at the Handbook, or into the script files of the libraries you are trying to access.
We're working on getting a proper documentation set up in the coming months.

Do you have a question?

If you have a question about why your code isn't working or how to optimize something you've been working on, just reply in this thread. If you feel like your issue is major and are afraid it'll drag on over several pages or get lost, don't hesitate to make a topic about it in the forum, though.
There are many people who are experienced in lunalua (myself included) who're glad to help you out.

Just make sure that you provide enough information on your issue so we can help you. Taking a screenshot of the error message (if there is one) or posting your code using the "code" bbcode or using http://hastebin.com/ are things you should always do. If your code is long and you are getting an error, make sure to specify which lines the error is tied to (and post to hastebin rather than using the bbcode, because hastebin has line numbers). (To find which line an error appears in, look at the number at the end of the directory in the first line of the error message, or just post a picture of the error along with the code).

With that out of the way, if you have any questions regarding how to do something in lua or regarding your lua code, this is the place to go.

Re: Need help with lua? - LunaLua General Help

Posted: Mon Apr 01, 2019 9:41 pm
by boingboingsplat
I made a slightly modified version of the Puntin' Chuck NPC, so that it could kick my own variant of the Football NPC.

However, I noticed something odd: It kicks half as often as the one I based it off of. Most of the code is the same, which made me curious, so I had it print off it's internal timer. It makes the issue apparent, it updates at half the rate, though I have no idea why.

Here's a clip of that happening: https://streamable.com/cbf23

Here's the full script for the sprite. Would love some guidance, is there something more I should be doing when making tweaked versions of NPCs that I neglected to do?

https://hastebin.com/uguwidoxij.rb

Re: Need help with lua? - LunaLua General Help

Posted: Tue Apr 02, 2019 12:19 am
by Quantumenace
It looks like the normal ones are too fast. Notice how their timer is always an even number? Maybe their behavior is being run twice for some reason.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Apr 02, 2019 1:40 am
by boingboingsplat
Ah, nice call. I had created my own version of the Punting Chuck, to fix a bug, and just loaded it in my level over the same NPC ID. When I remove it the issue stops. My guess is, that maybe doing this causes the "onTickEndNPC" event to be registered twice? Causing its behavior to be executed twice per tick.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Apr 02, 2019 5:41 am
by Hoeloe
boingboingsplat wrote:
Tue Apr 02, 2019 1:40 am
Ah, nice call. I had created my own version of the Punting Chuck, to fix a bug, and just loaded it in my level over the same NPC ID. When I remove it the issue stops. My guess is, that maybe doing this causes the "onTickEndNPC" event to be registered twice? Causing its behavior to be executed twice per tick.
If you use npc-#.lua files over an existing ID, it won't replace that NPCs behaviour, but run as well as the default behaviour. These files are for augmenting existing NPCs. If you want to create new ones from scratch, use IDs in the range 751-1000.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Apr 03, 2019 7:14 pm
by BigFrog
I'm trying to make a special double jump but I'm having problems trying to define if the player is on the ground or not, and such is causing the double jump to go off instantly. Is there a way to define when the player is on the ground?

Re: Need help with lua? - LunaLua General Help

Posted: Thu Apr 04, 2019 2:26 pm
by Hoeloe
BigFrog wrote:
Wed Apr 03, 2019 7:14 pm
I'm trying to make a special double jump but I'm having problems trying to define if the player is on the ground or not, and such is causing the double jump to go off instantly. Is there a way to define when the player is on the ground?

Code: Select all

player:isOnGround()

Re: Need help with lua? - LunaLua General Help

Posted: Fri Apr 05, 2019 4:41 pm
by Graham
Silly question. Is there a way to access SMBX event fields from within LunaLua, such as the name of the fourth event in my level?

Re: Need help with lua? - LunaLua General Help

Posted: Fri Apr 05, 2019 6:02 pm
by Dog In Da Grass
Image

I've been trying to get this Blue Sproingy Thing to telliport to particular areas
I have it's X and Y co-ordinates listed by mario (the bottom two numbers)

but I dont know how to get it to move with Lua.

I tried using the npc.x = number and npc.y = number but it didnt change the location of the blue sproingy, is there a particular funtion to teliport npcs around? how could I achieve this?


Image


Thanks, Woof!

Re: Need help with lua? - LunaLua General Help

Posted: Fri Apr 05, 2019 8:42 pm
by Quantumenace
Graham wrote:
Fri Apr 05, 2019 4:41 pm
Silly question. Is there a way to access SMBX event fields from within LunaLua, such as the name of the fourth event in my level?
Access it in what way? If you want to detect the event you use

function onEvent(eventName)
if eventName == "whateverYourEventNameIs" then
...
end
end

If you use onEventDirect(Event eventObj, string eventName) you can get the name field from the event object, but it doesn't look like there's an easy way to get the event from the event array itself.
Dog In Da Grass wrote: Image

I've been trying to get this Blue Sproingy Thing to telliport to particular areas
I have it's X and Y co-ordinates listed by mario (the bottom two numbers)

but I dont know how to get it to move with Lua.

I tried using the npc.x = number and npc.y = number but it didnt change the location of the blue sproingy, is there a particular funtion to teliport npcs around? how could I achieve this?

Image
It's case sensitive, don't capitalize X and Y and see if that works.

Re: Need help with lua? - LunaLua General Help

Posted: Fri Apr 05, 2019 10:55 pm
by Dog In Da Grass
Quantumenace wrote:
Fri Apr 05, 2019 8:42 pm
It's case sensitive, don't capitalize X and Y and see if that works.
lol, simple fix but it worked! Thank you!

Re: Need help with lua? - LunaLua General Help

Posted: Sat Apr 06, 2019 1:35 am
by Graham
Quantumenace wrote:
Fri Apr 05, 2019 8:42 pm
Access it in what way? If you want to detect the event you use

function onEvent(eventName)
if eventName == "whateverYourEventNameIs" then
...
end
end

If you use onEventDirect(Event eventObj, string eventName) you can get the name field from the event object, but it doesn't look like there's an easy way to get the event from the event array itself.
Unfortunately, access in the sense of get and set the raw fields from the event array. I'd been doing a bit of experimenting to try to give a more useful bug report, and I'd noticed that the bug where game end doesn't, well, end the game, only seems to occur when game end is called from an event. Calling game end directly from Lua and killing Bowser when legacy boss is set both seem to end the game properly, so I wanted to test if that has to do with the associated event field not being loaded with its proper value somehow. But it doesn't seem like there's a nice way to do that as is - ah well.

Thanks a lot for the help!

Re: Need help with lua? - LunaLua General Help

Posted: Sun Apr 07, 2019 5:41 am
by BabyPinkSnail
I wanna make it so the player is big at the start of the level but only at the very start so after you've gotten a checkpoint you'll spawn small, however I'm assuming level start means anytime the level is started the first time, or after a checkpoint, please tell me how to do it I've tried a few options and feel like an event trigger isn't probably the most optimized way to do it.

Re: Need help with lua? - LunaLua General Help

Posted: Sun Apr 07, 2019 7:22 am
by Hoeloe
You can actually check if you're spawning from a checkpoint pretty easily:

Code: Select all

function onStart()
	if Checkpoint.get() == nil then
		player.powerup = 2
	end
end
Should work.

Re: Need help with lua? - LunaLua General Help

Posted: Mon Apr 08, 2019 4:19 pm
by Murphmario
Hey, does anyone know why I'm getting this error? It's not an issue with Enjl's Parachute NPCs, so something must be wrong with my file.
Image

Re: Need help with lua? - LunaLua General Help

Posted: Mon Apr 08, 2019 5:08 pm
by Hoeloe
Murphmario wrote:
Mon Apr 08, 2019 4:19 pm
Hey, does anyone know why I'm getting this error? It's not an issue with Enjl's Parachute NPCs, so something must be wrong with my file.
If I'm right, your code will have a line with something like:

Code: Select all

npcmanager.registerDefines(NPC_ID, NPC_HITTABLE)
or something similar.

When using registerDefines, you have to supply a list of NPC styles, even if that list has only one item, like so:

Code: Select all

npcmanager.registerDefines(NPC_ID, {NPC_HITTABLE})

Re: Need help with lua? - LunaLua General Help

Posted: Thu Apr 18, 2019 9:13 am
by cato
Straight up copies from Enjl's video. Still doesn't work. I got some problems.The thing is, the counter doesn't even add 1.

Code: Select all

local counter = 0

SaveData[Level.filename()] = SaveData[Level.filename()] or {}
local levelSD = SaveData[Level.filename()]

function onStart()
    player.character=CHARACTER_TOAD
    player.powerup=PLAYER_BIG
    if levelSD.dragonCoinsCollected then
        for k,v in ipairs(NPC.get(274)) do
           v:kill(9)

        end    
    end
end

function onDraw()
    Text.print(counter,100 ,100)
    if SaveData[Level.filename()].dragonCoinsCollected then
        Text.print("Good job player!")
    end
end

function onNPCkill(eventObj, killedNPC, killReason)
    if killedNPC.id ~= 274 then return end

    if killReason ~= HARM_TYPE_OFFSCREEN then return end

    if Colliders.collide(player, killedNPC) then
        counter = counter+1
        if counter == 5 then
        levelSD.dragonCoinsCollected = true 
        end
    end    
end

Re: Need help with lua? - LunaLua General Help

Posted: Thu Apr 18, 2019 10:24 am
by Emral
onNPCKill. Case-sensitivity.

Re: Need help with lua? - LunaLua General Help

Posted: Sun Apr 21, 2019 5:47 pm
by Dog In Da Grass
is there a way to modify any of the characters? is there a way to have and change a personal copy of the lua file for your level folder?
More specifically I've been trying to edit Klonoa so you can interupt his flutter and also move while shooting the ring

I've been looking at the code and I think I found exactly what I want, I just don't know how to access it from my own lua file
Image

I'd like to change flaptimer but, yeah, don't quite know how to go about this, do any of you have any ideas? thanks~




EDIT
Found help elsewhere, I needed to have a "Characters" folder in my level file

Added in 1 hour 12 minutes 27 seconds:
Though a previous question I have still stands,
how do I switch to a custom character?

Re: Need help with lua? - LunaLua General Help

Posted: Sun Apr 21, 2019 7:46 pm
by Emral
Dog In Da Grass wrote:
Sun Apr 21, 2019 7:03 pm
how do I switch to a custom character?
You can use the character constants: player:transform(CHARACTER_KLONOA)