Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
Iggizorn
Shy Guy
Shy Guy
Posts: 8
Joined: Mon May 16, 2022 2:55 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Iggizorn » Mon May 16, 2022 1:10 pm

deice wrote:
Mon May 16, 2022 11:43 am
Iggizorn wrote:
Mon May 16, 2022 3:07 am
Hi Guys! I've been looking for a script with which I can create an "autorun (like super mario run)" level for a long time. Function should be: Mario runs automatically, he runs faster with sprinting, spin jump should work. I can deactivate buttons, but I just can't find the automatic run....-.- Can someone help me there?
if you're deactivating inputs using player.keys, you can force the player to hold certain keys in the same manner. while i haven't played super mario run, i can provide an example script that forces the player to run right but still lets them jump, which i assume is at least close to what you have in mind:

Code: Select all

function onInputUpdate()
	if(not Misc.isPaused()) then
		player.keys.left = false
		player.keys.down = false
		player.keys.altRun = false
		
		player.keys.run = true
		player.keys.right = true
	end
end

Thank you for the effort! just tried the script and it works as it should, except for one problem. Mario can only run or walk, he can't do both... if the problem can't be easily solved, I have to use it that way. Many thanks for the quick solution =)

deice
Volcano Lotus
Volcano Lotus
Posts: 544
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Mon May 16, 2022 1:14 pm

Iggizorn wrote:
Mon May 16, 2022 1:10 pm
Thank you for the effort! just tried the script and it works as it should, except for one problem. Mario can only run or walk, he can't do both... if the problem can't be easily solved, I have to use it that way. Many thanks for the quick solution =)
you can remove the lines that say

Code: Select all

player.keys.altRun = false
player.keys.run = true
which will return control over the run button to the player. do note though that the player won't instantly accelerate and will have to wait a bit to reach full speed just like normal smbx gameplay entails.

Iggizorn
Shy Guy
Shy Guy
Posts: 8
Joined: Mon May 16, 2022 2:55 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Iggizorn » Mon May 16, 2022 1:30 pm

deice wrote:
Mon May 16, 2022 1:14 pm
Iggizorn wrote:
Mon May 16, 2022 1:10 pm
Thank you for the effort! just tried the script and it works as it should, except for one problem. Mario can only run or walk, he can't do both... if the problem can't be easily solved, I have to use it that way. Many thanks for the quick solution =)
you can remove the lines that say

Code: Select all

player.keys.altRun = false
player.keys.run = true
which will return control over the run button to the player. do note though that the player won't instantly accelerate and will have to wait a bit to reach full speed just like normal smbx gameplay entails.

Perfect! 100% what I imagined. I could also switch this on and off via event with triggers?! I mean event "autorun on" and "autorun off" for individual sections?

deice
Volcano Lotus
Volcano Lotus
Posts: 544
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Mon May 16, 2022 2:11 pm

Iggizorn wrote:
Mon May 16, 2022 1:30 pm
Perfect! 100% what I imagined. I could also switch this on and off via event with triggers?! I mean event "autorun on" and "autorun off" for individual sections?
yes, you could. make two events inside your level with those names you mentioned above and use the following script:

Code: Select all

local pAutorun = false

function onEvent(e)
	if(e == "autorun on") then
		pAutorun = true
	elseif(e == "autorun off") then
		pAutorun = false
	end
end

function onInputUpdate()
	if(not Misc.isPaused() and pAutorun) then
		player.keys.left = false
		player.keys.down = false
		
		player.keys.right = true
	end
end

Iggizorn
Shy Guy
Shy Guy
Posts: 8
Joined: Mon May 16, 2022 2:55 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Iggizorn » Tue May 17, 2022 2:37 am

Thank you very much, I can start tinkering with this! I hope I don't spam the forum now, but I have another question^^

I use the Death Tracker with the respawn function, but now I have the problem when the player respawns, it is always small.

Could one make a query in the "rooms"as to how the level was started and take over the status from the player?



Solved it myself, just replaced "function onStart" with "function onTick" .... -.-

Iggizorn
Shy Guy
Shy Guy
Posts: 8
Joined: Mon May 16, 2022 2:55 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Iggizorn » Wed May 25, 2022 2:08 am

Hello again! I have another question! In SMW there is "MotorSkill", which means that if Mario jumps normally, he can switch to spin jumping in the air and vice versa. Is it possible to change the "jump" in the air in lua?

deice
Volcano Lotus
Volcano Lotus
Posts: 544
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Wed May 25, 2022 5:03 am

Iggizorn wrote:
Wed May 25, 2022 2:08 am
Hello again! I have another question! In SMW there is "MotorSkill", which means that if Mario jumps normally, he can switch to spin jumping in the air and vice versa. Is it possible to change the "jump" in the air in lua?
to change the player's spinjumping state, call

Code: Select all

player:mem(0x50, FIELD_BOOL, true)
to force them to be spinjumping (to force a normal jump, set it to false instead)

where you call this is up to you, either inside a keyboard event or through checking player.keys.
if you need to see an example, i believe mechdragon's mystery contest 21 submission has a similar mechanic.

Iggizorn
Shy Guy
Shy Guy
Posts: 8
Joined: Mon May 16, 2022 2:55 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Iggizorn » Wed May 25, 2022 3:01 pm

First of all, thanks again for the quick reply. The Mechadragon level is on youtube, but unfortunately without a download. I have now started 2 variants,

function onTick()
if ?player.isintheAir?
and keycode == KEY_SPINJUMP then
player:mem(0x50, FIELD_BOOL, true)
end
end


function onJump()
if keycode == KEY_SPINJUMP then
player:mem(0x50, FIELD_BOOL, true)
end
end

In the first version, the command for "in the air" is missing and the second version doesn't work. -.- ..... Is there a way to implement the first variant?

deice
Volcano Lotus
Volcano Lotus
Posts: 544
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Wed May 25, 2022 3:27 pm

Iggizorn wrote:
Wed May 25, 2022 3:01 pm
First of all, thanks again for the quick reply. The Mechadragon level is on youtube, but unfortunately without a download. I have now started 2 variants,
it's contained within the mystery contest 2021 level pack, you can download it from the archived thread.
Iggizorn wrote:
Wed May 25, 2022 3:01 pm
function onJump()
if keycode == KEY_SPINJUMP then
player:mem(0x50, FIELD_BOOL, true)
end
end

In the first version, the command for "in the air" is missing and the second version doesn't work. -.- ..... Is there a way to implement the first variant?
this doesn't work because there's no variable in that scope called "keycode", meaning it wil always evaluate to nil and never be equivalent to KEY_SPINJUMP. (also, onJump() is a deprecated event and shouldn't be used)

while i recommend you look up some more lua tutorials in order to familiarize yourself with the api a bit more (as you've grasped some basics but still seem to be struggling a bit with things like arguments and variable scope), the code you're looking for is actually rather simple:

Code: Select all

function onTick()
	if(player.keys.altJump) then
		player:mem(0x50, FIELD_BOOL, true)
	elseif(player.keys.jump) then
		player:mem(0x50, FIELD_BOOL, false)
	end
end

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Thu May 26, 2022 11:31 am

How would I make it so that the player getting damaged drops a number counter?
Let's say I have a "healthbar = 5" and if a player gets damaged, healthbar = healthbar - 1.

How would I go about doing this?

Added in 12 minutes 45 seconds:
Also, is there anyway to remove the score and coins from the HUD?

Added in 3 minutes 57 seconds:
And would it be possible for me to add another HUD beside the None, Itembox, and Hearts? (I'm looking at the hudoverride script right now)

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9720
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Thu May 26, 2022 12:21 pm

LunarCatUSA wrote:
Thu May 26, 2022 11:48 am
How would I make it so that the player getting damaged drops a number counter?
Let's say I have a "healthbar = 5" and if a player gets damaged, healthbar = healthbar - 1.

How would I go about doing this?

Added in 12 minutes 45 seconds:
Also, is there anyway to remove the score and coins from the HUD?

Added in 3 minutes 57 seconds:
And would it be possible for me to add another HUD beside the None, Itembox, and Hearts? (I'm looking at the hudoverride script right now)
function onPostPlayerHarm(harmedPlayer)
health = health - 1
end

local hudoverride = require("hudoverride")
hudoverride.visible.score = false
hudoverride.visible.coins = false

hudoverride.visible.itembox = false -- for disabling item box when wanting to show something else

function myCustomHud(cameraIndex, priority, isSplit)
Text.print("This is a custom hud element!")
end

Graphics.addHUDElement(myCustomHud)

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Thu May 26, 2022 9:58 pm

Alright thanks!

If it's not too much trouble, if the character is based off of the Mario character, is there anyway I can have it so that he doesn't power down upon normally running into an enemy or taking damage? (In this case, the healthbar is to give him 5 hit points before dying)

If not, I can probably modify the heart mechanic in hudoverride or something like that.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9720
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Fri May 27, 2022 10:44 am

hudoverride is only for drawing hud elements. It doesn't do any actual health management.
If you use the onPlayerHarm event instead of onPostPlayerHarm, you can conditionally cancel the player's harm event and do other things in its stead, like setting the 0x140 memory offset for some invulnerability frames, or changing a local health variable. https://docs.codehaus.moe/#/reference/l ... red-events

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Sat May 28, 2022 2:55 pm

Ah, neat. I'll probably turn the player into an "injury/damage" graphic. I guess my next question is how do I go about applying those I-frames? I know it's 0x140 and it needs a variable for how long the thing is active, is it just like "player.0x140 = 150" or something?

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9720
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat May 28, 2022 3:42 pm

LunarCatUSA wrote:
Sat May 28, 2022 2:55 pm
Ah, neat. I'll probably turn the player into an "injury/damage" graphic. I guess my next question is how do I go about applying those I-frames? I know it's 0x140 and it needs a variable for how long the thing is active, is it just like "player.0x140 = 150" or something?
Memory offsets are accessed via the mem methods:
https://docs.codehaus.moe/#/reference/p ... ce-methods

For getting, use the method with two arguments. For setting, use the method with three.
Example:
player:mem(0x140, FIELD_WORD, 150)

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Sat May 28, 2022 10:35 pm

Thank you so much,

One last thing for the time being, I noticed there isn't any documentation on there for Emitters/Particles on the codehaus.moe site. I ask because I wanted to add a particle effect to the player upon receiving damage.

What would it take to spawn some particles on the player given that I've made my custom particle png and its respective .ini file?

Added in 1 hour 7 minutes 10 seconds:
Oh and is there a way to freeze the player's movement with code? (Like the input drop for the controls that you see in the events) I want to make it so the player is stunned temporarily upon taking damage.

Added in 45 minutes 12 seconds:
Sorry, one more thing. During the time of the player being stunned, I'd like for them to transform into a specific sprite on the sprite sheet and then transform back when the stun duration is finished. These are at sprite position x=0, y=0 and x=0, y=5 with right face and left face respectively.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9720
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sun May 29, 2022 4:55 am

https://wohlsoft.ru/pgewiki/Particles.lua
Hasn't been ported yet but is still applicable. Particles is automatically loaded. Make an emitter with a rate of 0 and move it and call :Emit(numberOfParticles) manually when the player g ets hit.

To lock all controls you can put this into onTick:
for k,v in ipairs(player.keys) do
player.keys[k] = false
end
If you want to restore it you can store the onTick value and restore it in onTickEnd with another for loop. That'll stop players from throwing fireballs or jumping once the invulnerability period ends.
A positional lock is best achieved by forcing player position to their last position, and speed to 0 and -Defines.player_grav respectively.

To change the player's frame, set player.frame to a number based on this reference: https://i.imgur.com/1dnW3g3.png
The numbers are based on player direction already, so numbers going left/up are for left direction, and vice-versa.

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Mon May 30, 2022 2:26 am

Well, I got the first two taken care of so thanks for that, but the frame change isn't working. One's an injury sprite for the right direction (frame 49) and the other's an injury sprite for the left direction (frame 48). I wonder if it's just easier to transform the player to a certain effect png or something like that.

Is it possible to add a new player animation? If so, I may just do that.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9720
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Mon May 30, 2022 2:27 am

just lay out your spritesheet so that both are frame 48, or do an if-else based on player.direction

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Mon May 30, 2022 10:05 am

Okay, and to clarify;
It should be under "OnTick()" and I use the function "player.frame = 48"?


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari