I made a variable called “turn” that I increment through my game. It works fine until I go to the second level (not a separate collection, i just change the information in some tables) and reset the variable to 1 (which is what it started as) and it refuses to increment. The weird part is that it increments properly BEFORE the message is called but when it gets to the message it’s at its default value.
relevant code:
function update(self, dt)
................
num = num + .01 --
if num > 1.01 then --also while I'm here, this didn't work with just 1 but it worked with 1.01 Any reason why?
.......
print("Turn is", turn) --prints 1
turn = turn + 1
print("Turn after is", turn) --prints 2
num = .01
.............
if inputs > 0 then
msg.post("#", "Execute")
else
level = level + 1
end
if level == 2 then
battle2()
end
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("Execute") then
for i, t in pairs(tables)do --i have a table of tables that this thing is iterating through
print("Turn in message is", turn) --prints 1 even though before the message was sent in update, it was 2.
--before the variable turn was set back to 1 in battle2() it would show the correct result
if t[turn] ~= nil then
fight(t,i) --Sends table
end
end
..................
end
end
function fight(enemy, i)
print("Turn in fight is", turn) --prints 1 even though it's supposed to be 2
.......................................
end
function battle2()
.............. --bunch of table changing stuff
turn = 1
............
end
I would put in the entire script but there’s a crapton of if and elseif statements in my update() that I’m not entirely proud of (again, I will refactor it but getting this project in a working condition for school comes first) but if you need me to post the rest I can.