Need help with lua? - LunaLua General Help

This is the place for discussion and support for LunaLua and related modifications and libraries.
Forum rules
Before you make a topic/post, consider the following:
-Is there a topic for this already?
-Is your post on topic/appropriate?
-Are you posting in the right forum/following the forum rules?
Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9718
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat Jan 05, 2019 3:04 pm

Oh sorry I must've misread the code then. The lack of indenting and declaration of local functions made it seem like that captureAt WAS outside of local functions.

Basic lua loops and operators shouldn't be able to crash your code... especially on a reload. I will say though that using ^ seems a bit sketchy... The operator isn't very performant so usually it's avoided whenever multiplication does the job just fine. What do you need it for?

Oh also unrelated but you should maybe consider drawing your stuff in onDraw so that it renders while paused.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sat Jan 05, 2019 3:14 pm

Enjl wrote:
Sat Jan 05, 2019 3:04 pm
Oh sorry I must've misread the code then. The lack of indenting and declaration of local functions made it seem like that captureAt WAS outside of local functions.

Basic lua loops and operators shouldn't be able to crash your code... especially on a reload. I will say though that using ^ seems a bit sketchy... The operator isn't very performant so usually it's avoided whenever multiplication does the job just fine. What do you need it for?

Oh also unrelated but you should maybe consider drawing your stuff in onDraw so that it renders while paused.
Good point about onDraw there, I had just ignored the fact it doesn't draw while paused.

I need the ^ operator for powers of ten to allow you to customise the number of usable digits on the life counter. I could use multiplication, but I'd have to use it within another loop to perform the same function.

Added in 3 minutes 52 seconds:
Replacing the operator didn't help.

Quantumenace
Ripper II
Ripper II
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Sat Jan 05, 2019 5:53 pm

Tested it by systematically commenting out areas to disable them, the problem lines try to print tostring(trueLives), but that variable doesn't exist and apparently the game really doesn't like it when you try to print a tostring(nil).

Also, that rectangle of screen draws the scene without the black when using that darklevel api. You may want to test whether it appears without particles as well. I think the timing of your captureAt is key there, you'll want to do it after drawing everything else if I understand it correctly.


...The forum really needs a way to paste indents properly.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat Jan 05, 2019 8:12 pm

For the record, for Text.print you don't need a tostring as long as what you're trying to print is a number, boolean or nil.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sun Jan 06, 2019 6:58 am

Quantumenace wrote:
Sat Jan 05, 2019 5:53 pm
Tested it by systematically commenting out areas to disable them, the problem lines try to print tostring(trueLives), but that variable doesn't exist and apparently the game really doesn't like it when you try to print a tostring(nil).

Also, that rectangle of screen draws the scene without the black when using that darklevel api. You may want to test whether it appears without particles as well. I think the timing of your captureAt is key there, you'll want to do it after drawing everything else if I understand it correctly.


...The forum really needs a way to paste indents properly.
Thanks! Must have missed that out when rewriting the code. Doesn't crash anymore and works fine now, although I have a couple of questions relating to 2p support:

1) Is there a way to tell which player caused the loss or gain of a life?
2) How can you draw text to scene coordinates? Text.print and Text.printWP shows the text on both player's screens in splitscreen mode, which I don't want. For graphics there's drawImageToScene and drawImageToSceneWP (which I'm using), but there seems to be no text equivalent.

Added in 8 minutes 28 seconds:
I've also changed the captureAt priority to 4.0 (just below the HUD)

Added in 11 minutes 43 seconds:
I've taken a look at textblox.lua and although it allows you to draw text to scene co-ordinates, it has no priority/z layer option, so that won't work for my purposes.

PixelPest
Raccoon Mario
Raccoon Mario
Posts: 7111
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Contact:

Re: Need help with lua? - LunaLua General Help

Postby PixelPest » Sun Jan 06, 2019 9:00 am

The Dwarven Digger wrote:
Sun Jan 06, 2019 7:19 am
Quantumenace wrote:
Sat Jan 05, 2019 5:53 pm
Tested it by systematically commenting out areas to disable them, the problem lines try to print tostring(trueLives), but that variable doesn't exist and apparently the game really doesn't like it when you try to print a tostring(nil).

Also, that rectangle of screen draws the scene without the black when using that darklevel api. You may want to test whether it appears without particles as well. I think the timing of your captureAt is key there, you'll want to do it after drawing everything else if I understand it correctly.


...The forum really needs a way to paste indents properly.
Thanks! Must have missed that out when rewriting the code. Doesn't crash anymore and works fine now, although I have a couple of questions relating to 2p support:

1) Is there a way to tell which player caused the loss or gain of a life?
2) How can you draw text to scene coordinates? Text.print and Text.printWP shows the text on both player's screens in splitscreen mode, which I don't want. For graphics there's drawImageToScene and drawImageToSceneWP (which I'm using), but there seems to be no text equivalent.

Added in 8 minutes 28 seconds:
I've also changed the captureAt priority to 4.0 (just below the HUD)

Added in 11 minutes 43 seconds:
I've taken a look at textblox.lua and although it allows you to draw text to scene co-ordinates, it has no priority/z layer option, so that won't work for my purposes.
If you want to just draw plain text to the scene you can use the values given in the Camera class to figure out its position relative to the screen based on scene coordinates. If you actually create a Block in textblox it has a z property. I'd also take a look in the file just to see if it's possible it was just missed for printExt

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sun Jan 06, 2019 9:22 am

PixelPest wrote:
Sun Jan 06, 2019 9:00 am
The Dwarven Digger wrote:
Sun Jan 06, 2019 7:19 am
Quantumenace wrote:
Sat Jan 05, 2019 5:53 pm
Tested it by systematically commenting out areas to disable them, the problem lines try to print tostring(trueLives), but that variable doesn't exist and apparently the game really doesn't like it when you try to print a tostring(nil).

Also, that rectangle of screen draws the scene without the black when using that darklevel api. You may want to test whether it appears without particles as well. I think the timing of your captureAt is key there, you'll want to do it after drawing everything else if I understand it correctly.


...The forum really needs a way to paste indents properly.
Thanks! Must have missed that out when rewriting the code. Doesn't crash anymore and works fine now, although I have a couple of questions relating to 2p support:

1) Is there a way to tell which player caused the loss or gain of a life?
2) How can you draw text to scene coordinates? Text.print and Text.printWP shows the text on both player's screens in splitscreen mode, which I don't want. For graphics there's drawImageToScene and drawImageToSceneWP (which I'm using), but there seems to be no text equivalent.

Added in 8 minutes 28 seconds:
I've also changed the captureAt priority to 4.0 (just below the HUD)

Added in 11 minutes 43 seconds:
I've taken a look at textblox.lua and although it allows you to draw text to scene co-ordinates, it has no priority/z layer option, so that won't work for my purposes.
If you want to just draw plain text to the scene you can use the values given in the Camera class to figure out its position relative to the screen based on scene coordinates. If you actually create a Block in textblox it has a z property. I'd also take a look in the file just to see if it's possible it was just missed for printExt
Turns out printExt does have a z property that was missed in the PGE wiki documentation. However, it still has the same problem that drawText had: It draws the text on both player's screens simultaneously. The textblox API probably converts the scene coordinates to screen co-ords when it draws the text.

Also, I'm getting a weird error of "No matching overload found" when I place my API in the episode folder rather than the level folder that makes no sense being there. It doesn't matter if I load it through lunadll or lunaworld, the error still persists. Based on the traceback it seems to be coming from drawImageToSceneWP.

Added in 51 minutes 1 second:
By making images for each number and using my own printing function, I've managed to solve the issue of numbers drawing on both player's screens now.

Added in 5 hours 49 minutes 49 seconds:
Using Misc.resolveGraphicsFile() around each image path seemed to fix that error. I still don't know if it's possible to tell which player caused a gain/loss of a life though.
Is it possible to, using an onNPCKill event, find out which player killed said NPC, and what their current point combo is? If so I could ignore the lives being added from there and instead add the lives to the relevant player's life total using my own code.
Also, Is there anything to tell when a player dies?
If I can get all cases of life losses/gains and find the responsible player, then I can almost entirely ignore the standard life total and add/subtract the relevant values using my own code, but I have no idea if or how I could do this.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sun Jan 06, 2019 5:10 pm

The Dwarven Digger wrote:
Sun Jan 06, 2019 4:03 pm
1) Is there a way to tell which player caused the loss or gain of a life?
Lives are shared. No.
The Dwarven Digger wrote:
Sun Jan 06, 2019 4:03 pm
2) How can you draw text to scene coordinates? Text.print and Text.printWP shows the text on both player's screens in splitscreen mode, which I don't want. For graphics there's drawImageToScene and drawImageToSceneWP (which I'm using), but there seems to be no text equivalent.
Which function are you printing the text in? Certain ones get executed for each camera. Rendering to scene won't change this.
The Dwarven Digger wrote:
Sun Jan 06, 2019 4:03 pm
Also, I'm getting a weird error of "No matching overload found" when I place my API in the episode folder rather than the level folder that makes no sense being there. It doesn't matter if I load it through lunadll or lunaworld, the error still persists. Based on the traceback it seems to be coming from drawImageToSceneWP.
Can't help if you don't provide information such as relevant code lines and the contents of the episode and level folders. Likely related to a missing Misc.resolveFile if images were moved outside of the level folder as well, though.
The Dwarven Digger wrote:
Sun Jan 06, 2019 4:03 pm
Is it possible to, using an onNPCKill event, find out which player killed said NPC, and what their current point combo is?
Not reliably.
The Dwarven Digger wrote:
Sun Jan 06, 2019 4:03 pm
Also, Is there anything to tell when a player dies?
Offset 0x13E is the death timer.

