Page 1 of 1

Kirby Playable Help

Posted: Mon May 11, 2020 10:55 am
by Throw Kirby
I'm trying to make it so that my Kirby playable spits out a star when not full Do you think this code will work?

Code: Select all

function Inhale
    if keycode == KEY_RUN and SldInit = false
        if Full == true
            starID = NPC.spawn(108, player.x, player.y, player.section)
            Full = false
            starID.speedX = 15 * direction;
            starID(0xF0, FIELD_DFLOAT, 4)
        else
	(Inhale code, IDK)
    end
end

Re: Kirby Playable Help

Posted: Wed May 20, 2020 9:44 am
by MrDoubleA
This is not valid lua code. I’d personally recommend watching Enjl’s lunalua tutorials before taking on the task of making a custom playable.

Re: Kirby Playable Help

Posted: Wed May 20, 2020 10:08 am
by Cedur
The first thing I see (without having any scripting knowledge myself) is the missing parentheses after "function Inhale"

Re: Kirby Playable Help

Posted: Wed May 20, 2020 11:22 am
by Emral
My lua tutorials are the second place to look. They assume general knowledge of syntax, to some extent. Using them as a smbx-specific resource can be very valid, but I recommend using the official lua tutorial in tandem with them. Start here and use the navigational arrows. Feel free to skip pages you already are confident on: https://www.lua.org/pil/2.html

From your code above, here are some things I noticed:
- As Cedur said, functions are denoted with parentheses. The parentheses are necessary, because parameters are defined in them. As in, custom variables that are passed to the function.
- In the line below, you are missing a "then". Some languages use {} to denote scope, lua uses "do", "then" (based on which one makes more sense when reading the code like if it was english) and "end" (for the ending)
- In the same line, you use == once and = another time. Pay attention to those two. They're easy to mix up, but what might help is that = is assignment and == is comparison, so because = is short you can read it as "set" and == is long so you can think of something like "are you sure, that...?"
Think of this line saying "if you are sure that keycode is KEY_RUN and SldInit is false then" and then the mistake should make more sense
- in the SAME line you use 2 uninitialized variables: SldInit and keycode. keycode is a parameter passed to the predefined function onKeyDown. It doesn't exist outside of onKeyDown. SldInit doesn't exist by default. You can define it outside of functions (above the code you posted) by writing "local SldInit = false"
- Repeating error of uninitialized variable and missing then in the line below
- If you don't need starID outside of the if statement, make sure to declare the variable as local. Local means that once the next "end" is reached, the variable stops existing. It's useful to avoid the variables leaking into other bits of code.
- StarID is a misnomer. It's an NPC, not an ID. "star" or "starNPC" would be more fitting names.
- direction is undefined, a few lines lower. Do you mean player.direction or starID.direction?
- The line starID(0xF0, FIELD_DFLOAT, 4) is wrong in two ways. Firstly, starID is not a function so you cannot call it. Secondly, we have a field for it.
-- You are likely trying to use starID:mem(...). That would be valid.
-- However, 0xF0 is already exposed as a field. Just put starID.ai1 = 4.
That's about it. I hope they make sense and you have an idea on how to resolve them. And yeah, as MrDoubleA said, making a playable is complicated. It's not too bad in terms of technical knowledge, but it's just a large quantity of code, since you need to do a lot of state management for the player. Good luck!