Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Code in indirect file behaves different

Author
Message
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 7th Mar 2017 22:22
Hello

Sorry for weird title, no idea how I should put it.

I have some code that moves the camera around with mouse + keyboard:


Works like a charm when I put this in the main loop.

But when I put that in a function, and call that function in the mainloop, like this:



The mouseinput is registered wrong, when I click, the camera is repositioned seemingly arbitrairarly, but it works when I move and click, but on the next click, the camera is again repositioned.
Why does this code behave different when put in a function?

Sorry if this is a stupid question, I'm really a beginner.

Thanks!
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 7th Mar 2017 22:23
Sorry title is wrong, I thought it had something to do with code in another file, but in a function in the same file yields the same results.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 8th Mar 2017 00:20 Edited at: 8th Mar 2017 04:03
i'm not 100% sure but startx#, starty#, angx# and angy# may be defined within the function and therefore they may be destroyed when the function returns
try defining them as global at the top of your program
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 8th Mar 2017 01:18
The start variables only look to be used in the function though so that shouldn't be an issue.

I'm interested in the 'pressed' variable which doesn't seem to be used in this snippet, I feel like we are missing the full picture.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 8th Mar 2017 01:38 Edited at: 8th Mar 2017 04:04
startx#, starty#, angx# and angy# need to be persistent because they are only set when the pointer is pressed
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 8th Mar 2017 04:00
Oh you're right, good catch
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 8th Mar 2017 08:36
Hi guys

Thanks! I should've known that the variable went out of scope indeed. I expected that the variable would go out of scope after every loop also. But it is only destroyed after the do loop is finished (which is never in case of the main loop)

Thanks a bunch!

@Ortu, 'pressed' is legacy and does nothing in this context. The code shown came directly from the Terrain example, in that code 'pressed' is used for something else.

Login to post a reply

Server time is: 2024-09-29 23:26:00
Your offset time is: 2024-09-29 23:26:00