Even if you figure out which player added which life and removed which life, there's another thing that I'm unsure if it can be caught at all. After a player's death, they can respawn with a button press when lives are left in the pool. The respawn is handled by vanilla SMBX using the default life counter. I don't know how you would work around that.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sun Jan 06, 2019 5:34 pm

I've fixed the error with the episode folder (Needed more Misc.resolveFile() functions) and the drawing of text to scene co-ordinates, and I'm trying to find a workaround for the shared lives pool. I've managed to catch the player deaths and, if it's player 2 that just died, shifts a life from player 1's counter to player 2's. However, overriding the player's inputs for the respawn seems to do nothing, I suspect that
a) The respawn key press is handled in a different way to any other, such that overriding inputs does not work. Perhaps it's executed before the onInputUpdate function.
or b) Player 1's inputs also work for the respawn
I'll try overriding both player's inputs to see if that makes any difference, but re-enabling all the normal functions for the inputs would be rather cumbersome and annoying.

Perhaps I could work through each death type to see if I can record the player that caused it. Maybe things like using the nearest player on a kill requiring physical contact with the player, for kills requiring contact with another NPC I could see which player (if any) threw it using memory offsets, etc. I can probably use the kill counters for the player (stomps, tail swipes?) and projectiles to work out if they should be producing lives or not. As for bonking, the block doesn't seem to store who bonked it (despite player being an optional argument for block:hit()). There's no unused memory addresses, only unknowns, so I may have to use an additional table for that.

Basically I might be able to work around it, but it'll probably be rather clunky and horrible.

Added in 2 minutes 12 seconds:
Overriding both player's inputs does nothing. Respawning must use a separate method of getting key presses. Not sure how or if this can be worked around, but I'm not giving up just yet.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sun Jan 06, 2019 5:37 pm

The respawn event, like all SMBX tick logic (not draw logic), is handled after onTick and before onTickEnd every frame.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sun Jan 06, 2019 5:48 pm

Enjl wrote:
Sun Jan 06, 2019 5:37 pm
The respawn event, like all SMBX tick logic (not draw logic), is handled after onTick and before onTickEnd every frame.
...and onInputUpdate is before onTick, correct? In which case, the input override would work if the respawn event used the same system as all other SMBX input-related events (like movement). I know there's an offset for the respawn animation state but apparently you have to use extreme caution around it so I'm not sure if I can edit that without causing a crash.

fern
Swooper
Swooper
Posts: 56
Joined: Sat Dec 02, 2017 8:59 pm

Re: Need help with lua? - LunaLua General Help

Postby fern » Sun Jan 06, 2019 6:25 pm

Is there a way to detect the type of slope under the player? The memory offset named "slope modifier" (0x48) seems to give random values.

Quantumenace
Ripper II
Ripper II
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Sun Jan 06, 2019 7:02 pm

It's not random, it looks like it returns the index of the slope block being stood on, but it's 0 if the player isn't on one. You should be able to get the block using

local b=Block.get()[slopeblock]

where slopeblock is the value at 0x48. Don't try to do it if that's 0, it's out of the index range and will probably cause problems. You can get the type of slope using the block's .id field and the list of IDs of slope blocks in the colliders API.

There may be a risk that something might get messed up if you have something that changes the number and index of blocks in the level, like a blue P-switch or Block.spawn, and it changes that between when the game sets the 0x48 value and when you access it.

fern
Swooper
Swooper
Posts: 56
Joined: Sat Dec 02, 2017 8:59 pm

Re: Need help with lua? - LunaLua General Help

Postby fern » Sun Jan 06, 2019 7:37 pm

Quantumenace wrote:
Sun Jan 06, 2019 7:02 pm
It's not random, it looks like it returns the index of the slope block being stood on, but it's 0 if the player isn't on one. You should be able to get the block using

local b=Block.get()[slopeblock]

where slopeblock is the value at 0x48. Don't try to do it if that's 0, it's out of the index range and will probably cause problems. You can get the type of slope using the block's .id field and the list of IDs of slope blocks in the colliders API.

There may be a risk that something might get messed up if you have something that changes the number and index of blocks in the level, like a blue P-switch or Block.spawn, and it changes that between when the game sets the 0x48 value and when you access it.
Hey, tyvm. You were right about it being the index of the block, however I'm not familiar with colliders.lua, how would you go about finding the slope type? I'm seeing that there's tables with the IDs of each type, do I just check each array for the value? Or does it include a function to do this?

Quantumenace
Ripper II
Ripper II
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Sun Jan 06, 2019 7:54 pm

What I do for stuff like this is make a table:

(this goes outside functions)
local blockSlopeTypes={}
for k,v in ipairs({452,321,365,316,357,358,306,305,302,616,299,340,341,472,480,636,635,326,324,604,600,332}) do blockSlopeTypes[v]=1 end --bottom left to top right slope "/"
for k,v in ipairs({451,319,366,315,359,360,308,307,301,617,300,343,342,474,482,638,637,327,325,601,605,333}) do blockSlopeTypes[v]=2 end --top left to bottom right slope "\"

and then you can use blockSlopeTypes[b.id] to get 1 or 2 to indicate the type. It will be nil if it isn't any of those types.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Mon Jan 07, 2019 11:08 am

Sorry, me again. I've noticed that at the time of onNPCKill being called, any player or NPC/projectile kill combo counters are always one step ahead of the amount of points being awarded. I presume this is because by the time of the event calling, they've already registered the kill and incremented their kill count values. Is there any way I could get a more accurate value that reflects the number of points (or lives) awarded for that kill other than keeping a table of these values for every NPC, and updating this in onTick()? And would that approach be able to cope with multiple enemy kills in a single frame?

fern
Swooper
Swooper
Posts: 56
Joined: Sat Dec 02, 2017 8:59 pm

Re: Need help with lua? - LunaLua General Help

Postby fern » Mon Jan 07, 2019 4:36 pm

Quantumenace wrote:
Sun Jan 06, 2019 7:54 pm
What I do for stuff like this is make a table:

(this goes outside functions)
local blockSlopeTypes={}
for k,v in ipairs({452,321,365,316,357,358,306,305,302,616,299,340,341,472,480,636,635,326,324,604,600,332}) do blockSlopeTypes[v]=1 end --bottom left to top right slope "/"
for k,v in ipairs({451,319,366,315,359,360,308,307,301,617,300,343,342,474,482,638,637,327,325,601,605,333}) do blockSlopeTypes[v]=2 end --top left to bottom right slope "\"

and then you can use blockSlopeTypes[b.id] to get 1 or 2 to indicate the type. It will be nil if it isn't any of those types.
Thank you very much, what I ended up doing was using one of the APIs functions to determine which way the slope faces and then just checking the block's width to categorize them by steepness.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Fri Jan 11, 2019 4:56 pm

In the NPC Memory map are two locations listed as unused: 0x38 and 0xD0. It turns out that (after setting these to other values in an attempt to use them as per-NPC stored variables) these are actually spawn X and Y speeds, which makes sense considering the structure of the memory locations before them:
X position, Y position, Height, Width, X speed, Y speed,
Spawn X, Spawn Y, Spawn Height, Spawn Width, 0x38, 0xD0

Since these were the only "unused" memory locations listed on the PGE wiki, does anyone know if there are actually any unused NPC memory locations I could utilise that are listed as "unknown"? Otherwise, how could I store data on a per-NPC basis? The NPC array index seems to be rather odd, and for certain NPCs (such as player projectiles) it is constantly set to 0.

PixelPest
Raccoon Mario
Raccoon Mario
Posts: 7111
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Contact:

Re: Need help with lua? - LunaLua General Help

Postby PixelPest » Fri Jan 11, 2019 4:59 pm

The Dwarven Digger wrote:
Fri Jan 11, 2019 4:56 pm
In the NPC Memory map are two locations listed as unused: 0x38 and 0xD0. It turns out that (after setting these to other values in an attempt to use them as per-NPC stored variables) these are actually spawn X and Y speeds, which makes sense considering the structure of the memory locations before them:
X position, Y position, Height, Width, X speed, Y speed,
Spawn X, Spawn Y, Spawn Height, Spawn Width, 0x38, 0xD0

Since these were the only "unused" memory locations listed on the PGE wiki, does anyone know if there are actually any unused NPC memory locations I could utilise that are listed as "unknown"? Otherwise, how could I store data on a per-NPC basis? The NPC array index seems to be rather odd, and for certain NPCs (such as player projectiles) it is constantly set to 0.
Use pnpc.lua. It is intended exactly for that. You can find documentation for it on the wiki

Quantumenace
Ripper II
Ripper II
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Fri Jan 11, 2019 9:52 pm

Yeah, I don't know why the wiki says that 0xE6 is the index of the NPC in the array without qualifiers unless that's a new feature in Beta 4. As far as I know, pnpc uses that address for its index because it's otherwise completely unused.

Edit: Oh, some NPCs do have that value set for the array now. I tested it in the previous beta and it is not used there, it's probably used now because some of the new NPCs need pnpc to function.


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari