Need help with lua? - LunaLua General Help

This is the place for discussion and support for LunaLua and related modifications and libraries.

Moderator: Userbase Moderators

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: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Thu May 19, 2016 12:54 am

You can play the sound using playSFX(int soundindex)
The index for the thwomp can be found in ini_examples\sounds.ini

You can do a getIntersecting check in an area relative to the player and kill certain enemies when they're in range. A constant for all hurtable npc ids is found in horikawaTools.lua

That method is quite dodgy and finnicky, though. I don't know if there is an easier way.

TDK
Phanto
Phanto
Posts: 1440
Joined: Wed Nov 11, 2015 12:26 pm
Flair: Retired

Re: Need help with lua? - LunaLua General Help

Postby TDK » Thu May 19, 2016 2:26 pm

Is there a way to check if there's no blocks in a certain coordinate?

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Thu May 19, 2016 2:31 pm

TheDinoKing432 wrote:Is there a way to check if there's no blocks in a certain coordinate?
Block.getIntersecting(number x1, number y1, number x2, number y2)

TDK
Phanto
Phanto
Posts: 1440
Joined: Wed Nov 11, 2015 12:26 pm
Flair: Retired

Re: Need help with lua? - LunaLua General Help

Postby TDK » Fri May 20, 2016 3:44 pm

Enjl wrote:
TheDinoKing432 wrote:Is there a way to check if there's no blocks in a certain coordinate?
Block.getIntersecting(number x1, number y1, number x2, number y2)
Thanks.
Also, is there a way to make an enemy spawn anywhere inside the screen?

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Fri May 20, 2016 3:47 pm

TheDinoKing432 wrote:
Enjl wrote:
TheDinoKing432 wrote:Is there a way to check if there's no blocks in a certain coordinate?
Block.getIntersecting(number x1, number y1, number x2, number y2)
Thanks.
Also, is there a way to make an enemy spawn anywhere inside the screen?
Grab coordinates of the camera with Camera.get()[playerIndex], use rng library to generate random numbers between 0-800 and 0-600 respectively and use NPC.spawn.

Quantumenace
Chain Chomp
Chain Chomp
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Sat May 21, 2016 12:41 am

Is there a way to forbid the screen from splitting in 2-player (short of forcing the section boundaries to always be the size of a screen)?

zlaker
Reznor
Reznor
Posts: 2844
Joined: Fri Dec 20, 2013 1:46 pm

Re: Need help with lua? - LunaLua General Help

Postby zlaker » Sat May 21, 2016 9:50 am

I've this drawn image on screen and I want it to be kept being drawn on screen whenever a message box is open since it disappears whenever one pops up.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat May 21, 2016 2:04 pm

zlakergirl357 wrote:I've this drawn image on screen and I want it to be kept being drawn on screen whenever a message box is open since it disappears whenever one pops up.
You're drawing in onTick. Draw in onDraw instead.

Fennor
Buster Beetle
Buster Beetle
Posts: 94
Joined: Sat Apr 25, 2015 12:08 pm

Re: Need help with lua? - LunaLua General Help

Postby Fennor » Wed May 25, 2016 11:04 pm

How do I get an NPC into a specific layer? On the NPC offset page I found that 0x3C is for the Layer. I know how to use v:mem when I want to set it to a number, but it doesn't work whenever I have to use text.

How do I have to write the example below when Memory type is "VB6 String pointer" instead of "FIELD_WORD" and I have a text instead of a number?

Code: Select all

if v:mem(0x00,FIELD_WORD) == 1 then
	v:mem(0x01,FIELD_WORD,1)
end

Kevsoft
Ripper II
Ripper II
Posts: 375
Joined: Sun Jul 27, 2014 8:03 am

Re: Need help with lua? - LunaLua General Help

Postby Kevsoft » Thu May 26, 2016 4:54 am

Type FIELD_STRING should do it.

Reign
Chain Chomp
Chain Chomp
Posts: 327
Joined: Tue Jan 21, 2014 4:22 am

Re: Need help with lua? - LunaLua General Help

Postby Reign » Fri May 27, 2016 11:48 am

Slowing down a koopa shell, for example NPC-115, doesn't seem to work with NPC codes (speed=0.5 etc.). I'm wondering if there is way to slow it down with LunaLua? I'm trying to study NPC (class) here but rather confused as where I'd have to start with this.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Fri May 27, 2016 12:03 pm

There's no reliable way to modify a NPC's speed with lua. SMBX overrides speedX/speedY values when there's vanilla movement on either axis in the current frame.
You COULD set the x/y position to something like:

Code: Select all

koopa.x = koopa.x - 0.5 * koopa.direction
to essentially slow it by half its speed. However, that's an ugly way to do it and things can break if you're doing this (when hitting a wall, change of direction doesn't trigger on its own).

Reign
Chain Chomp
Chain Chomp
Posts: 327
Joined: Tue Jan 21, 2014 4:22 am

Re: Need help with lua? - LunaLua General Help

Postby Reign » Fri May 27, 2016 12:15 pm

Thanks, I'll just have to live with the normal speed then.

Fennor
Buster Beetle
Buster Beetle
Posts: 94
Joined: Sat Apr 25, 2015 12:08 pm

Re: Need help with lua? - LunaLua General Help

Postby Fennor » Fri May 27, 2016 3:04 pm

You could make shells act like they are in water. Because in water shells are slower. (Maybe too slow, though?) They would also fall slower, which you probably don't want.

Creasion
Buster Beetle
Buster Beetle
Posts: 89
Joined: Sat Dec 19, 2015 9:58 am

Re: Need help with lua? - LunaLua General Help

Postby Creasion » Sat May 28, 2016 10:11 am

Is there a way to make it so that an npc changes into another npc after a set amount of time or even randomly and then reverts back to normal.

Also is there a way to make homing npcs?

Fennor
Buster Beetle
Buster Beetle
Posts: 94
Joined: Sat Apr 25, 2015 12:08 pm

Re: Need help with lua? - LunaLua General Help

Postby Fennor » Sat May 28, 2016 11:12 am

Code: Select all

local Timer = 120

function onLoop()
	if Timer == 60 then
		for k,v in pairs(NPC.get(1, player.section)) do	
			v.id = 2; 
			Timer = Timer -1
		end	
	else
		if Timer == 0 then
			for k,v in pairs(NPC.get(2, player.section)) do	
				v.id = 1
				Timer = 120
			end
		else
			Timer = Timer -1 
		end
	end			
end	
This turns a brown Goomba into a Red Goomba after 1 second, and back to a brown one after another second.

For chasing it depends what you want. You could even make a very smart chaser who tries to fly where you will be in a few seconds, not where you are. For normal chasing you need to compare the position of the enemy to the position of the player. Like:

Code: Select all

if player.x <= v.x then
     v.speedX = -1
else
     v.speedX = 1     
end
The enemy will go left when the player xposition is lower than the xposition of the enemy, in other words if the player is further to the left, otherwise it will go right. This is would not look very smooth, though. Because he would go from going left to right instantly after passing you. Instead you would probably want to give him some sort of acceleration. For that you would have to write v.speedX = v.speedX -0.1 instead.(or +0.1 when he is going right)

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat May 28, 2016 11:39 am

Spoilers: None of your code will work.
Fennor wrote:

Code: Select all

local Timer = 120

function onLoop()
	if Timer == 60 then
		for k,v in pairs(NPC.get(1, player.section)) do	
			v.id = 2; 
			Timer = Timer -1
		end	
	else
		if Timer == 0 then
			for k,v in pairs(NPC.get(2, player.section)) do	
				v.id = 1
				Timer = 120
			end
		else
			Timer = Timer -1 
		end
	end			
end	
This turns a brown Goomba into a Red Goomba after 1 second, and back to a brown one after another second.
Few things before I propose a new code:
-SMBX runs at 64.14 frames per second. Your changes would happen slightly faster.
-use onTick, not onLoop.
-you decrement the timer by 1 for each brown goomba in the player section. There's a chance you get below zero just from that first for loop.
Here's what I propose. Comments are added to make what I'm doing more transparent:

Code: Select all

local pnpc = API.load(pnpc) -- pnpc is to store a value for the timer for each NPC

function onTick()
	for k,v in ipairs(NPC.get({1,2}, player.section)) do
		local w = pnpc.wrap(v) --wraps the npc into pnpc or returns a pnpc object if it has been already wrapped
		if w.data.timer == nil then --initiate timer
			w.data.timer = 1
		end
		if w.data.timer%64 == 0 then --or 65, depends on how you want to measure seconds. This is true every 64 ticks.
			if w.id == 1 then
				w.id = 2
			else
				w.id = 1
			end
		end
		w.data.timer = w.data.timer + 1 --you could add a check to see if the npc is onscreen.
	end
end
Fennor wrote: For chasing it depends what you want. You could even make a very smart chaser who tries to fly where you will be in a few seconds, not where you are. For normal chasing you need to compare the position of the enemy to the position of the player. Like:

Code: Select all

if player.x <= v.x then
     v.speedX = -1
else
     v.speedX = 1     
end
The enemy will go left when the player xposition is lower than the xposition of the enemy, in other words if the player is further to the left, otherwise it will go right. This is would not look very smooth, though. Because he would go from going left to right instantly after passing you. Instead you would probably want to give him some sort of acceleration. For that you would have to write v.speedX = v.speedX -0.1 instead.(or +0.1 when he is going right)
Getting this wrong isn't your fault. It's SMBX's and we're not being very transparent about the issue:
SMBX overrides your changes to speedX/speedY if there already is movement on that axis in that frame. This is also why you can't move NPCs with layers and events unless they're generators or very specific non-moving NPCs. (I think)

Besides, you're checking for the upper left corner of the NPC and player. You could do something like this:

Code: Select all

local playerCenter = player.x + 0.5 * player.width
for k,v blah do
	local enemyCenter = v.x + 0.5*v.width
	if enemyCenter > playerCenter then
		v.x = v.x + 1
	else --be aware that this will also trigger when the two coordinates are the same
		v.x = v.x - 1
	end
end
This WILL work, but it's kind of a hack-job. I have previously mentioned that modifying the x and y of an NPC directly can have unwanted results. For homing NPCs you're better off making an object entirely with lua and using vectors for the distance between the objects you want to eventually meet.

Fennor
Buster Beetle
Buster Beetle
Posts: 94
Joined: Sat Apr 25, 2015 12:08 pm

Re: Need help with lua? - LunaLua General Help

Postby Fennor » Sat May 28, 2016 1:17 pm

Enjl wrote:Besides, you're checking for the upper left corner of the NPC and player.
Totally forgot about that. I thought about it when building my level, but relized the npc and player had the same width, so it didn't matter. Later when I added custom graphics with different width I didn't remember.

I also have a question about that: What I want to do is an npc who shoots projectiles (spawns another npc where he stands). So I probably have to create the projectile on npc1Xpos + 0.5 * npc1Witdh - 0.5 * npc2Width

Code: Select all

for k,v1 in pairs(NPC.get(209, player.section)) do
	for k,v2 in pairs(NPC.get(260, player.section)) do
		NPC.spawn(260, v1.x + (0.5 * v1.width) - (0.5 * v2.width), v1.y , player.section)			
	end
end	
But the problem with that is that before shooting once the projectile won't exist, so it won't do that. If I just place it somewhere first it will work. Is there a way to make it work without doing that? Or do I have to just manually create a variable with the npc width and include that in the equation?

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat May 28, 2016 1:19 pm

There are more arguments for NPC.spawn:
NPC.spawn(int npcId, number x, number y, int sectionNumber, boolean respawn, boolean centered)
if you set "centered" to true you will spawn the center of the NPC at the specified coordinates.

Creasion
Buster Beetle
Buster Beetle
Posts: 89
Joined: Sat Dec 19, 2015 9:58 am

Re: Need help with lua? - LunaLua General Help

Postby Creasion » Sat May 28, 2016 4:09 pm

By homing I mean like an npc that literally flies to you like a heat seeking missile and any direction you go to try and avoid it , it will stick to you and not fly off like a parakoopa does.

Do those codes do this?


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 2 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari