Page 1 of 1

Cannot get lua to work

Posted: Sat Dec 30, 2017 9:59 pm
by asianqueen
I got t his codes from youtube and try to apply. However it's not working. I tried other codes as well and nothing happens when I test the game. I'm using notepad++.

Code: Select all

local playerDirection = 0;

function onloop()
	playerDirection = player:mem(0x106, FIELD_WORD)
end

function onKeyDown(keycode)
	if keycode == KEY_JUMP then
		a = NPC.spawn(1, player.x, player.y - 128,player.section)
		a.speedY = -2;
		a.speedX = 5 * playerDirection;
	end
end

Re: Cannot get lua to work

Posted: Sat Dec 30, 2017 10:04 pm
by PixelPest
This is actually really close!

The only issue I can see that would cause it to not work is that "onloop" should have a capital "L" instead of a lowercase "l".

Code: Select all

local playerDirection = 0;

function onloop()
   playerDirection = player:mem(0x106, FIELD_WORD)
end

function onKeyDown(keycode)
   if keycode == KEY_JUMP then
      a = NPC.spawn(1, player.x, player.y - 128,player.section)
      a.speedY = -2;
      a.speedX = 5 * playerDirection;
   end
end
Just be wary of a few things though. (1) onLoop is deprecated and onTick should be used instead. (2) onKeyDown isn't needed here and should be onInputUpdate instead in which you can check player.jumpKeyPressing. (3) Make sure you declare a in a local scape, like you've done playerDirection.

Code: Select all

local playerDirection = 0;

function onTick()
   playerDirection = player:mem(0x106, FIELD_WORD)
end

function onInputUpdate()
   if player.jumpKeyPressing then
      local a = NPC.spawn(1, player.x, player.y - 128,player.section)
      a.speedY = -2;
      a.speedX = 5 * playerDirection;
   end
end

Re: Cannot get lua to work

Posted: Sat Dec 30, 2017 10:10 pm
by asianqueen
so the syntax and everything else is case sensitive? If it is; this is not a easy language.

Re: Cannot get lua to work

Posted: Sat Dec 30, 2017 10:11 pm
by PixelPest
asianqueen wrote:so the syntax and everything else is case sensitive? If it is; this is not a easy language.
Most languages are case sensitive and this is in fact probably one of the easiest besides drag-and-drop interfaces. Don't worry. It's not bad when you get used to it. It uses a specific pattern; when you have multiple words together (without a space or period) in most cases it will start with a lowercase letter and then all other words will start with capitals: onTick, player.jumpKeyPressing, etc.

Re: Cannot get lua to work

Posted: Sat Dec 30, 2017 10:17 pm
by asianqueen
yeah, I copy and paste your script then save it. Still not working. I'm not sure what I did wrong. Here's my steps to launch the script.

Open up smbx2.exe > choose editor > Load my level > script > Lunalua Script -> level global. It opens up with notepad++. Pasted the code in there and save it. Run the test.

Re: Cannot get lua to work

Posted: Sat Dec 30, 2017 10:18 pm
by Emral
asianqueen wrote:yeah, I copy and paste your script then save it. Still not working. I'm not sure what I did wrong. Here's my steps to launch the script.

Open up smbx2.exe > choose editor > Load my level > script > Lunalua Script -> level global. It opens up with notepad++. Pasted the code in there and save it. Run the test.
The editor saves it under the wrong name in Beta 3. You need to rename it to lunadll.lua (inside level folder) or lunaworld.lua (inside episode folder). The name changes to luna.lua in Beta 4.

Re: Cannot get lua to work

Posted: Sat Dec 30, 2017 10:24 pm
by asianqueen
Enjl wrote:
asianqueen wrote:yeah, I copy and paste your script then save it. Still not working. I'm not sure what I did wrong. Here's my steps to launch the script.

Open up smbx2.exe > choose editor > Load my level > script > Lunalua Script -> level global. It opens up with notepad++. Pasted the code in there and save it. Run the test.
The editor saves it under the wrong name in Beta 3. You need to rename it to lunadll.lua (inside level folder) or lunaworld.lua (inside episode folder). The name changes to luna.lua in Beta 4.
Perfect. It works now. Wow, didn't know it as simple as renaming it. Time to create some useful script. I'm new to it, but hopefully I can come up with something to the community. Thanks a bunch.