lunalua warning every tick causing code to not work?

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Tue Aug 31, 2021 11:56 pm

I just got the code i needed for a boss fight from wyldstryker and i am getting this warning every tick
Spoiler: show
2t - Warning: Overwritten handler 'onNPCHarm' in worlds/Potential Episode/Me!/luna.lua
stack traceback:
main.lua:717: in function '__newindex'
worlds/Potential Episode/Me!/luna.lua:13: in function <worlds/Potential Episode/Me!/luna.lua:12>
scripts/base/engine/main_events.lua:179: in function 'callEventInternal'
scripts/base/engine/main_events.lua:240: in function <scripts/base/engine/main_events.lua:218>
[C]: in function '__xpcall'
main.lua:780: in function <main.lua:779>
the code also doesnt seem to work and I think it is because of this error so if anyone knows how to fix this and/or what this means that would be awesome :) :D . I am not sure if this post is in the right area or not?

Hoeloe
Phanto
Phanto
Posts: 1465
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Pronouns: she/her

Re: lunalua warning every tick causing code to not work?

Postby Hoeloe » Wed Sep 01, 2021 7:00 pm

It means you've used onNPCHarm twice, just like the error says. You can't use more than one of the same handler in the same file.

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Wed Sep 01, 2021 10:06 pm

this is the code im using
Spoiler: show
function onNPCHarm(eventObj,killedNPC,killReason,culpritOrNil )

if killedNPC.id==751 and killedNPC.ai5>1 then
eventObj.cancelled = true
killedNPC.ai5=killedNPC.ai5-1
end
end
end
im not sure what is exactly wrong here it doesnt look like im using it twice?

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

Re: lunalua warning every tick causing code to not work?

Postby Emral » Thu Sep 02, 2021 5:16 am

You have more code than that. If this was all the code, it would error due to too many "end"s.

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Thu Sep 02, 2021 10:41 am

I am very confused though because this is ALL the code in my lua file
Spoiler: show
function onTick()
function onNPCHarm(eventObj,killedNPC,killReason,culpritOrNil )

if killedNPC.id==751 and killedNPC.ai5>1 then
eventObj.cancelled = true
killedNPC.ai5=killedNPC.ai5-1
end
end
end
I checked my folder for a duplicate luna.lua or something and nothing is there the only thing i can think of that is conflicting with this is npc-751.lua which is this
Spoiler: show
--NPCManager is required for setting basic NPC properties
local npcManager = require("npcManager")

--Create the library table
local sampleNPC = {}
--NPC_ID is dynamic based on the name of the library file
local npcID = NPC_ID

--Defines NPC config for our NPC. You can remove superfluous definitions.
local sampleNPCSettings = {
id = npcID,
--Sprite size
gfxheight = 128,
gfxwidth = 128,
--Hitbox size. Bottom-center-bound to sprite size.
width = 128,
height = 128,
--Sprite offset from hitbox for adjusting hitbox anchor on sprite.
gfxoffsetx = 0,
gfxoffsety = 0,
--Frameloop-related
frames = 1,
framestyle = 0,
framespeed = 8, --# frames between frame change
--Movement speed. Only affects speedX by default.
speed = 1,
--Collision-related
npcblock = false,
npcblocktop = false, --Misnomer, affects whether thrown NPCs bounce off the NPC.
playerblock = false,
playerblocktop = false, --Also handles other NPCs walking atop this NPC.

nohurt=false,
nogravity = false,
noblockcollision = false,
nofireball = false,
noiceball = false,
noyoshi= false,
nowaterphysics = false,
--Various interactions
jumphurt = false, --If true, spiny-like
spinjumpsafe = false, --If true, prevents player hurt when spinjumping
harmlessgrab = false, --Held NPC hurts other NPCs if false
harmlessthrown = false, --Thrown NPC hurts other NPCs if false

grabside=false,
grabtop=false,

--Identity-related flags. Apply various vanilla AI based on the flag:
--iswalker = false,
--isbot = false,
--isvegetable = false,
--isshoe = false,
--isyoshi = false,
--isinteractable = false,
--iscoin = false,
--isvine = false,
--iscollectablegoal = false,
--isflying = false,
--iswaternpc = false,
--isshell = false,

--Emits light if the Darkness feature is active:
--lightradius = 100,
--lightbrightness = 1,
--lightoffsetx = 0,
--lightoffsety = 0,
--lightcolor = Color.white,

--Define custom properties below
}

--Applies NPC settings
npcManager.setNpcSettings(sampleNPCSettings)

--Register the vulnerable harm types for this NPC. The first table defines the harm types the NPC should be affected by, while the second maps an effect to each, if desired.
npcManager.registerHarmTypes(npcID,
{
--HARM_TYPE_JUMP,
--HARM_TYPE_FROMBELOW,
HARM_TYPE_NPC,
HARM_TYPE_PROJECTILE_USED,
--HARM_TYPE_LAVA,
--HARM_TYPE_HELD,
--HARM_TYPE_TAIL,
--HARM_TYPE_SPINJUMP,
--HARM_TYPE_OFFSCREEN,
--HARM_TYPE_SWORD
},
{
--[HARM_TYPE_JUMP]=10,
--[HARM_TYPE_FROMBELOW]=10,
[HARM_TYPE_NPC]=10,
[HARM_TYPE_PROJECTILE_USED]=10,
--[HARM_TYPE_LAVA]={id=13, xoffset=0.5, xoffsetBack = 0, yoffset=1, yoffsetBack = 1.5},
--[HARM_TYPE_HELD]=10,
--[HARM_TYPE_TAIL]=10,
--[HARM_TYPE_SPINJUMP]=10,
--[HARM_TYPE_OFFSCREEN]=10,
--[HARM_TYPE_SWORD]=10,
}
);

--Custom local definitions below


--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.despawnTimer <= 0 then
--Reset our properties, if necessary
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
end

--Execute main AI. This template just jumps when it touches the ground.
v.ai5 = 10
v.speedX = 1
end

--Gotta return the library table!
return sampleNPC
so im really not sure where the duplicate instance is?

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

Re: lunalua warning every tick causing code to not work?

Postby Emral » Thu Sep 02, 2021 11:50 am

See, that top line was very important!
You should never define one function inside another. You're defining a new onNPCHarm every frame. 64 times a second. Get rid of the onTick around the onNPCHarm to fix it.
Last edited by Emral on Thu Sep 02, 2021 11:51 am, edited 1 time in total.

Marioman2007
2025 Egg Hunter
2025 Egg Hunter
Posts: 530
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: lunalua warning every tick causing code to not work?

Postby Marioman2007 » Thu Sep 02, 2021 11:50 am

KoolGamer32 wrote:
Thu Sep 02, 2021 10:41 am
function onTick()
function onNPCHarm(eventObj,killedNPC,killReason,culpritOrNil )

if killedNPC.id==751 and killedNPC.ai5>1 then
eventObj.cancelled = true
killedNPC.ai5=killedNPC.ai5-1
end
end
end
this should be:

Code: Select all

function onNPCHarm(eventObj,killedNPC,killReason,culpritOrNil )
    if killedNPC.id==751 and killedNPC.ai5>1 then
        eventObj.cancelled = true
        killedNPC.ai5=killedNPC.ai5-1
    end
