Lua help thread
- Willhart
- Stalker, Doxxer, and Sexual Harasser
- Banned
- Posts: 2434
- Joined: 13 years ago
- Location: Finland
Re: Lua help thread
Can this be used to make a Goomba shoot bullets like a Snifit?
-
- Posts: 54
- Joined: 9 years ago
Re: Lua help thread
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.
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
- Rednaxela
- Maker of Shenanigans
- Posts: 897
- Joined: 10 years ago
- Pronouns: they/them
- https://rednaxela.talkhaus.com
Re: Lua help thread
Firstly, you misunderstand how Lua functions work and how onEvent works.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.
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
Code: Select all
function onEvent(eventName)
if eventName == "fishmen" then
mariohunting=1
Text.windowDebug("Test")
end
end
-
- Posts: 54
- Joined: 9 years ago
Re: Lua help thread
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.
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.
Re: Lua help thread
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.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.
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
-
- Posts: 54
- Joined: 9 years ago
Re: Lua help thread
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?
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?
Re: Lua help thread
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:acrowdofpeople wrote: Is that correct?
Code: Select all
function myFunc(arg1, arg2)
local r = arg1+arg2;
return r;
end
Code: Select all
function onLoop()
local n = myFunc(4,5);
windowDebug(tostring(n));
end
Code: Select all
function onLoop()
local arg1 = 4;
local arg2 = 5;
local r = arg1+arg2;
local n = r;
windowDebug(tostring(n));
end
-
- Posts: 54
- Joined: 9 years ago
Re: Lua help thread
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?
Re: Lua help thread
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.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?
-
- Posts: 54
- Joined: 9 years ago
Re: Lua help thread
I see. I'll try that, then. Thank you.
- SAJewers
- ASMBXT Level Wrangler/A2XT Project Coordinator /AAT Level Designer
- Posts: 4219
- Joined: 12 years ago
- Location: Nova Scotia
Re: Lua help thread
http://lua-users.org/wiki/MathLibraryTutorial
maybe try that?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. :-)
-
- Posts: 54
- Joined: 9 years ago
Re: Lua help thread
Oh, nice! Now the lotus flowers are giving me stuff in a totally random order! Thanks!
Re: Lua help thread
how do I spawn an NPC with velocity?
(Like, spawning a sniffit bullet with upwards velocity)
(Like, spawning a sniffit bullet with upwards velocity)
Re: Lua help thread
You can spawn the NPC, and save it to a variable, then set the speed. Like this:7NameSam wrote:how do I spawn an NPC with velocity?
(Like, spawning a sniffit bullet with upwards velocity)
Code: Select all
local bullet = NPC.spawn(...);
bullet.speedY = 4;
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.
- CloudyCloud
- Posts: 234
- Joined: 11 years ago
Re: Lua help thread
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?
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?