Global Entities

Started by Steve Elliott, January 22, 2018, 18:57:02

Previous topic - Next topic

Steve Elliott

I don't know how Blitz3D works regarding 2D or 3D Graphic Entities, but I'm guessing it's the same as AGK 2; that is, a 2D Sprite, or a 3D mesh are treated as global and therefore can be accessed from any function.

Using lots of global variables is regarded as bad practice, so what are your thoughts on graphic entities in a language engine behaving in this way?

Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

RemiD

#1
Quote
2D or 3D Graphic Entities, but I'm guessing it's the same as AGK 2; that is, a 2D Sprite, or a 3D mesh are treated as global and therefore can be accessed from any function.
in Blitz3d, unless an entity is defined as global or if it is in a dim() array (which is global), an entity is not global

Quote
Using lots of global variables is regarded as bad practice, so what are your thoughts on graphic entities in a language engine behaving in this way?
i used many globals in my game, and several goto, but the game works well, is stable, runs fast enough.
So why should i care about arbitrary rules ?
My suggestion : listen to what others have to say to learn new concepts/techniques, but don't necessarily repeat and follow, do your thing. If it works well, all good !

If you understand how your code "flows" (is executed), there is no reason to have a problem due to a global...
The exception is if you are not the only person writing code for a project, but there are ways to use globals even in a team work, for example if each coder add a prefix to the variables/lists/functions which are used only for a part of the game.

Steve Elliott

Quote
in Blitz3d, unless an entity is defined as global or if it is in a dim() array (which is global), an entity is not global

Ah, interesting.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

STEVIE G

In B3d I avoid using a lot of globals, if any.  Not to adhere to some mythical rule, probably more an OCD thing as I like neat code.

I've found that the neatest and easiest method is to create a Game Type and store almost everything there.  You only need to make one instance of the Game Type global.  The only exception would be if you need multidimentional arrays but you can obviously store them in a single array too.

Unless you are working as part of a team, where rules must be set and adhered to I would do what suits your own style rather than conforming to some standard.  Noone else will see the code anyway  ;D



RemiD

What i find important, is to choose variables names (especially for globals or constants) which are unique and different from the keywords (commands names) of the language/engine that you use.
This way, when you need to change the name of a variable, or you need to see how the variable is used/modified, you can quickly search it using the "search next keyword" (F3 on blitz3d)

Steve Elliott

Thanks, but I was really more interested in the way a language (such as Blitz3D) integrated it's non logic system.

Because of the UpdateWorld and RenderWorld commands in Blitz3D I'd assumed graphic entities were all global - which I don't think is a good way to implement such a system.

Now it's been confirmed entities are local by default I'll take another look at how it handles things.  Yes I never really used Blitz3D, and I don't intend too.  I was just curious how Mark tackled this problem.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

RemiD

I think that what a coder writes in an ide is different than what is going on behind the scenes.
So you have to take a look at the c++ source code to understand what is really going on...

( updateworld() is a really weird name (this function detects collisions and reposition colliders, but also updates the position of skinned vertices (influenced by joints/animations) )
( renderworld() is a ok name ( this function renders the 3d scene on an image (on the backbuffer ) of the size of the defined camera view )

TomToad

Entities are neither global nor local.  They are structures internally maintained by Blitz3D.  You interact with the entities through the use of handles.  Any command or function can access and modify the entities given that it has the proper handle.

This is the best way for things to work.  The idea of local is that functions don't have access to those variables, and thus cannot accidentally alter the contents.  For example, If I were to call MyFunction(MyVariable), The contents of MyVariable will be copied, and the function MyFunction() works on that copy.  When the function exits, the copy is destroyed.  The original MyVariable will be left exactly as it was when the function was called.

If entities were local, then the only way a function could access it is if a copy of the entity is made, but then any modifications made to the entity will be lost when the function ends and the copy is destroyed.  By sending a copy of the handle instead, the function can then access the entity through that handle.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Steve Elliott

Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb