Virtual hearts system

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?
Gaming-Dojo
Tweeter
Tweeter
Posts: 144
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Virtual hearts system

Postby Gaming-Dojo » Mon Jun 12, 2017 4:46 pm

As I found a code for additional hearts over at PGE forums I decided to modify if to replace the text by pictures as well as adding the heart container and heart piece system of LoZ.
However, the following code throws a no matcing overload error even though I've checked my naming carefully.As I don't know when else such an error can appear I appreciated your help now.For better following which effect I want each conditional to have I've also posted the commentary I made (marked by these signs--):

Code: Select all

local hearts = {}

local vhearts = 3
local heartLimit = 3
local heartPieces = 0
local limit3 = Graphics.loadImage(Misc.resolveFile("hearts/limit3"))
local limit4 = Graphics.loadImage(Misc.resolveFile("hearts/limit4"))
local limit5 = Graphics.loadImage(Misc.resolveFile("hearts/limit5"))
local limit6 = Graphics.loadImage(Misc.resolveFile("hearts/limit6"))
local limit7 = Graphics.loadImage(Misc.resolveFile("hearts/limit7"))
local limit8 = Graphics.loadImage(Misc.resolveFile("hearts/limit8"))
local limit9 = Graphics.loadImage(Misc.resolveFile("hearts/limit9"))
local limit10 = Graphics.loadImage(Misc.resolveFile("hearts/limit10"))
local heart = Graphics.loadImage(Misc.resolveFile("hearts/heart.png"))
local piece1 = Graphics.loadImage(Misc.resolveFile("hearts/piece1.png"))
local piece2 = Graphics.loadImage(Misc.resolveFile("hearts/piece2.png"))
local piece3 = Graphics.loadImage(Misc.resolveFile("hearts/piece3.png"))
local piece4 = Graphics.loadImage(Misc.resolveFile("hearts/piece4.png"))
local piecebg = Graphics.loadImage(Misc.resolveFile("hearts/piecebg.png"))

function hearts.onInitAPI()
registerEvent(hearts, "onTick", "onTick", true)
registerEvent(hearts, "onHUDDraw", "onHUDDraw",true)
end
function hearts.onTick()
--Detect extra hearts. It'll add an infinite number of hearts.
      if ((player:mem(0x16, FIELD_WORD)) > 2) then
         vhearts = vhearts + 1;
         player:mem(0x16, FIELD_WORD, 2);
      end
--You've defined a number of hearts for some reason, no?
      if (vhearts > heartLimit) then
         vhearts = heartLimit;
      end
--If damaged, take extra hearts from virtual hearts
      if ((player:mem(0x16, FIELD_WORD)) == 1) and (vhearts > 1) then
         vhearts = vhearts - 1;
      end
--This keeps the player alive
      if (player.powerup == PLAYER_SMALL) and (vhearts > 2)
	  or (player.powerup == PLAYER_SMALL) and (vhearts == 2) then
          player.powerup = PLAYER_BIG;
      end
for k,v in pairs(NPC.get(250,-1)) do  --functions like a heart container in LoZ: extends the maximal hearts by 1
    heartLimit = heartLimit + 1
	vhearts = heartLimit
end
for k,v in pairs(NPC.get(249,-1)) do
    heartPieces = heartPieces + 1
	vhearts = vhearts       --avoids that heart pieces heal you
end
    if heartPieces == 4 then
	   heartLimit = heartLimit + 1
	   heartPieces = 0
	   vhearts = heartLimit  -- turns 4 heart pieces into an extra heart
	end
    if heartLimit > 10 then
	   heartLimit = 10  -- to limit maximal hearts even if they'd be extended by a container
	end
function hearts.onHUDDraw()  --this draws everything.See comments below for further details
if vhearts == 1 then
Graphics.drawImageWP(heart,36,38,5) --draws full red hearts to indicate current hp
end
if vhearts == 2 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
end
if vhearts == 3 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
end
if vhearts == 4 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
end
if vhearts == 5 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
end
if vhearts == 6 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
end
if vhearts == 7 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
end
if vhearts == 8 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
Graphics.drawImageWP(heart,36,74,5)
end
if vhearts == 9 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
Graphics.drawImageWP(heart,36,74,5)
Graphics.drawImageWP(heart,72,74,5)
end
if vhearts == 10 then
Graphics.drawImageWP(heart,36,38,5)
Graphics.drawImageWP(heart,72,38,5)
Graphics.drawImageWP(heart,108,38,5)
Graphics.drawImageWP(heart,144,38,5)
Graphics.drawImageWP(heart,180,38,5)
Graphics.drawImageWP(heart,216,38,5)
Graphics.drawImageWP(heart,252,38,5)
Graphics.drawImageWP(heart,36,74,5)
Graphics.drawImageWP(heart,72,74,5)
Graphics.drawImageWP(heart,108,74,5)   --draws full red hearts to indicate current hp
end
if heartLimit == 3 then
Graphics.drawImageWP(limit3,36,38,5)   --draws empty grey hearts to indicate the current maximum
end
if heartLimit == 4 then
Graphics.drawImageWP(limit4,36,38,5)
end
if heartLimit == 5 then
Graphics.drawImageWP(limit5,36,38,5)
end
if heartLimit == 6 then
Graphics.drawImageWP(limit6,36,38,5)
end
if heartLimit == 7 then
Graphics.drawImageWP(limit7,36,38,5)
end
if heartLimit == 8 then
Graphics.drawImageWP(limit8,36,38,5)
end
if heartLimit == 9 then
Graphics.drawImageWP(limit9,36,38,5)     
end
if heartLimit == 10 then
Graphics.drawImageWP(limit10,36,38,5)    --draws empty grey hearts to indicate the current maximum
end
if heartPieces == 0 or
   heartPieces > 0 then
Graphics.drawImageWP(piecebg,262,38,4)   --draws the player's current heart pieces and their background
end
if heartPieces == 1 then
Graphics.drawImageWP(piece1,262,38,5)
end
if heartPieces == 2 then
Graphics.drawImageWP(piece1,262,38,5)
Graphics.drawImageWP(piece2,279,38,5)
end
if heartPieces == 3 then
Graphics.drawImageWP(piece1,262,38,5)
Graphics.drawImageWP(piece2,279,38,5)
Graphics.drawImageWP(piece3,262,55,5)
end
if heartPieces == 4 then
Graphics.drawImageWP(piece1,262,38,5)
Graphics.drawImageWP(piece2,279,38,5)
Graphics.drawImageWP(piece3,262,55,5)
Graphics.drawImageWP(piece4,279,55,5)     -- draws the heart pieces the player currently has
end
end

end

return hearts

The0x539
Eerie
Eerie
Posts: 751
Joined: Fri Jan 22, 2016 8:02 pm

Re: Virtual hearts system

Postby The0x539 » Mon Jun 12, 2017 4:50 pm

The reason for the error is that you're missing the image file's extension.

PixelPest
Link
Link
Posts: 7111
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Contact:

Re: Virtual hearts system

Postby PixelPest » Mon Jun 12, 2017 4:57 pm

Also I'd recommend storing things like that in tables since it's much easier and you have a limited number of variables afaik. I'd suggest trying to implement something like this:

Code: Select all

local pieces = {};

for i = 1, 4 do
    pieces[i] = Graphics.loadImage(Misc.resolveFile("hearts/piece"..i..".png");
end 

Gaming-Dojo
Tweeter
Tweeter
Posts: 144
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Re: Virtual hearts system

Postby Gaming-Dojo » Thu Jun 15, 2017 2:47 pm

So now I tried to store my graphics in tables and produced the following code which throws in line 8 a '}' expected near 'Graphics' error even though I put {} at beginning and end of the list.

Code: Select all

local hearts = {}

local vhearts = 3
local heartLimit = 3
hearts.m_hp = 1
hearts.pieces = 0
local limit = {Graphics.loadImage(Misc.resolveFile("hearts/limit3.png")),Graphics.loadImage(Misc.resolveFile("hearts/limit4.png")),Graphics.loadImage(Misc.resolveFile("hearts/limit5.png")),Graphics.loadImage(Misc.resolveFile("hearts/limit6.png")),Graphics.loadImage(Misc.resolveFile("hearts/limit7.png")),Graphics.loadImage(Misc.resolveFile("hearts/limit8.png"))Graphics.loadImage(Misc.resolveFile("hearts/limit9.png")),Graphics.loadImage(Misc.resolveFile("hearts/limit10.png"))}
local heart = Graphics.loadImage(Misc.resolveFile("hearts/heart.png"))
local piece = {Graphics.loadImage(Misc.resolveFile("hearts/piece1.png")),Graphics.loadImage(Misc.resolveFile("hearts/piece2.png")),Graphics.loadImage(Misc.resolveFile("hearts/piece3.png")),Graphics.loadImage(Misc.resolveFile("hearts/piece4.png"))}
local piecebg = Graphics.loadImage(Misc.resolveFile("hearts/piecebg.png"))

function hearts.onInitAPI()
registerEvent(hearts, "onNPCKill", "onNPCKill", false)
registerEvent(hearts, "onHUDDraw", "onHUDDraw",true)
registerEvent(hearts, "onTick", "onTick", false)
registerEvent(hearts, "onStart", "onStart", true)
end
function harts.onStart()
Graphics.activateHud(false)
end
function hearts.onNPCKill(killObj, killedNPC, killReason)
So does anyone of you remark the reason for the error and might help me.Btw,that's not the full code but I thought that this extract would be enough to see my method and find the error.

The0x539
Eerie
Eerie
Posts: 751
Joined: Fri Jan 22, 2016 8:02 pm

Re: Virtual hearts system

Postby The0x539 » Thu Jun 15, 2017 3:36 pm

yoshiegg wrote:So now I tried to store my graphics in tables and produced the following code which throws in line 8 a '}' expected near 'Graphics' error even though I put {} at beginning and end of the list.
You missed a comma after limit8. But, more importantly, you're kinda missing the point of doing this, and the hard-to-find typo goes to show why it's important to keep your code clean.

Code: Select all

local limit = {}
for i=3,10 do
	limit[i] = Graphics.loadImage(Misc.resolveFile("hearts/limit"..i..".png"))
end

Gaming-Dojo
Tweeter
Tweeter
Posts: 144
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Re: Virtual hearts system

Postby Gaming-Dojo » Mon Jun 19, 2017 1:38 pm

I used this method bc I am more familiar with it while i didn't know how to influence the variable that determins, which pic is called as well as how to structure my folder with the pics.
But by your comment, I got motivated to try pixels simpler method.So that's what's come out.However, this code throws a ( expected near if error in line 43 of my full code which i marked with a pink 43 in that extract.So can anyone say me when I need () in an if-conditional an when not or what other circumstance causes my error?:

Code: Select all

local hearts = {}

local vhearts = 3
local heart = Graphics.loadImage(Misc.resolveFile("hearts/heart.png"))
i = 0
local pieces = {};

for i = 0,4 do
    pieces[i] = Graphics.loadImage(Misc.resolveFile("hearts/piece"..i..".png"));
end

local limit = {}
for n = 3,10 do
   limit[n] = Graphics.loadImage(Misc.resolveFile("hearts/limit"..n..".png"))
end

...

	if killedNPC.id == 250 then  --functions like a heart container in LoZ: extends the maximal hearts by 1
	n = n + 1
	vhearts = n
    end
    if killedNPC.id == 249 then
    i = i + 1
	vhearts = vhearts       --avoids that heart pieces heal you
   end
end
function hearts.onTick
--You've defined a number of hearts for some reason, no?
      if vhearts > n then
43

Code: Select all

vhearts = n
      end
--If damaged, take extra hearts from virtual hearts
      if ((player:mem(0x16, FIELD_WORD)) == 1) and (vhearts > 1) then
         vhearts = vhearts - 1;
      end
--This keeps the player alive
      if (player.powerup == PLAYER_SMALL) and (vhearts > 1)
          player.powerup = PLAYER_BIG;
      end
    if i == 4 then
	   n = n + 1
	   i = 0
	   vhearts = n  -- turns 4 heart pieces into an extra heart
	end
    if n > 10 then
	   n = 10  -- to limit maximal hearts even if they'd be extended by a container.(to avoid confusion when hud isn't drawn due to lacking graphics).
	end
end

Graphics.drawImageWP(limit,36,38,4)   --draws empty grey hearts to indicate the current maximum

Graphics.drawImageWP(pieces,292,38,5)
end
end

return hearts
Btw, I haven't tested this yet due to the line 43 error so wait with other error reports than about suggestions of how to fix that error until I was able to as I want to check the code for errors myself first to get better at error spotting and so finally at coding.

The0x539
Eerie
Eerie
Posts: 751
Joined: Fri Jan 22, 2016 8:02 pm

Re: Virtual hearts system

Postby The0x539 » Mon Jun 19, 2017 2:47 pm

function hearts.onTick()

Gaming-Dojo
Tweeter
Tweeter
Posts: 144
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Re: Virtual hearts system

Postby Gaming-Dojo » Tue Jun 20, 2017 3:13 am

Now I've worked on my code a bit and fixed several errors so I am imo close to get it to work soon.However, there's still an ")" expected near "i" error in line 11 (the second in the extract below) reported and I can't explain it as afaik, Graphics.loadImage doesn't need more than 2 brackets so I would be thankful for help.:

Code: Select all

for i = 0,4 do
    pieces[i] = Graphics.loadImage(Misc.resolveFile("hearts/piece/"i".png"));
end

local limit = {}
for n = 3,10 do
   limit[n] = Graphics.loadImage(Misc.resolveFile("hearts/limit/"n".png"));
end

The0x539
Eerie
Eerie
Posts: 751
Joined: Fri Jan 22, 2016 8:02 pm

Re: Virtual hearts system

Postby The0x539 » Tue Jun 20, 2017 6:42 pm

Lua doesn't know how to handle the thing you gave it for a filename, you need to explicitly concatenate, like this:

Code: Select all

Misc.resolveFile("hearts/piece/"..i..".png")

Gaming-Dojo
Tweeter
Tweeter
Posts: 144
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Re: Virtual hearts system

Postby Gaming-Dojo » Wed Jun 21, 2017 11:19 am

The0x539 wrote:Lua doesn't know how to handle the thing you gave it for a filename, you need to explicitly concatenate, like this:

Code: Select all

Misc.resolveFile("hearts/piece/"..i..".png")
All right.But where do I have to place the image resource and how do I have to name it then?

The0x539
Eerie
Eerie
Posts: 751
Joined: Fri Jan 22, 2016 8:02 pm

Re: Virtual hearts system

Postby The0x539 » Wed Jun 21, 2017 11:54 am

Literally the same code as before but with a pair of full stops in between the strings and the variables.

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

Re: Virtual hearts system

Postby Hoeloe » Wed Jun 21, 2017 4:33 pm

The0x539 wrote:Literally the same code as before but with a pair of full stops in between the strings and the variables.
To clarify, this:

Code: Select all

"somestring"variable"morestring"
is not valid. You need to explicitly tell Lua that you want to add those strings together into a longer string. For that, you need the concatenate operator, denoted by ..

Code: Select all

a..b
means "add b to the end of a to make a longer string", so if your first example was:

Code: Select all

"somestring"..variable.."morestring"
It would mean "create a string that looks like: "somestring<var>morestring" So, if variable contained, for example, the number 6, the concatenation would produce: "somestring6morestring".

Gaming-Dojo
Tweeter
Tweeter
Posts: 144
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Re: Virtual hearts system

Postby Gaming-Dojo » Wed Jun 21, 2017 8:04 pm

and if I haven't put anything after the value of my variable? Can or do I have to leave out the second... then?
I personally believe yes but it's nighttime for me now so I won't try it on my Computer and appreciated your help. If I find it out by myself tomorrow, I'll let you know.
However, the point I actually told about was referring to the folder structure which has probably become clear for me in the meantime and by your posts.


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari