(shouting)

Lua help thread

If you lost something come look for it here
User avatar
Willhart
Stalker, Doxxer, and Sexual Harasser
Banned
Posts: 2434
Joined: 13 years ago
Location: Finland

Re: Lua help thread

Post by Willhart »

Can this be used to make a Goomba shoot bullets like a Snifit?
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

I'm having some trouble getting filters to work. The idea is that the player goes through section 3 the first time as Mario, and then upon triggering the event "fishmen" returns to section 3 to play as Luigi, which then opens up an alternate path, which itself is triggered by the event "Bowserman." However, when I enter section 3 as Toad the first time, I remain Toad.

Another issue I'm having is whenever I load or start the level, I get the error [Error: {filepath}lunadll.lua:10:<name> or '...' expected near '"fishmen"'], and the Lua event triggered by "fishmen" never triggers.. But when I don't include the quotation marks around "fishmen," I get the debug text box immediately upon loading the level.

Code: Select all

local mariohunting=0
function onLoadSection2()
      if (mariohunting==0) then
        player:mem(0xF0, FIELD_WORD, 1)
    elseif (mariohunting==1) then
        player:mem(0xF0, FIELD_WORD, 2)
		triggerEvent("Bowserman")
    end
end
function onEvent("fishmen")
mariohunting=1
Text.windowDebug("Test")
end
User avatar
Rednaxela
Maker of Shenanigans
Posts: 897
Joined: 10 years ago
Pronouns: they/them
https://rednaxela.talkhaus.com

Re: Lua help thread

Post by Rednaxela »

acrowdofpeople wrote:I'm having some trouble getting filters to work. The idea is that the player goes through section 3 the first time as Mario, and then upon triggering the event "fishmen" returns to section 3 to play as Luigi, which then opens up an alternate path, which itself is triggered by the event "Bowserman." However, when I enter section 3 as Toad the first time, I remain Toad.

Another issue I'm having is whenever I load or start the level, I get the error [Error: {filepath}lunadll.lua:10:<name> or '...' expected near '"fishmen"'], and the Lua event triggered by "fishmen" never triggers.. But when I don't include the quotation marks around "fishmen," I get the debug text box immediately upon loading the level.
Firstly, you misunderstand how Lua functions work and how onEvent works.
1) Please learn how functions work in Lua from something like this or this
2) but in summary, instead of:

Code: Select all

function onEvent("fishmen")
mariohunting=1
Text.windowDebug("Test")
end
you want

Code: Select all

function onEvent(eventName)
    if eventName == "fishmen" then
        mariohunting=1
        Text.windowDebug("Test")
    end
end
Secondly, when you say "However, when I enter section 3 as Toad the first time, I remain Toad.", is this after having the error you later describe? Because, after having that error, no more Lua code will run, explaining why your player character setting wouldn't happen.
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

Oh! Okay, I see now! Somehow I misunderstood the way the function parameters worked there. I was thinking that "onEvent" worked similarly to "triggerEvent." Thank you!

You're right, the issue wit changing characters disappeared once I did the fix you recommended. I thought it might be related to the issue I had with some code from the tutorial here, where anytime I combine "onLoad" with a player pointer, I get an "invalid player pointer" error and the game crashes.
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

acrowdofpeople wrote:Oh! Okay, I see now! Somehow I misunderstood the way the function parameters worked there. I was thinking that "onEvent" worked similarly to "triggerEvent." Thank you!

You're right, the issue wit changing characters disappeared once I did the fix you recommended. I thought it might be related to the issue I had with some code from the tutorial here, where anytime I combine "onLoad" with a player pointer, I get an "invalid player pointer" error and the game crashes.
I suggest making sure you understand the difference between a function call and a function definition. When you use onEvent, you're defining a function, but when you use triggerEvent, you're calling an existing one. It's important to understand the difference.

Also, that error occurs because the onLoad function is called when the level is loaded, even when the editor is loading it. When the editor loads a level, it doesn't create a player, so if you try to use the player there, the game will error and crash. However, you can do a check to see if the player is nil, and use the isValid field in the player structure, to make sure the player can be used. If you run that check at the start of onLoad:

Code: Select all

if(player == nil or not player.isValid) then return end
then the function won't execute any further if the player doesn't exist, and you can refer to the player as much as you like after that.
Image
Image
Image
Image
Image
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

Thanks! That'll help a lot for this level idea I had!

So whenever I start a line with "function," I'm defining a function, so the arguments in the parenthesis are generic items that are used within the function I'm writing, dependent upon what the function is doing - hence the if structure for "onEvent" making sure it's the precise event I wanted. But when I call a function, the arguments are the specific arguments I want to be put into the function - so if I were to make my own function, "function1(arg1)", then I would leave arg1 generic in the "function funciton1(arg1)" line, but when I call it from another function, I would call it with function1("string") , and use an if structure within the function to check the string to make sure it was the correct one. But if I just wanted function1 to do a thing with a number it had been handed, I'd call it function1(10,000) and then use arg1 within the function to stand in for the number.

Is that correct?
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

acrowdofpeople wrote: Is that correct?
More or less. Essentially, when you define a function, you also define a list of arguments, which are essentially local variables that exist within the scope of the function. When calling a function, you specify the initial values of those arguments. It's pretty much valid to consider a function as a mini program. Take this:

Code: Select all

function myFunc(arg1, arg2)
  local r = arg1+arg2;
  return r;
end
What I'm trying to get at is "arg1" and "arg2" aren't fundamentally different from "r" - they're all variables declared in the scope of the function. All you do when you call a function is assign values to those variables. So, if you call the function with some values:

Code: Select all

function onLoop()
  local n = myFunc(4,5);
  windowDebug(tostring(n));
end
That's basically equivalent to this:

Code: Select all

function onLoop()
  local arg1 = 4;
  local arg2 = 5;
  local r = arg1+arg2;
  local n = r;
  windowDebug(tostring(n));
end
Note how I've simply pasted the function definition into the loop, but am first assigning the arg1 and arg2 variables. That's more or less what a function call does (it's not exactly the same, but it should help show roughly how it works). Essentially, your arguments are just local variables, and you assign those variables when you call a function with its arguments.
Image
Image
Image
Image
Image
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

I think I get it now. Thanks!
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

So I'm using the math.random function to generate random NPCs from a Lava Lotus, but I've noticed that I always get the same results in the same order. Is there something I can do to make the results actually random, or is this just something I have to put up with?
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

acrowdofpeople wrote:So I'm using the math.random function to generate random NPCs from a Lava Lotus, but I've noticed that I always get the same results in the same order. Is there something I can do to make the results actually random, or is this just something I have to put up with?
Lua's random number generator is notoriously terrible. I'll probably make a library that does a better job of generating random numbers at some point. If I remember correctly, if you generate a few numbers in a row it improves, so maybe give that a try.
Image
Image
Image
Image
Image
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

I see. I'll try that, then. Thank you.
User avatar
SAJewers
ASMBXT Level Wrangler/A2XT Project Coordinator /AAT Level Designer
Posts: 4199
Joined: 11 years ago
Location: Nova Scotia

Re: Lua help thread

Post by SAJewers »

http://lua-users.org/wiki/MathLibraryTutorial
But beware! The first random number you get is not really 'randomized' (at least in Windows 2K and OS X). To get better pseudo-random number just pop some random number before using them for real:

-- Initialize the pseudo random number generator
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- done. :-)
maybe try that?
ImageImageImageImageImage | Image | Image
Sharenite | RetroAchievements | NameMC | IGDB
acrowdofpeople
Posts: 54
Joined: 8 years ago

Re: Lua help thread

Post by acrowdofpeople »

Oh, nice! Now the lotus flowers are giving me stuff in a totally random order! Thanks!
User avatar
7NameSam
hey
Posts: 248
Joined: 9 years ago
Pronouns: they/them

Re: Lua help thread

Post by 7NameSam »

how do I spawn an NPC with velocity?
(Like, spawning a sniffit bullet with upwards velocity)
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

7NameSam wrote:how do I spawn an NPC with velocity?
(Like, spawning a sniffit bullet with upwards velocity)
You can spawn the NPC, and save it to a variable, then set the speed. Like this:

Code: Select all

local bullet = NPC.spawn(...);
bullet.speedY = 4;
Basically, you can sort of consider everything happening in one run of any function as happening at the same time. It all occurs on the same frame (but reads the lines of code from top to bottom), so spawning an NPC and then setting the speed will have the same effect as spawning it with a specific speed, because both the spawning of the NPC and setting the speed happen on the same frame.

Keep in mind that unless you use pnpc at this point, you can only use that variable safely on the frame you spawn the NPC, so if you need to access specific NPCs later, use pnpc.
Image
Image
Image
Image
Image
User avatar
CloudyCloud
Posts: 234
Joined: 11 years ago

Re: Lua help thread

Post by CloudyCloud »

1. Is there a way to make the player character float when they are holding item in the ocean? I am curious if such code is written already.

you know, SMW physics.(In SMW, player can float and thus swim faster when they hold item in the water.)

2. Possible to make only some item able to help player float on the water?
(For example, ice block. Well, you get the ice flower and then you freeze the monsters, then the frozen enemy-block will float up. And thus I am thinking that one should be able to float if they hold the frozen block also.)(I kinda want to make a swim ring.)

1st question is more to make a SMW water level.
2nd question is more for other reason.

Or actually 1st question is just extension of 2nd?
Post Reply