localvar x entityvar x indexedvar

O Ilusionista

Captain 100K
What is the difference between localvar, entityvar and indexedvar?
All of then works local and not global, right?

What is the PROs and CONS of each one?
 
An indexedvar occupies the global scope, just like a global var. Once defined you can use it anywhere, any time and it remains until destroyed or the engine is shut down.

+ Much faster than global vars. If you have a bunch of globals and replace them with indexed vars, you are almost certain to see significant framerate gains on slower platforms.
+ NOT persistent across saves, so no worries about leftover information from a save file polluting your values.
- Better keep those IDs straight or you'll have a huge mess on your hands. Use of defined constants is all but required.
- Just like globals you have to be careful to get rid of these when you don't need them or they stick around forever.



The documentation uTunnels wrote on localvars is rather fuzzy. I cannot tell if they only work in the current function or script file (the former is more likely). I've never had a use for these at all as they seem do the exact same thing as script variables with no advantages (script variables are already indexed).



Entityvars are invaluable. They are tied to the entity (not model - each spawned entity has its own set of entity vars). From special effects, stats, tracking bind for grappling, pretty much any value you want to store about an entity should go here.

+ Tied to entity allowing you to individually track stats and values with ease.
+ Since the entity's pointer is what's used internally for tracking the owner of an entity var, you can do some interesting tricks (like copying all entity vars from one entity to another in a couple of lines) if you are careful.
- Easy to get carried away with if you aren't careful. If you store 20 values to an entity, it takes only 5 spawns in play to have 100 entity variables floating around in memory.



HTH
DC
 
thank you sir.

About this:
- Easy to get carried away with if you aren't careful. If you store 20 values to an entity, it takes only 5 spawns in play to have 100 entity variables floating around in memory.
So even if the entity is destroyed, the variables will still be consuming memory, right? Even if they pointers are invalid (because the entity is no more). We should then use a ondeathscript to free those variables.

Since the entity's pointer is what's used internally for tracking the owner of an entity var, you can do some interesting tricks (like copying all entity vars from one entity to another in a couple of lines) if you are careful.
As I thought. That is cool.

One more question:
Its possible to extract all variables (local/entity/global) and populate an array? So I can print the result to track everything?
 
O Ilusionista said:
We should then use a ondeathscript to free those variables.

Correct. Index based variables are very fast and very useful but do require more up front work and cleanup.

As I thought. That is cool.

One more question:
Its possible to extract all variables (local/entity/global) and populate an array? So I can print the result to track everything?

There's no built in function to clean or get all indexed vars of any type, but you can get the maximum allowed and do it yourself with a loop. Here's an example of cleaning entity vars:

Code:
void ent = {target}			   // Target entity to clean vars.
int i	= 0;				   // Loop counter.
int max = openborvariant("maxentityvars"); // Entity var max.
	
for (i = 0; i <= max; i++)
{
     setentityvar(ent, i, NULL());
}
 
I dont recommend cleaning any variables if you want to avoid problems, you should do that only when you play again after gameover, i had some problems when i used cleaning variables and now im just setting variables to 0 instead of cleaning them.I clean globalvar only on select screen when there was game over before.
 
not cleaning unused vars is a bad habbit and will consume memory. Even if the var value is 0, the memory is still consumed.
0 and NULL are different things.

When you NULL a var, you remove the pointer to that variable. IOW its not accessible anymore. But when you simply put 0 as value, the pointer is STILL valid.
 
well, as i said, tried that, run into huge problems later and now i never clean them, only on select screen in waiting animation.
IF you dont use variables then you probably wont have problems but if you do use them then you will run into problems, you clean variable and you cant set it again properly or it will clean too much even if you clean specific one, i dont remember exactly but it gave me a lot of problems and wasted hours before i discovered that setting variable to 0 instead of cleaning it will work fine, especially if youre using it constantly during gameplay.
I didnt tried nulling them but ill try that later if its any different than cleaning them.
I also had cleaning global vars when levels started but i removed all that from he-man cause it created problems, i remember utunnels said that you shouldnt clean global variables like that if ever, better to clean individually but im doing it on select screen so everything should be erased anyway.
 
especially if youre using it constantly during gameplay.
That is the question: if you use it constantly, why you ever clean it? It would make no sense.
THEN I agree with you: just set to 0.

But my point is UNNEEDED variables. something you set one time to check something specific, then don't use it anymore.
 
I use global variable to check if he-man is on battlecat and change it to 0 when he isnt on it, if i clean it then weird stuff happens sometimes.
 
I always set NULL() and have never had any problems. Also, indexed vars are not global vars.

bWWd said:
I use global variable to check if he-man is on battlecat and change it to 0 when he isnt on it, if i clean it then weird stuff happens sometimes.

Global vars are a different animal from entity vars and indexed vars. I avoid them at all costs. They are the devil in any language, not just OpenBOR.

DC
 
bWWd said:
I use global variable to check if he-man is on battlecat and change it to 0 when he isnt on it, if i clean it then weird stuff happens sometimes.

This is what was hapening on my issue with the weapons. The drawsprite was using a globalvar to store the value, and whe you was on the normal model, it nullify the globalvar. Even if I made it check again on the weaponmodel, this happens on the next frame, so in the splitsecond where you change the model...BOOOM, crash.
 
Back
Top Bottom