Static

Previous topic - Next topic

erico

We have STATIC command inside GLB, I wonder out of curiosity if it can be used as this nice article states:
http://hackaday.com/2015/08/04/embed-with-elliot-the-static-keyword-you-dont-fully-understand/#more-164040

MrTAToad

#1
You mean this example :

Code (glbasic) Select
void loop()
{
    static int count;
    count = count + 1;
    if (count > 10) {
        Serial.println("Hello World");
    }
}
?

This is the GLBasic equivalent :

Code (glbasic) Select
WHILE TRUE
loop()
SHOWSCREEN
WEND


FUNCTION loop%:
STATIC count% = 0

INC count%
IF count%>10
DEBUG "Hello world"
ENDIF
ENDFUNCTION


I think you will find that it works exactly the same :)

As or the later examples, it wont work, most likely due to how C++ works (GLBasic wont allow STATIC variables in the global space).  However, that is where extended TYPES come in - you would have count% and the various functions in there, which would more or less that same way, without not much extra code.

Hemlos

#2
Use TYPE, oops did i say that again?!

lol

GLOBALS and STATIC are alike ONLY in the sense that thier values stay until you change them.
They are UNALIKE in the way of SCOPE....static is scoped into the code and limited to where its called.
GLOBALS are not scoped and they can corrupt multiple runs of a program.

Use TYPE....they are more like globals than STATIC is!

TYPE MyGlobals:
MyNameIs$
ENDTYPE

//Header:
LOCAL NAME AS MyGlobals

//Anywhere in your Program:
NAME.MyNameIs$ = "Neil"



Now you can use this anywhere in the program, and you can change it anywhere.



Bing ChatGpt is pretty smart :O

erico

From the article and both your examples I get a glimpse on how this would work on GLB.
I understand the logic of a static variable within the global/local scope,
but I fail to actually picture a use of it in a game.

Either way, thanks for the info!

MrTAToad

It would all depend on how you view globals really - there isn't really much of the difference.  Static does make things a bit tidier, but at the expense of trying to find variables :)

Hemlos

#5
There is a big difference in memory declarations in c++, which is what glbasic is made in righty?

GLOBAL  // can be accessed outside the program GLOBAL to the computer.
GLOBAL // permanent during duration of runtime

STATIC // can not be accessed outside the program
STATIC // permanent during duration of runtime, IN SCOPE ONLY.

LOCAL Permanent$ //this in your header is a global....same as static cant be externaly accessed.
LOCAL Temporary$ // elsewhere in a program, other than header, the memory goes away after the frame render, and is a scope.


G has recommended to me once upon a time, that using LOCAL in your header is the best way to go.
edit: i forgot this note...LOCAL in the header is global....in the main loop only.
eg. it wont scope into a function.
Bing ChatGpt is pretty smart :O

Hemlos

Local is tricky..and interestingly enough..
If you declare a LOCAL TYPE in the header...it wont scope into a function either.
Bing ChatGpt is pretty smart :O

kanonet

Quote from: HemlosLocal is tricky..and interestingly enough..
No, LOCAL is easy and the way to go, see below.

Quote from: Hemlosheader
Actually I wonder what you are talking about Hemlos, what is a header in GLBasic? GLBasic only has .gbas files, no differentiation between headers and implementation files.

Quote from: HemlosGLOBAL  // can be accessed outside the program GLOBAL to the computer.
This is just simply wrong. A GLOBAL can not be accessed outside of the program, all variables are just for internal use of the program. If you want something to be accessed from outside you could set a environment variable or write to a file or something... Generally this should be avoided of cause.

Ontopic:
You can only create LOCAL variables inside the main area (in the main gbas file, above the 1st FUNCTION/SUB) or inside a function/sub. The scope of a LOCAL variable is always local to that area where it is defined, that means you can only use it from that area. If you call a function that contains a LOCAL variable the variable gets created and when the function is done and returns to its caller, the variable gets destroyed.

STATIC variables get created in a local scope to, just like a LOCAL variable. Only difference to LOCAL is, that a STATIC variable gets not destroyed when its containing function is done and return. So you can save its value between function calls. But you still can not access it from outside of the function. So STATICs are the way to go if you need to save data between function calls, that are only needed inside that function.

GLOBALs are defined globally (yes really!) that means you can read or write to them from everywhere inside your program. You can define a GLOBAl everywhere, even outside of any function or the main scope. You should never have a variable with the same name of a GLOBAl, because this shadowing can really confuse readers of your code and my be a huge source of bugs. So creating a GLOBAL somewhere actively limits the names of variables that you use anywhere in your project, so if you create one, give it a long and meaningful name. Also being able to access and change the value of a GLOBAL everywhere in your program can be a huge source of bugs, if anything changes that value and you can have a really painful time searching that bug. So generally advice is: avoid GLOBAL when ever possible, think twice or even 3-4 times if you can find any way to avoid a GLOBAL, replace it with a static or pack all your data that belongs together in the same type. Globals are evil most times.

-> LOCALs should be the tool that you use 99,99% of your time. Really dont use anything else, unless really needed. Then try to use static, or pack the data in a TYPE and declare this one LOCAL. BTW types... they are just normal variables. Soyoucan declare an instance of a type LOCAL, STATIC or GLOBAL as any otehr variable too. Of cause you should declare it LOCAL when ever possible.

BTW all this was about variable scope in GLBasic. In c++ its a bit different (but the ban on global vars is just as important too) and the keyword static has a totally different meaning! So a C++ article about static would not help you that much in GLBasic.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrPlow

#8
I typically use a naming convention to avoid confusing locals and globals...

Globals are very handy for keeping state of timers and other key values...but are you saying that they should be replaced with Statics?!

Do Statics give a performance benefit? If so is significant?

Also using Type Endtype for all variable declares is a bit too verbose for me essentially 4 lines of code for each global val...No?!

I would think accessing via the type structure is probably slower than accessing via a global - but I dont know for sure.

Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

erico

In my case my code is usually 95% globals... :(

MrTAToad

When I did my speed tests several versions ago, there were speed advantages for each call to a function in extended types.

There should be no speed loss for STATIC variables.

Slydog

@MrPlow:
QuoteAlso using Type Endtype for all variable declares is a bit too verbose for me essentially 4 lines of code for each global val...No?!

You could use just one TYPE for ALL globals.  Extending Hemlos's code:

Code (glbasic) Select
TYPE TGlobals:
  PlayerTimer%
  EnemyTimer%
  PlayerScore%
  TimeRemaining%
ENDTYPE
LOCAL Globals AS TGlobals

Globals.PlayerTimer = 1000
Globals.PlayerScore = 2000

My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Paul Smith

Quote from: erico on 2015-Aug-13
In my case my code is usually 95% globals... :(

Me to :)

You should see the mess of my Latest app. not even sure what half of them do any more =D
Amstrad CPC 6128, ATARI STE.
Acer  SW5-173 & AMD RYZEN 7,RTX 3060TI

Hemlos

@ kano:
by header i mean the top of your program above the main loop where you define most of your globals.

I did research before sharing this K.
Globals are "extern" accessable....so you can use C++ tools to view data and memory leaks etc.
Static are not accessable this way, even though they are a global.
When you declare a GLOBAL, it gets pushed into a volatile area of your ram, thats how extern can access it....its GLOBAL for the whole computer, not just your program.....that said, this data is not secure.

I highly recommend everyone look into and study this a lil more, your code will be more reliable, and potentially safer for your computer, and your customers personal info better secured.
Bing ChatGpt is pretty smart :O

kanonet

Quoteby header i mean the top of your program above the main loop where you define most of your globals.
This is just part of what i called the main area, in C++ it will be in the main function. That you did not define a main loop yet (you dont need one) does not change anything. Its simply the main function. And variables inside main function have same scope as variables inside every function, LOCALs/STATICs from main loop are not accessible inside any other function. You can define a GLOBAL anywhere in your program, it does not need to be at top of your main file.

QuoteI did research before sharing this K.
Globals are "extern" accessable....so you can use C++ tools to view data and memory leaks etc.
Static are not accessable this way, even though they are a global.
When you declare a GLOBAL, it gets pushed into a volatile area of your ram, thats how extern can access it....its GLOBAL for the whole computer, not just your program.....that said, this data is not secure.
If you use debugging/hacking/analysing tools you can access any variable in a program, no matter if it was a global, static or local. All are in stored in ram and ram can be read out completely by programs with high enough privileges. But this has nothing to do with globals, like I said you can also read out statics/locals. But without such special tools no variable is readable from outside your program, even not globals. If you still say otherwise I demand a prove/link.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64