end
EDIT: this is just what Enjl said (I didn't saw their post)

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Thu Sep 02, 2021 6:12 pm

thanks for all the help the errors are now fixed but there is another problem. I am trying to make a custom boss that takes 10 hits to kill and whenever i shoot a projectile like a hammer or toads boomering it doesnt do anything except score points and 1ups extremely quickly. I am trying to make a functioning health system, Is this even the correct code for an npcs health system?

my boss npcs code:
Spoiler: show
--NPCManager is required for setting basic NPC properties
local npcManager = require("npcManager")

--Create the library table
local sampleNPC = {}
--NPC_ID is dynamic based on the name of the library file
local npcID = NPC_ID

--Defines NPC config for our NPC. You can remove superfluous definitions.
local sampleNPCSettings = {
id = npcID,
--Sprite size
gfxheight = 128,
gfxwidth = 128,
--Hitbox size. Bottom-center-bound to sprite size.
width = 128,
height = 128,
--Sprite offset from hitbox for adjusting hitbox anchor on sprite.
gfxoffsetx = 0,
gfxoffsety = 0,
--Frameloop-related
frames = 1,
framestyle = 0,
framespeed = 8, --# frames between frame change
--Movement speed. Only affects speedX by default.
speed = 1,
--Collision-related
npcblock = false,
npcblocktop = false, --Misnomer, affects whether thrown NPCs bounce off the NPC.
playerblock = false,
playerblocktop = false, --Also handles other NPCs walking atop this NPC.

nohurt=false,
nogravity = false,
noblockcollision = false,
nofireball = false,
noiceball = false,
noyoshi= false,
nowaterphysics = false,
--Various interactions
jumphurt = false, --If true, spiny-like
spinjumpsafe = false, --If true, prevents player hurt when spinjumping
harmlessgrab = false, --Held NPC hurts other NPCs if false
harmlessthrown = false, --Thrown NPC hurts other NPCs if false

grabside=false,
grabtop=false,

--Identity-related flags. Apply various vanilla AI based on the flag:
--iswalker = false,
--isbot = false,
--isvegetable = false,
--isshoe = false,
--isyoshi = false,
--isinteractable = false,
--iscoin = false,
--isvine = false,
--iscollectablegoal = false,
--isflying = false,
--iswaternpc = false,
--isshell = false,

--Emits light if the Darkness feature is active:
--lightradius = 100,
--lightbrightness = 1,
--lightoffsetx = 0,
--lightoffsety = 0,
--lightcolor = Color.white,
health = 10,
--Define custom properties below
}

--Applies NPC settings
npcManager.setNpcSettings(sampleNPCSettings)

--Register the vulnerable harm types for this NPC. The first table defines the harm types the NPC should be affected by, while the second maps an effect to each, if desired.
npcManager.registerHarmTypes(npcID,
{
HARM_TYPE_NPC,
HARM_TYPE_HELD,
HARM_TYPE_PROJECTILE_USED,
HARM_TYPE_SWORD
},
{
[HARM_TYPE_NPC]=10,
[HARM_TYPE_HELD]=10,
[HARM_TYPE_PROJECTILE_USED]=10,
[HARM_TYPE_SWORD]=10
}
);

--Custom local definitions below


--Register events
function sampleNPC.onInitAPI()
npcManager.registerEvent(npcID, sampleNPC, "onTickNPC")
--npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
--npcManager.registerEvent(npcID, sampleNPC, "onNPCHarm")
--npcManager.registerEvent(npcID, sampleNPC, "onDrawNPC")
--npcManager.registerEvent(npcID, 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.despawnTimer <= 0 then
--Reset our properties, if necessary
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
end

--Execute main AI. This template just jumps when it touches the ground.
v.ai5=10
end
--Gotta return the library table!
return sampleNPC
my lunalua code
Spoiler: show
function onNPCKill(eventObj,killedNPC,killReason,culpritOrNil )
if killedNPC.id==751 and killedNPC.ai5>1 then
killedNPC.ai5=killedNPC.ai5-1

end
end
I thought this code would make a health system and idk why it isnt?

wyldstrykr
Buster Beetle
Buster Beetle
Posts: 84
Joined: Mon May 31, 2021 11:20 pm

Re: lunalua warning every tick causing code to not work?

Postby wyldstrykr » Thu Sep 02, 2021 8:03 pm

Code: Select all

function onNPCKill(eventObj,killedNPC,killReason,culpritOrNil )
if killedNPC.id==751 and killedNPC.ai5>1 then
killedNPC.ai5=killedNPC.ai5-1
eventObj.cancelled=true
end
end
forgot to add the cancelled part. it will still give you points tho. idk.
EDIT: i didnt forget it, you did.

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

Re: lunalua warning every tick causing code to not work?

Postby Emral » Fri Sep 03, 2021 1:25 am

onNPCHarm runs before the score calculation, onNPCKill runs after the score calculation. You are seeing the score increase because you are using onNPCKill. You might also need to implement invulnerability frame periods after getting hit, or kill the projectile upon dealing damage.

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Fri Sep 03, 2021 1:44 am

Could I also please get the code for the invulnerability frames and the projectile kill thing. Will be useful because I don’t know much about lunalua then I can also copy functioning code and don’t get an error every time. :)

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

Re: lunalua warning every tick causing code to not work?

Postby Emral » Fri Sep 03, 2021 4:23 am

KoolGamer32 wrote:
Fri Sep 03, 2021 1:44 am
Could I also please get the code for the invulnerability frames and the projectile kill thing. Will be useful because I don’t know much about lunalua then I can also copy functioning code and don’t get an error every time. :)
If this is about the result rather than learning to write code, you can just use lighthitpoint. viewtopic.php?f=101&t=24443

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Fri Sep 03, 2021 1:05 pm

I am trying to use lighthitpoints.lua and I cant figure out how I am supposed to customize it. I also dont know if I am supposed to put the code from lighthitpoints.lua in luna.lua or leave it in lighthitpoints.lua. Where do I put the stuff for customization and how do I add multiple enemies in the customization.

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

Re: lunalua warning every tick causing code to not work?

Postby Emral » Sat Sep 04, 2021 3:53 am

lighthitpoint is a library
https://docs.codehaus.moe/#/concepts/libraries

Libraries are loaded into a variable via the require() function.
The lighthitpoint thread explains how to customize it:

Code: Select all

Customization options:
lhp.iFrames: Unified amount of invulnerability frames across all IDs (25 by default)
lhp.setHarmtypeDamage(Harmtype, value): Sets damage multiplier for specific harm types.
lhp.setHarmtypeSound(Harmtype, value): Sets sound effect for harm types. By default, onNPCHarm cancelling also cancels the SFX, so this adds that back in.
lhp.setNPCDamage(NPCID, value): Sets damage which a specific NPC does when it's determined to be the NPC causing the damage. Stuff like fireballs, hammers, etc...
lhp.setHP(NPCID, value): Sets HP for a specific ID of NPC.
so after doing "local lhp = require("lighthitpoint")" you have access to the above functions and the lhp.iFrames variable from your code.

You can then set lhp.setHP(1, 10) to give a goomba 10 hp. But that alone isn't enough because the code doesn't yet know what can inflict damage.
You can set lhp.setNPCDamage(13, 1) to define the damage dealt by a fireball (npc 13) to be universally 1.
You can set lhp.setHarmtypeDamage(HARM_TYPE_JUMP, 3) to define the damage dealt by jumping to be universally 3. https://docs.codehaus.moe/#/constants/enemy-harm-types
And this ID column on the left may be helpful for setHarmtypeSound: https://docs.codehaus.moe/#/concepts/sfx-list

To add more customization just use more of these lines but for different IDs and harmtypes.

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Sat Sep 04, 2021 10:24 am

where exactly do I put this code

lhp..iFrames = 25

lhp.setNPCDamage (171, 1)

lhp.setHP (751, 10)

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setHarmtypeSound ((HARM_TYPE_PROJECTILEUSED, 1)

every time i run the game I get a different error depending on where this code is in lighthitpoints.lua

MrDoubleA
Ripper II
Ripper II
Posts: 394
Joined: Mon Aug 20, 2018 7:02 am
Flair: How much munchers?

Re: lunalua warning every tick causing code to not work?

Postby MrDoubleA » Sat Sep 04, 2021 11:58 am

KoolGamer32 wrote:
Sat Sep 04, 2021 10:24 am
where exactly do I put this code

lhp..iFrames = 25

lhp.setNPCDamage (171, 1)

lhp.setHP (751, 10)

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setHarmtypeSound ((HARM_TYPE_PROJECTILEUSED, 1)

every time i run the game I get a different error depending on where this code is in lighthitpoints.lua
It shouldn't be in lighthitpoints.lua, rather it should be in your luna.lua file. Additionally, you have "..iFrames" rather than just ".iFrames".

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Sat Sep 04, 2021 5:37 pm

thanks for that part but I wondering where i put this code
Spoiler: show
lhp.iFrames = 25

lhp.setNPCDamage (171, 1)

lhp.setHP (751, 10)

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setHarmtypeSound (HARM_TYPE_PROJECTILEUSED, 1)
in this code
Spoiler: show
local lhp = require("lighthitpoint")

local lhp = {}

local npcdamage = {

}

local npchp = {

}

local harmtypesound = {HARM_TYPE_PROJECTILEUSED, 1

}

local harmtypedamage = {HARM_TYPE_PROJECTILEUSED, 1

}

function lhp.setHarmtypeDamage(HARMTYPE, value)
harmtypedamage[HARMTYPE] = value
end

function lhp.setHarmtypeSound(HARMTYPE, value)
harmtypesound[HARMTYPE] = value
end

function lhp.setNPCDamage(NPCID, value)
npcdamage[NPCID] = value
end

function lhp.setHP(NPCID, value)
npchp[NPCID] = value
end

function lhp.onInitAPI()
registerEvent(lhp, "onNPCHarm")
registerEvent(lhp, "onCameraDraw")
end

function lhp.onNPCHarm(e, v, r, c)
if (r ~= HARM_TYPE_OFFSCREEN) and npchp[v.id] then
if v.data.lhphp == nil then
v.data.lhphp = npchp[v.id]
end
local damage = 1
damage = damage * (harmtypedamage[r] or 1)
if c and c.__type == "NPC" then
damage = damage * (npcdamage[c.id] or 1)
end

if v.data.iFrames and v.data.iFrames > lunatime.tick() - lhp.iFrames then
damage = 0
else
v.data.iFrames = lunatime.tick()
end
v.data.lhphp = math.max(v.data.lhphp - damage, 0)
if damage ~= 0 and v.data.lhphp > 0 then
if harmtypesound[r] then
SFX.play(harmtypesound[r])
end
end
if v.data.lhphp > 0 then
e.cancelled = true
end
end
end

local function drawHPBar(v)
local x = v.x + 0.5 * v.width
local y = v.y - 10

local width = 10 + 5 * math.ceil((npchp[v.id] or 1) * 0.2)

Graphics.drawBox{
x = x - 0.5 * width,
width = width,
y = y - 2,
height = 4,
color = Color.grey,
sceneCoords = true,
priority = -45
}

Graphics.drawBox{
x = x - 0.5 * width,
width = width * (v.data.lhphp / (npchp[v.id] or 1)),
y = y - 2,
height = 4,
color = Color.yellow,
sceneCoords = true,
priority = -45
}
end

function lhp.onCameraDraw()
for k,v in ipairs(NPC.getIntersecting(camera.x, camera.y, camera.x + camera.width, camera.y + camera.height)) do
if NPC.HITTABLE_MAP[v.id] and v:mem(0x12A, FIELD_WORD) > 0 and not v.friendly then
if v.data.lhphp then
drawHPBar(v)
end
end
end
end

return lhp
specifically what line do i need to put this code in for it to function properly because it keeps saying that the top line of code i insert below lhp.iFrames is nil (whatever arrangement i put the code in such as
lhp.setNPCDamage (171, 1)

lhp.setHP (751, 10)

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setHarmtypeSound (HARM_TYPE_PROJECTILEUSED, 1)

or

lhp.setHP (751, 10)

lhp.setNPCDamage (171, 1)

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setHarmtypeSound (HARM_TYPE_PROJECTILEUSED, 1)

or

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setNPCDamage (171, 1)

lhp.setHP (751, 10)

lhp.setHarmtypeSound (HARM_TYPE_PROJECTILEUSED, 1))

point is whatever arrangment the code is the top line (BOLDED) is nil in the error message

so I would like to know the exact line in this code
Spoiler: show
local lhp = require("lighthitpoint")

local lhp = {}

local npcdamage = {

}

local npchp = {

}

local harmtypesound = {HARM_TYPE_PROJECTILEUSED, 1

}

local harmtypedamage = {HARM_TYPE_PROJECTILEUSED, 1

}

function lhp.setHarmtypeDamage(HARMTYPE, value)
harmtypedamage[HARMTYPE] = value
end

function lhp.setHarmtypeSound(HARMTYPE, value)
harmtypesound[HARMTYPE] = value
end

function lhp.setNPCDamage(NPCID, value)
npcdamage[NPCID] = value
end

function lhp.setHP(NPCID, value)
npchp[NPCID] = value
end

function lhp.onInitAPI()
registerEvent(lhp, "onNPCHarm")
registerEvent(lhp, "onCameraDraw")
end

function lhp.onNPCHarm(e, v, r, c)
if (r ~= HARM_TYPE_OFFSCREEN) and npchp[v.id] then
if v.data.lhphp == nil then
v.data.lhphp = npchp[v.id]
end
local damage = 1
damage = damage * (harmtypedamage[r] or 1)
if c and c.__type == "NPC" then
damage = damage * (npcdamage[c.id] or 1)
end

if v.data.iFrames and v.data.iFrames > lunatime.tick() - lhp.iFrames then
damage = 0
else
v.data.iFrames = lunatime.tick()
end
v.data.lhphp = math.max(v.data.lhphp - damage, 0)
if damage ~= 0 and v.data.lhphp > 0 then
if harmtypesound[r] then
SFX.play(harmtypesound[r])
end
end
if v.data.lhphp > 0 then
e.cancelled = true
end
end
end

local function drawHPBar(v)
local x = v.x + 0.5 * v.width
local y = v.y - 10

local width = 10 + 5 * math.ceil((npchp[v.id] or 1) * 0.2)

Graphics.drawBox{
x = x - 0.5 * width,
width = width,
y = y - 2,
height = 4,
color = Color.grey,
sceneCoords = true,
priority = -45
}

Graphics.drawBox{
x = x - 0.5 * width,
width = width * (v.data.lhphp / (npchp[v.id] or 1)),
y = y - 2,
height = 4,
color = Color.yellow,
sceneCoords = true,
priority = -45
}
end

function lhp.onCameraDraw()
for k,v in ipairs(NPC.getIntersecting(camera.x, camera.y, camera.x + camera.width, camera.y + camera.height)) do
if NPC.HITTABLE_MAP[v.id] and v:mem(0x12A, FIELD_WORD) > 0 and not v.friendly then
if v.data.lhphp then
drawHPBar(v)
end
end
end
end

return lhp
i put the aformentioned 4 lines of code
Spoiler: show
lhp.setNPCDamage (171, 1)

lhp.setHP (751, 10)

lhp.setHarmtypeDamage (HARM_TYPE_PROJECTILEUSED, 1)

lhp.setHarmtypeSound (HARM_TYPE_PROJECTILEUSED, 1)
for it to function properly because it seems like even if I have code that is supposed to work it still errors because it is in the wrong place or I customized it the wrong way

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

Re: lunalua warning every tick causing code to not work?

Postby Emral » Sun Sep 05, 2021 5:13 am

Let's start from the beginning cause there's a lot of misconceptions here.

1) The lighthitpoint code as it is on pastebin goes into its own file, lighthitpoint.lua, in your level folder next to luna.lua. It is not to be tampered with
2) You can write your own code in luna.lua
3) local lhp = require("lighthitpoint") will load the code from the lighthitpoint.lua file into the lhp variable
4) Once you have the lhp variable in your luna.lua, you can call lhp.setNPCDamage and everything else you need on it.

KoolGamer32
Bob-Omb
Bob-Omb
Posts: 24
Joined: Tue Jan 19, 2021 11:35 am
Flair: Life is cool and stuff so yeah!
Pronouns: he/him
Contact:

Re: lunalua warning every tick causing code to not work?

Postby KoolGamer32 » Sun Sep 05, 2021 11:30 am

thanks so very much for all the help everyone the problem has been solved


Return to “LunaLua Help”

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari