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?
User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sat Jan 05, 2019 2:57 pm

Enjl wrote:
Sat Jan 05, 2019 2:47 pm
Just a hunch but... might be related to you capturing outside of functions with the capture buffer. At that point in time most SMBX objects aren't initialised yet, so that might lead to an error.
The buffer object is initialised outside of functions but the captureAt function is only called inside the onTick() function. This is the exact setup I had in the previous version that worked fine, so that's not it. The main new things I've used (new for the API and for me) are the different type of for loops (as in "for i=1, 3, 1 do ... "), the % operator and the ^ operator. I've used everything else in some way before.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 6778
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » 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.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
Quantumenace
Mega Mole
Mega Mole
Posts: 260
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.

User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 6778
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » 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.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
PixelPest
Link
Link
Posts: 7129
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Current Project: Boom Boom Evolution
Location: Canada, Eh?
Chat Username: PixelPest
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

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 6778
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » 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.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 6778
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » 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.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
fern
Spiny
Spiny
Posts: 55
Joined: Sat Dec 02, 2017 8:59 pm
Chat Username: fern

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.

User avatar
Quantumenace
Mega Mole
Mega Mole
Posts: 260
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.

User avatar
fern
Spiny
Spiny
Posts: 55
Joined: Sat Dec 02, 2017 8:59 pm
Chat Username: fern

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?

User avatar
Quantumenace
Mega Mole
Mega Mole
Posts: 260
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.

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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?
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
fern
Spiny
Spiny
Posts: 55
Joined: Sat Dec 02, 2017 8:59 pm
Chat Username: fern

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.

User avatar
The Dwarven Digger
Lakitu
Lakitu
Posts: 481
Joined: Sun Oct 30, 2016 11:17 am
Current Project: Currently unnamed
Location: In the mines

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.
I support: show
Image
Image
Due to a problem with my laptop, all my projects and levels have been lost. But fear not, for new ones will be arriving! (and hopefully completed)
Spoiler: show
rockythechao wrote:Image

User avatar
PixelPest
Link
Link
Posts: 7129
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Current Project: Boom Boom Evolution
Location: Canada, Eh?
Chat Username: PixelPest
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


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 3 guests