October 18, 2019, 06:29:06 PM

Author Topic: Weird behaviours e.g. Global command..  (Read 1320 times)

Offline Rick Nasher

  • Hero Member
  • *****
  • Posts: 783
Weird behaviours e.g. Global command..
« on: August 09, 2017, 08:28:30 PM »
Hi guys.

I've come across some peculiarities in AGK.

Can anyone tell me why-oh-why the following doesn't work:
Code: [Select]
global screenwidth = 1024
global screenheight  =  768
global halfscreenwidth  = screenwidth / 2
global halfscreenheight = screenheight / 2

Instead this does work??
Code: [Select]
global screenwidth = 1024
global screenheight =  768
global halfscreenwidth 
global halfscreenheight
halfscreenwidth  = screenwidth / 2
halfscreenheight = screenheight / 2

Is this a bug?


Also I tried moving a piece of character controller code from the main loop into a function, but then it somehow also doesn't work as intended; the mouse free look movement code just stops updating.

Code: [Select]

// #################### main loop #########################
do


// read keyboard input..
if getrawkeystate(87)=1 or  GetRawMouseLeftState() // W = Forward
moveobjectlocalz(player,speed#)
endif
if getrawkeystate(83)=1 or GetRawMouseRightState() // S = Backward
moveobjectlocalz(player,-speed#)
endif
if getrawkeystate(65)=1 // A = Left strafe
moveobjectlocalx(player,-speed#)
endif
if getrawkeystate(68)=1 // D = Right strafe
moveobjectlocalx(player,speed#)
endif

if getrawkeystate(27)=1  // Esc= End game.
//end
endGame=1
endif

// read mouse input..
mousex#=getpointerx()-  halfscreenwidth   //read how far from the misscreen the pointer moved.,  so basically the distance
mousey#=getpointery()- halfscreenheight

xmove#=xmove#+(mousex#)  // x=x + mouse distance
ymove#=ymove#+(mousey#)

// limit up/down movements
if ymove#<  -screenwidth
ymove#= -screenwidth
endif
if ymove# > screenwidth   
ymove# = screenwidth 
endif

setcamerarotation(1,ymove#/16,xmove#/16,0)
setobjectrotation(player, getobjectanglex(player), getcameraangley(1), getobjectanglez(player))

setcameraposition(1,getobjectx(player), GetObjectHeightMapHeight(TerrainObjectID,GetObjectx(player),GetObjectz(player))+1.6, GetObjectz(player))
setrawmouseposition(halfscreenwidth,halfscreenheight)
   Sync()
loop
Code: [Select]
do
playercontroller()

   Sync()
loop

I've tried delcaring all as globals but nogo.
Only thing I can't declare global is the cam as the language doesn't contain a CreateCamera command as in Blitz3d.

Anybody has a clue, why this refuses to work from within a function?
_______________________________________
 B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2150
Re: Weird behaviours e.g. Global command..
« Reply #1 on: August 09, 2017, 09:15:45 PM »
Regarding the below not working.
Quote
global halfscreenwidth  = screenwidth / 2
global halfscreenheight = screenheight / 2
It's not a bug it's just that ideally you are supposed to define your variables first, for example Global halfScreenHeight As Integer. You can assign an already defined variable on declaration but it doesn't like, as you found out, declaring a variable with math as it's value.

Quote
Also I tried moving a piece of character controller code from the main loop into a function, but then it somehow also doesn't work as intended; the mouse free look movement code just stops updating.
I bet ya missed declaring one or more as a global variable :P. Check check check :D
Until the next time...

Offline Rick Nasher

  • Hero Member
  • *****
  • Posts: 783
Re: Weird behaviours e.g. Global command..
« Reply #2 on: August 09, 2017, 11:05:25 PM »
Aaarrrghhhh! Big thanks, of course you were right. Beginners mistake, pfff. :-[ That's what ya get from holiday partying with way too much booze few days in a row I guess.  ;D Read over it like a gazillion times and still didn't see it.

Was also thrown of a bit by the Global thing, was starting to have doubts about the language consistency, but I was wrong apparently.


_______________________________________
 B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 617
    • Cerberus X
Re: Weird behaviours e.g. Global command..
« Reply #3 on: August 10, 2017, 08:05:22 AM »
One thing you should  add is a #option_explicit at the top of your main file. This way you will avoid missing declarations or typos.

Offline Rick Nasher

  • Hero Member
  • *****
  • Posts: 783
Re: Weird behaviours e.g. Global command..
« Reply #4 on: August 10, 2017, 10:58:05 AM »
Indeed that helps, thanks. I think that if the language is this picky, they rather had made #option_explicit non-optional. lol
It would certainly prevent headaches afterwards.

Nevertheless, still having fun with it, similar to Blitz3d in then beginnings, before mark decided to re-invent the wheel and went for a C#/++ rewrite. I'm kinda falling in love with this.(  I doubt if he checks here, but just in case  ;D )




 
_______________________________________
 B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 617
    • Cerberus X
Re: Weird behaviours e.g. Global command..
« Reply #5 on: August 10, 2017, 11:14:34 AM »
Well, AGK is a BASIC dialect. So if you don't make typos, then you can get anway with less typing. But having #option_explicit is good for us coders who are used to more restrictive coding.

Offline Rick Nasher

  • Hero Member
  • *****
  • Posts: 783
Re: Weird behaviours e.g. Global command..
« Reply #6 on: August 10, 2017, 11:30:58 AM »
It indeed does make for a more structured approach.
[cut.. moved to new topic  ;D]
_______________________________________
 B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________