Page 24 of 76
Re: Need help with lua? - LunaLua General Help
Posted: Thu May 19, 2016 12:54 am
by Emral
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.
Re: Need help with lua? - LunaLua General Help
Posted: Thu May 19, 2016 2:26 pm
by TDK
Is there a way to check if there's no blocks in a certain coordinate?
Re: Need help with lua? - LunaLua General Help
Posted: Thu May 19, 2016 2:31 pm
by Emral
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)
Re: Need help with lua? - LunaLua General Help
Posted: Fri May 20, 2016 3:44 pm
by TDK
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?
Re: Need help with lua? - LunaLua General Help
Posted: Fri May 20, 2016 3:47 pm
by Emral
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.
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 21, 2016 12:41 am
by Quantumenace
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)?
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 21, 2016 9:50 am
by zlaker
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.
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 21, 2016 2:04 pm
by Emral
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.
Re: Need help with lua? - LunaLua General Help
Posted: Wed May 25, 2016 11:04 pm
by Fennor
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
Re: Need help with lua? - LunaLua General Help
Posted: Thu May 26, 2016 4:54 am
by Kevsoft
Type FIELD_STRING should do it.
Re: Need help with lua? - LunaLua General Help
Posted: Fri May 27, 2016 11:48 am
by Reign
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.
Re: Need help with lua? - LunaLua General Help
Posted: Fri May 27, 2016 12:03 pm
by Emral
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).
Re: Need help with lua? - LunaLua General Help
Posted: Fri May 27, 2016 12:15 pm
by Reign
Thanks, I'll just have to live with the normal speed then.
Re: Need help with lua? - LunaLua General Help
Posted: Fri May 27, 2016 3:04 pm
by Fennor
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.
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 28, 2016 10:11 am
by Creasion
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?
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 28, 2016 11:12 am
by Fennor
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)
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 28, 2016 11:39 am
by Emral
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.
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 28, 2016 1:17 pm
by Fennor
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?
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 28, 2016 1:19 pm
by Emral
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.
Re: Need help with lua? - LunaLua General Help
Posted: Sat May 28, 2016 4:09 pm
by Creasion
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?