Page 1 of 1
Unable to change variable with a function
Posted: Tue Jan 03, 2017 10:54 pm
by UniPopCorn
Hello everyone.
So I''m currently learning programming and to make things more interesting I decided to code a game and learn in process.
So far it was great, but I ran into a problem I can't solve.
The thing is I implemented a simple gamestate system (basically a string gamestate which changes to "menu" or "playing" and "if" statements to check which block of code should run), and buttons using a table with tables (I don't know how to call it properly, sorry). There are "start" button which changes the gamestate to "playing" (so basically starts the game), the options one which currently does nothing and the "Choose Hero" one, which is the one I'm having problems with. It's supposed to reset the hero if a player chose one (hero is a string variable which equals to hero name when hero is chosen and equals to "none" otherwise) and change the state to "playable", because hero choosing screen appears in "playable" state when hero wasn't chosen. worst explanation ever
But it just doesn't work. It only changes the gamestate, the hero variable remains the same no matter how I try to change it within the function button calls. I made a bind that does what this button should to when a player presses "R" and it works perfectly. I'm trying to make this work for like 2 days but no success, so I decided to ask for help.
English is not my native language so text is probably full of mistakes, sorry about this, you will probably understand everything better by examining the .love.
Thanks in advance!
Re: Unable to change variable with a function
Posted: Wed Jan 04, 2017 4:58 am
by pgimeno
If I'm understanding your problem properly, the issue is that when you select a hero, then press Escape and then click "Select Hero" again, the hero selection screen doesn't appear again, even though when pressing 'R' it works as expected. Is that right?
Re: Unable to change variable with a function
Posted: Wed Jan 04, 2017 1:26 pm
by UniPopCorn
pgimeno wrote:If I'm understanding your problem properly, the issue is that when you select a hero, then press Escape and then click "Select Hero" again, the hero selection screen doesn't appear again, even though when pressing 'R' it works as expected. Is that right?
That's absolutely right.
Re: Unable to change variable with a function
Posted: Wed Jan 04, 2017 4:02 pm
by pgimeno
Okay. It's the mousepressed event that is setting hero back to the originally chosen one.
I think that the problem is that you don't clear heroes["mouseOver"][xxx] when one is selected, and the next time there's a click, first the button handler is called, then the heroes mouseOver detection loop sets it back.
On second thought, it's probably just the fact that there's no dedicated state for the hero selection screen. Using state "playing" for selecting the hero seems wrong.
Commented fragment below:
Code: Select all
function love.mousepressed( x, y, button, istouch )
if gamestate == "menu" then
for key, value in pairs(buttons["mouseOver"]) do
if buttons["mouseOver"][key] and button == 1 then
-- this sets hero to "none"
menu.useButton(buttons["name"][key])
end
end
-- (*)
end
for key, value in pairs(heroes["mouseOver"]) do
if heroes["mouseOver"][key] and button == 1 then
-- this sets back hero to the previous value
menu.setHero(heroes["hero"][key])
end
end
end
Inserting a return in the point I've marked (*) fixes it for me. I'd advise you to make a state for the hero selection screen, though, and only execute that last loop when in that state.
Re: Unable to change variable with a function
Posted: Wed Jan 04, 2017 11:11 pm
by UniPopCorn
pgimeno wrote:Okay. It's the mousepressed event that is setting hero back to the originally chosen one.
I think that the problem is that you don't clear heroes["mouseOver"][xxx] when one is selected, and the next time there's a click, first the button handler is called, then the heroes mouseOver detection loop sets it back.
On second thought, it's probably just the fact that there's no dedicated state for the hero selection screen. Using state "playing" for selecting the hero seems wrong.
Commented fragment below:
Code: Select all
function love.mousepressed( x, y, button, istouch )
if gamestate == "menu" then
for key, value in pairs(buttons["mouseOver"]) do
if buttons["mouseOver"][key] and button == 1 then
-- this sets hero to "none"
menu.useButton(buttons["name"][key])
end
end
-- (*)
end
for key, value in pairs(heroes["mouseOver"]) do
if heroes["mouseOver"][key] and button == 1 then
-- this sets back hero to the previous value
menu.setHero(heroes["hero"][key])
end
end
end
Inserting a return in the point I've marked (*) fixes it for me. I'd advise you to make a state for the hero selection screen, though, and only execute that last loop when in that state.
Yea, inserting return there fixed it, thought I read the code again a few times and still didn't fully understand the thing.
I initially thought that you need to make as little gamestates as possible, but I was wrong I guess, thanks for the help (and a piece of advice)!
Re: Unable to change variable with a function
Posted: Thu Jan 05, 2017 10:51 am
by pgimeno
I haven't analysed it in enough detail as to be able to say why is it behaving like that; it's a lot of code. My guess is that after clicking, heroes["mouseOver"] remains set to the last clicked hero, and the code that clears them doesn't get a chance to get executed until later.
What was clear, by putting some print statements, was that in the same click, the first setHero (the one in the button, called from useButton) cleared the hero, and then the second loop called setHero again with the old value.
As for states, well, it's a question of organization. You create as many states as you need; minimizing states is not necessarily a goal. Typically, every screen that displays data unrelated to every other screen, and that needs to handle mouse and keyboard input differently to others, has its own state. Depending on how sophisticated your GUI library is, you can maybe play with showing and hiding GUI elements instead of making a state for the different menu screens; that's a design decision. But what looks wrong is that the state used for playing is also the same state used for the hero selection GUI.