Page 1 of 1

Star Battle Code (Help needed)

Posted: Sat Aug 12, 2017 4:50 pm
by Gaming-Dojo
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.

Re: Star Battle Code (Help needed)

Posted: Sat Aug 12, 2017 5:06 pm
by The0x539
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.

Re: Star Battle Code (Help needed)

Posted: Sat Aug 12, 2017 9:21 pm
by PixelPest
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)

Re: Star Battle Code (Help needed)

Posted: Sun Aug 13, 2017 12:54 am
by The0x539
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

Re: Star Battle Code (Help needed)

Posted: Sun Aug 13, 2017 7:56 am
by Gaming-Dojo
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.

Re: Star Battle Code (Help needed)

Posted: Sun Aug 13, 2017 8:47 am
by The0x539
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

Re: Star Battle Code (Help needed)

Posted: Sun Aug 13, 2017 8:48 am
by PixelPest
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

Re: Star Battle Code (Help needed)

Posted: Sun Aug 13, 2017 9:07 am
by Gaming-Dojo
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.

Re: Star Battle Code (Help needed)

Posted: Sun Aug 13, 2017 9:49 pm
by PixelPest
So you don't reload the level in-between rounds?

Re: Star Battle Code (Help needed)

Posted: Mon Aug 14, 2017 7:02 am
by Gaming-Dojo
PixelPest wrote:So you don't reload the level in-between rounds?
Exactly.

Re: Star Battle Code (Help needed)

Posted: Mon Aug 14, 2017 7:09 am
by PixelPest
yoshiegg wrote:
PixelPest wrote:So you don't reload the level in-between rounds?
Exactly.
What do you do instead?

Re: Star Battle Code (Help needed)

Posted: Mon Aug 14, 2017 7:58 am
by Gaming-Dojo
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.

Re: Star Battle Code (Help needed)

Posted: Mon Aug 14, 2017 8:44 am
by The0x539
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.