Weird behaviours e.g. Global command..

Started by Rick Nasher, August 09, 2017, 20:28:30

Previous topic - Next topic

Rick Nasher

Hi guys.

I've come across some peculiarities in AGK.

Can anyone tell me why-oh-why the following doesn't work:

global screenwidth = 1024
global screenheight  =  768
global halfscreenwidth  = screenwidth / 2
global halfscreenheight = screenheight / 2


Instead this does work??

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.



// #################### 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


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
_______________________________________

Qube

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.

QuoteAlso 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
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

Rick Nasher

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
_______________________________________

MikeHart

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.

Rick Nasher

#4
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
_______________________________________

MikeHart

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.

Rick Nasher

#6
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
_______________________________________