Page 7 of 11

Re: Need help with lua? - LunaLua General Help

Posted: Fri Aug 30, 2019 1:13 pm
by NoMoreStars
So I'm tyring to get the player's current coordinates and store the x and y positions as variables. I thought this could be accomplished by using a for loop to create a table as you would do with other objects like npcs, however the variables I keep setting to Player.x and Player.y keep returning simply as nil. I'm sure I'm going about this wrong but could someone please explain how I'd go about getting the player coordinates?

Code: Select all

for index,myPlayer in ipairs(Player.get()) do

		local playerPosX = Player.x
		local playerPosY = Player.y
	
	end

Re: Need help with lua? - LunaLua General Help

Posted: Fri Aug 30, 2019 2:12 pm
by Enjl
"local" means a variable is confined to the scope where it's declared. It'll be discarded when the next "end" on the same level of indentation is hit. To illustrate this:

(not using the code block thing cause the smbx forum theme still doesn't display line breaks)

function onStart()
local val = 1
end

print(val) -- prints nil

on the other hand:

local val = 0

function onStart()
val = 1
end

print(val) --prints 1

So what you want is your variables to be declared outside of all functions so that they aren't discarded. Within the functions/loops you can update them then.
Your setup has one other difficulty: You're using a loop over players and saving both player's values into the same variable. Which means that on the first iteration, your variable is set to the location of the first player, and then it's immediately overridden in the second loop. I recommend using a table instead:

local playerPositions = {
{x = 0, y = 0}. -- index 1
{x = 0, y = 0} -- index 2
}

function onTick()
for k,p in ipairs(Player.get()) do
playerPositions[k].x = p.x
playerPositions[k].y = p.y
end
end

Re: Need help with lua? - LunaLua General Help

Posted: Fri Aug 30, 2019 10:34 pm
by FlyingGym
What would replicate a red koopa troopa's invulnerability to falling off of cliffs/blocks?

Re: Need help with lua? - LunaLua General Help

Posted: Sat Aug 31, 2019 5:36 am
by Enjl
FlyingGym wrote:
Fri Aug 30, 2019 10:34 pm
What would replicate a red koopa troopa's invulnerability to falling off of cliffs/blocks?
The "cliffturn" npc config flag.
viewtopic.php?f=35&t=425
Just put "cliffturn=true" without the quotation marks into your npc config file.

Re: Need help with lua? - LunaLua General Help

Posted: Sat Aug 31, 2019 1:38 pm
by FlyingGym
so im using a smb1 gloomba to make an goombrat, what files do i load to activate the cliffturn without getting an error about not loading NPCcode?

Re: Need help with lua? - LunaLua General Help

Posted: Sat Aug 31, 2019 8:53 pm
by DrMekar
FlyingGym wrote:
Sat Aug 31, 2019 1:38 pm
so im using a smb1 gloomba to make an goombrat, what files do i load to activate the cliffturn without getting an error about not loading NPCcode?
If you're fine with with replacing a NPC (The Gloomba) then you can just write "cliffturn=1" in the txt document.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Sep 03, 2019 5:09 pm
by Daring Tombstone
Don't know why this isn't working. It's the dragon coin thing from enjl's video.

Image

Pretty sure this is all correct. (minus it's coinCounter instead of other thing)

My problem is that the counter doesn't increment on collecting a coin. Something wrong in the script and it's me being stupid?

Re: Need help with lua? - LunaLua General Help

Posted: Thu Sep 05, 2019 6:20 am
by Enjl
OFFSCREEN doubles for collection. Instead of that check, try a Colliders.collide(player, v) call for each player on the dying NPC.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Sep 10, 2019 6:43 pm
by Murphmario
Trying to make an NPC that behaves like the Sidehoppers in Super Metroid, but the code doesn't seem to work. It just keeps going through all of it's animation frames, and I don't know why.

If it helps, I partially based this off of Enjl's Bearvor NPC in terms of it's grounded state.
Spoiler: show

Code: Select all

local hoprng = 1
local animationtimer = 0

--Register events
function sampleNPC.onInitAPI()
	npcManager.registerEvent(npcID, sampleNPC, "onTickNPC")
	--npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
	--npcManager.registerEvent(npcID, sampleNPC, "onDrawNPC")
	--registerEvent(sampleNPC, "onNPCKill")
end

function sampleNPC.onTickNPC(v)
	--Don't act during time freeze
	if Defines.levelFreeze then return end
	
	local data = v.data
	
	--If despawned
	if v:mem(0x12A, FIELD_WORD) <= 0 then
		v.data.state = 0
		data.initialized = false
		return
	end

	--Initialize
	if not data.initialized then
		--Initialize necessary data.
		data.initialized = true
	end

	--Depending on the NPC, these checks must be handled differently
	if v:mem(0x12C, FIELD_WORD) > 0    --Grabbed
	or v:mem(0x136, FIELD_BOOL)        --Thrown
	or v:mem(0x138, FIELD_WORD) > 0    --Contained within
	then
		--Handling
	hoprng = hoprng + 1
	if hoprng == 3 then
		hoprng = 1
	end
	
	local ST_Grounded = 0
	local ST_Air = 1
	
	v.data.state = 1
	
	if data.state == ST_Air then
		if v.collidesBlockBottom then
			v.speedX = 0
			v.animationFrame = 0
			data.state = ST_Grounded
		end
	elseif data.state == ST_Grounded then
		animationtimer = animationtimer + 1
		if animationtimer == 1000 then
			v.animationFrame = 1
			if hoprng == 1 then
				v.speedX = 2 * v.direction
				v.speedY = -6
				animationtimer = 0
				data.state = ST_Air
			end
			elseif hoprng == 2 then
				v.speedX = 6 * v.direction
				v.speedY = -2
				animationtimer = 0
				data.state = ST_Air
			end
		end
	end

Re: Need help with lua? - LunaLua General Help

Posted: Tue Sep 10, 2019 7:10 pm
by Enjl
Murphmario wrote:
Tue Sep 10, 2019 6:43 pm
onTick happens before SMBX sets animation frame, onTickEnd happens after. I used onTickEnd.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Sep 11, 2019 3:24 pm
by Daring Tombstone
I might be stupid but gonna ask anyways. I'm trying to play a SFX using lua. I've done this so many times but I'm having trouble somehow in the new PAL version.

Image

This shows up every time I try to play it and have no idea why. Never had this error before so don't know what it means. I should add that i'm attempting to play it through the lunaworld.lua and have the sound effect file in the episode folder instead of the level folder. Do I need to place the sound effect in every level folder or is there a way to play it in the episode folder?

Re: Need help with lua? - LunaLua General Help

Posted: Wed Sep 11, 2019 3:36 pm
by Enjl
It means there is no file called "Uhhh.ogg" in the Beep Boop folder in the Test Level folder.
2 things (assuming PAL build):
1) SFX.play(Misc.resolveFile("Uhhh.ogg")) will search in the episode folder after searching in the level folder.
2) luna.lua in the episode folder is the new lunaworld.lua

Re: Need help with lua? - LunaLua General Help

Posted: Fri Sep 13, 2019 5:02 pm
by NoMoreStars
So i'm trying to implement a situation where something is triggered when a block is hit without using the old event system in order to gain more control. I wanted to try this with the Block:hit function as seen on the Block Class page on the wiki, but I noticed that it only simulates a block hit and returns a nil value. In order for my situation to work, I would need a boolean to be returned to continue on with the control structure. Instead I opted to try using the Block Memory offsets, specifically SMBX Block+0x04 which tracks how many times a block is hit according to the wiki. I don't think I quite have the syntax right though. I'm going off of Enjl's Luna Lua Tutorial 3 Episode where he implemented something similar with boss health by accessing the npc memory and I just copied the syntax he used there for what I'm trying to accomplish:

local hitCount = block:mem(0x04, FIELD_WORD)

I've also set it up so that if hitCount is greater than zero then a sound will play, which is how I know it isn't working properly.
I'm pretty sure I have the looping structure correct for the blocks I am trying to get the memory address for:
Spoiler: show
function onTick()
local offSwitchBlock = Block.get(283)
for k,v in ipairs(offSwitchBlock) do

local hitCount = block:mem(0x04, FIELD_WORD)
if hitCount > 0 then
Audio.playSFX(47)
end

end

end
Any idea what's going on? Is it the syntax of the memory offset like I thought?

Re: Need help with lua? - LunaLua General Help

Posted: Fri Sep 13, 2019 5:12 pm
by Enjl
If you're on the PAL release, you can use onBlockHit instead:

function onBlockHit(eventObj, v, fromUpSide, playerOrNil)
if v.isValid and v.id == 283 then
SFX.play(47)
end
end

eventObj allows you to cancel the hit event, v is the block, fromUpSide is a boolean and playerOrNil is which player hit the block. They're not useful for this case, but useful to know about. Aplogies for the lack of documentation at the moment.

Re: Need help with lua? - LunaLua General Help

Posted: Fri Sep 13, 2019 7:50 pm
by MarioLover64
Would it be possible to change the bounce of the noteblock to for example be one block higher

Re: Need help with lua? - LunaLua General Help

Posted: Fri Sep 13, 2019 7:53 pm
by NoMoreStars
Enjl wrote:
Fri Sep 13, 2019 5:12 pm
If you're on the PAL release, you can use onBlockHit instead:

function onBlockHit(eventObj, v, fromUpSide, playerOrNil)
if v.isValid and v.id == 283 then
SFX.play(47)
end
end

eventObj allows you to cancel the hit event, v is the block, fromUpSide is a boolean and playerOrNil is which player hit the block. They're not useful for this case, but useful to know about. Aplogies for the lack of documentation at the moment.
Would I wanna put that within the loop (that itself is running in the onTick function) or would I wanna run this within it's own function and call on it from the loop? Either way it still doesn't seem to be working despite everything. I am running on the most recent version of the PAL release also. Thanks for the help btw and I understand documentation takes time. I appreciate all the work ya'll put in to make SMBX something really great.

Re: Need help with lua? - LunaLua General Help

Posted: Fri Sep 13, 2019 8:10 pm
by Enjl
No. onBlockHit is a function like onTick or onNPCKill. yYou don't need to check for a block to be hit every frame, nor do you need to manually call it. I basically provided a replacement to the code you had.

If it's not working, please use Misc.dialog(debug message) to find out where the issue lies. A good first step is to print v.isValid before checking for it in onBlockHit.

Re: Need help with lua? - LunaLua General Help

Posted: Sat Sep 14, 2019 1:41 am
by NoMoreStars
Enjl wrote:
Fri Sep 13, 2019 8:10 pm
No. onBlockHit is a function like onTick or onNPCKill. yYou don't need to check for a block to be hit every frame, nor do you need to manually call it. I basically provided a replacement to the code you had.

If it's not working, please use Misc.dialog(debug message) to find out where the issue lies. A good first step is to print v.isValid before checking for it in onBlockHit.
Ah that makes a lot more sense. In that case I got it workin'! Thanks for all your help Enjl.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Sep 18, 2019 5:22 pm
by Daring Tombstone
Is there a page somewhere where I can look up these types of memory values?

mem(0x00B2C8B4, FIELD_WORD, -1)

I looked but must have overlooked it somewhere. (Btw thanks on help so far, forgot misc.resolvefile existed.)

Re: Need help with lua? - LunaLua General Help

Posted: Wed Sep 18, 2019 6:04 pm
by Enjl
Daring Tombstone wrote:
Wed Sep 18, 2019 5:22 pm
Is there a page somewhere where I can look up these types of memory values?

mem(0x00B2C8B4, FIELD_WORD, -1)

I looked but must have overlooked it somewhere. (Btw thanks on help so far, forgot misc.resolvefile existed.)
https://wohlsoft.ru/pgewiki/SMBX_Global_Memory