Star Battle Code (Help needed)

This is the place for discussion and support for LunaLua and related modifications and libraries.
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?
yoshiegg
Hoopster
Hoopster
Posts: 122
Joined: Mon Jan 25, 2016 9:20 am

Star Battle Code (Help needed)

Postby yoshiegg » Sat Aug 12, 2017 4:50 pm

Hello guys.
While trying to write a code for a NSMB-like star-battle, I got a "No such operator defined"-error which I suspect to be caused by my player variables and their use in my code.However, I've no idea how to differ in lua, which player has collected a certain object.According to that issue I also have the question, if alternate powerup-systemss (like PixelPests altpsystem.lua) work in 2 player/battle mode for both players.But now back to the code with which i need your help, here it is :

Code: Select all

local star_battle = {}

function star_battle.onInitAPI()
registerEvent(star_battle, "onNPCKill", "onNPCKill", false)
registerEvent(star_battle, "onTick", "onTick", false)
registerEvent(star_battle, "onStart", "onStart", false)
registerEvent(star_battle, "onHUDDraw", "onHUDDraw",true)
end

local player1 = player()
local player2 = player(2)
local star_count1 = 0
local star_count2 = 0
local player1_wins = 0
local player2_wins = 0
local star = Graphics.loadImage(Misc.resolveFile("star_battle/star.png"))

function star_battle.onStart()
Graphics.activateHud(false)
end

function star_battle.onTick()
local isHurt = player:mem(0x140, FIELD_WORD)
if player == player1 then
mem(0x00B2C5AC,FIELD_FLOAT,99)
end
if player == player2 then
mem(0x00B2C5AC,FIELD_FLOAT,99)
end

if (isHurt == 150) then
if player == player1 then 
star_count1 = star_count1 - 1
else
star_count2 = star_count2 - 1
end
end

if star_count1 == 5 then
player1_wins = player1_wins + 1
end
if star_count2 == 5 then
player2_wins = player2_wins + 1
end
end

function star_battle.onNPCKill(killObj, killedNPC, killReason)
if killedNPC.id == 196 then
if player == player1 then 
star_count1 = star_count1 + 1
else
star_count2 = star_count2 + 1
end
end
end
So I hope you can help me out. Greetings
yoshiegg

PS: Don't complain about my code not ending the level, I'll add that later.

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

Re: Star Battle Code (Help needed)

Postby The0x539 » Sat Aug 12, 2017 5:06 pm

player1 isn't a constant, and player is always the first player. If you want to run code for multiple players, loop through Player.get() (be sure to use ipairs) and check the value of your loop's index variable.

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

Re: Star Battle Code (Help needed)

Postby PixelPest » Sat Aug 12, 2017 9:21 pm

Also at the top of your code when defining your variables, instead of defining player variables loop through them in your code (as The0x539 said) and when you need to make reference to a specific player use player for player 1 and Player(2) for player 2. Also why are you using zeroes and ones for your variables? Use booleans instead (player1_wins = false; at the beginning and then set it to true)

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

Re: Star Battle Code (Help needed)

Postby The0x539 » Sun Aug 13, 2017 12:54 am

PixelPest wrote:Also at the top of your code when defining your variables, instead of defining player variables loop through them in your code (as The0x539 said) and when you need to make reference to a specific player use player for player 1 and Player(2) for player 2. Also why are you using zeroes and ones for your variables? Use booleans instead (player1_wins = false; at the beginning and then set it to true)
player2 is a valid and existing reference to player 2

yoshiegg
Hoopster
Hoopster
Posts: 122
Joined: Mon Jan 25, 2016 9:20 am

Re: Star Battle Code (Help needed)

Postby yoshiegg » Sun Aug 13, 2017 7:56 am

PixelPest wrote:Also at the top of your code when defining your variables, instead of defining player variables loop through them in your code (as The0x539 said) and when you need to make reference to a specific player use player for player 1 and Player(2) for player 2. Also why are you using zeroes and ones for your variables? Use booleans instead (player1_wins = false; at the beginning and then set it to true)
You got the intention of this variable wrong.It is meant to be a counter that indicates how often each player has won.To do that, it increases each time, one of the star counters reaches 5.

Hello.
now I tried to follow your tips and modified my code to this:

Code: Select all

local star_battle = {}
function star_battle.onInitAPI()
registerEvent(star_battle, "onNPCKill", "onNPCKill", false)
registerEvent(star_battle, "onTick", "onTick", false)
registerEvent(star_battle, "onStart", "onStart", false)
registerEvent(star_battle, "onHUDDraw", "onHUDDraw",true)
end

local star_count1 = 0
local star_count2 = 0
local player1_wins = 0
local player2_wins = 0
local star = Graphics.loadImage(Misc.resolveFile("star_battle/star.png"))

function star_battle.onStart()
Graphics.activateHud(false)
end

   function star_battle.onTick()
22   for k,v in ipairs(player.get(player)) do   
   mem(0x00B2C5AC,FIELD_FLOAT,99)
   end
   for k,v in ipairs(player.get(player2)) do
   mem(0x00B2C5AC,FIELD_FLOAT,99)
   end

if player:mem(0x140,FIELD_WORD) == 150 then
star_count1 = star_count1 - 1
end
if player2:mem(0x140,FIELD_WORD) == 150 then
star_count2 = star_count2 - 1
end

if star_count1 == 5 then
player1_wins = player1_wins + 1
star_count1 = 0
star_count2 = 0
end
if star_count2 == 5 then
player2_wins = player2_wins + 1
star_count1 = 0
star_count2 = 0
end
end

function star_battle.onNPCKill(killObj, killedNPC, killReason,killer)
   if killedNPC.id == 196 then
      if killer == player then
         star_count1 = star_count1 + 1
         end
      if killer == player(2) then
         star_count2 = star_count2 + 1
         end
   end
end
However, I get an error message that says: line 22(marked): attempt to load field "get"(a nil value) and don't know why and what I can do about it so please help me again.
Last edited by yoshiegg on Sun Aug 13, 2017 8:40 am, edited 1 time in total.

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

Re: Star Battle Code (Help needed)

Postby The0x539 » Sun Aug 13, 2017 8:47 am

The constant player, representing the first player, has no "get" function. Player, the abstract class, does, and it doesn't accept arguments. I said to loop through "Player.get()" and I meant exactly that.

Code: Select all

for i,p in ipairs(Player.get()) do
    if i == 1 then
        p.x = p.x + 1
    elseif i == 2 then
        p.x = p.x - 1
    end
end

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

Re: Star Battle Code (Help needed)

Postby PixelPest » Sun Aug 13, 2017 8:48 am

Also if it is a counter then all if your data will be lost each time the level is reloaded so you should save the number of wins of each round via the data class

yoshiegg
Hoopster
Hoopster
Posts: 122
Joined: Mon Jan 25, 2016 9:20 am

Re: Star Battle Code (Help needed)

Postby yoshiegg » Sun Aug 13, 2017 9:07 am

imo, that's no problem as you won't end the level unless you stop playing and as I'll play with different people so the wins Don't need to be kept when reloading.

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

Re: Star Battle Code (Help needed)

Postby PixelPest » Sun Aug 13, 2017 9:49 pm

So you don't reload the level in-between rounds?

yoshiegg
Hoopster
Hoopster
Posts: 122
Joined: Mon Jan 25, 2016 9:20 am

Re: Star Battle Code (Help needed)

Postby yoshiegg » Mon Aug 14, 2017 7:02 am

PixelPest wrote:So you don't reload the level in-between rounds?
Exactly.

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

Re: Star Battle Code (Help needed)

Postby PixelPest » Mon Aug 14, 2017 7:09 am

yoshiegg wrote:
PixelPest wrote:So you don't reload the level in-between rounds?
Exactly.
What do you do instead?

yoshiegg
Hoopster
Hoopster
Posts: 122
Joined: Mon Jan 25, 2016 9:20 am

Re: Star Battle Code (Help needed)

Postby yoshiegg » Mon Aug 14, 2017 7:58 am

Now I don't get any errors but whoever collects the star, only one counter increases.The functiononNPCKill-part of my code looks like this now::

Code: Select all

function star_battle.onNPCKill(killObj, killedNPC, killReason,killer)
   if killedNPC.id == 196 then 
for i,p in ipairs(Player.get()) do
if i == 1 then
         star_count1 = star_count1 + 1
         end
		 if i == 2 then
          star_count2 = star_count2 + 1
         end
   end
end
end
Please help me as I don't know how to make a working counter for each player which is the most important thing to make it suitable for battle.
PixelPest wrote:
yoshiegg wrote:
PixelPest wrote:So you don't reload the level in-between rounds?
Exactly.
What do you do instead?
I just stay in the level until I want to stop playing as the level isn't supposed to end with lives set to 99 every tick.

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

Re: Star Battle Code (Help needed)

Postby The0x539 » Mon Aug 14, 2017 8:44 am

yoshiegg wrote:Now I don't get any errors but whoever collects the star, only one counter increases.The functiononNPCKill-part of my code looks like this now::

Code: Select all

function star_battle.onNPCKill(killObj, killedNPC, killReason,killer)
   if killedNPC.id == 196 then 
for i,p in ipairs(Player.get()) do
if i == 1 then
         star_count1 = star_count1 + 1
         end
		 if i == 2 then
          star_count2 = star_count2 + 1
         end
   end
end
end
Please help me as I don't know how to make a working counter for each player which is the most important thing to make it suitable for battle.
The onNPCKill callback does not, to my knowledge or according to official documentation, have a fourth argument that specifies the player who kills it. The best way to go about determining which player collected it might be to determine which player is closest to killedNPC. Player.getIntersecting is undocumented but might be quite helpful for this, and works like any other getIntersecting function.


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari