SetGlobal ... Script MAX 64

Tech support for Battle Academy

Moderators: Slitherine Core, BA Moderators

Post Reply
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

SetGlobal ... Script MAX 64

Post by Merr »

Pip,

You're killing me :wink:

Can you remove the restriction of 64 Globals in the campaign folder?
At the very least, bump it up to 128.

The ugly part is that it counts a Global irregardless of whether it's set or not.

The biggest consumer of Globals are the BONUS' ... I can't include my Bonus mods for that reason :( .

I'm looking at a workaround ...
I hope you can bump the limit.
Thanks.
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

I'm not entirely sure what you mean - which error(s) are you seeing?

Do you mean campaign vars? Or something else? What are you using them for - there may be a better approach :).

Not sure what you mean by the BONUS - is this a variable or the bonus scripts themselves?

Sorry for being dense!

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

No camp vars ... simply the SetGlobal("blah", variable) counts as one global.

I'm not in front of my cpu at the moment, but you can duplicate this ... The error reads something like "too many globals in the script, max is 64". This happens when it loads the data from the user's campaign folder and any global in the campaign folder is counted as 1, with the total exceeding 64.

To duplicate this easily (in the meantime, until I can post the exact error), copy over all the BONUS "scripts" from the core folder into your user campaign folder. Then, in your scenarioBSF, under "PreBattleSetup", set at least 32 Globals (may need more) and run the scenario. I know it's overkill but you should get an error on the load .... If it's easier, skip the inclusion of the BONUS scripts and set 65 globals in the prebattle setup ... Again, overkill, but you'll get the error.
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

You can only have 64 global variables in a script, but that's per script instance (although it will count globals used in included scripts of course). But the BONUS scripts won't cause this issue - they are generally not included anywhere else as far as I know?

If you have ton of them in your script, then it could trigger on less that 64 of your own because there are some used in the tools scripts.

I'll take a look at whether I can improve the situation for you. What are you tending to do which uses such a lot of globals? It might be that a more flexible data container would be helpful now you guys are doing pretty complex stuff!

If I am off target again, then punt me over the campaign you are seeing the issue in and I will track down what is going on :).

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

You're right on target with everything you said :) .
pipfromslitherine wrote:But the BONUS scripts won't cause this issue - they are generally not included anywhere else as far as I know?
Yes ... however, to create a modded BONUS script it will count, however, it's not "included" with the other scripts .... simply having the BONUS scripts in the bonus folder in your camp folder will count. A really good example is your new SLITH_MP_RANDOM, note how you have the Bonus folder in that campaign (not needed but those count towards the total).
pipfromslitherine wrote:If you have ton of them in your script, then it could trigger on less that 64 of your own because there are some used in the tools scripts.
Must be doing this too ... unfortunately, in most of my experience, you have to include TOOLS and FUNCTIONS to get the scripts in the camp folder to run.
pipfromslitherine wrote:I'll take a look at whether I can improve the situation for you. What are you tending to do which uses such a lot of globals? It might be that a more flexible data container would be helpful now you guys are doing pretty complex stuff!
Well, I've split my functions off so it can be easily managed and passing one variable between functions makes globals help. Also, to make my random map compatible with any size BAM (that a player wants) I had to use globals. I can explain in detail later, showing you what's what.

The easiest thing on your end is to lift the 64 restriction :roll: ... well, easy for me to say!

Thanks!
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

Now I'm really getting confused! :)

The random MP campaign I built doesn't include any bonus overloads? And anything in the BONUS folder is a self-contained script. There isn't some system-wide global limit, it's per script instance. What error message are you seeing, is it:

Too many global values in...

or is it

Too many globals in script...

Just checking as there are two error messages for different areas of the code. My guess is we're using different terms for different things - it's always simplest having these discussions over text ;).

Unfortunately there won't be an update imminently so any fix I make this end won't be available to you anyway :(. So hopefully we can clear it up so you can continue.

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

pipfromslitherine wrote: What error message are you seeing...
Too many globals in script... (scenario script!)

It was hard to troubleshoot when I was AFK earlier... so I took a deep breath and when back to this and evaluated everything you've been saying.

It was the TOOLS.bsf all this time!!! ... In my scenario script, it included TOOLS.bsf ... bingo! It was never the BONUS scripts like you said (phew!)... I thought for sure it was the BONUS scripts adding the global count but my mind must have taken a siesta during those game runs :oops:

Thank you for walking me through this crisis. I should have said something from the start instead of plugging away at the problem and coming to my own conclusions (that's how I teach myself, the hard way!)

Now ... I have a second problem but I should be able to solve it :shock: . The problem is related to the BONUS scripts and I can't Set/Get a global directly in the BONUS script to control it's "ready" status.

Sooooo, I'm thinking I can Set the global in the scenario script, then use Get/SetScriptGlobal in the BONUS script to control the value ... Does this sound right?
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

Get/SetScriptGlobal is designed to allow you to exactly that - set globals that you are using inside the bonus (for example) script. I use this to do things like have a global 'hide' variable for a bonus, for example.

If you want to alter a global inside the bonus script from the scenario script, or I guess the other way around too given that you know the name of the scenario script, then these commands will do it for you.

If I knew the details of what you were trying to do, I might be able to suggest a better approach.

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

Ok ... cool ... I need better approaches, that's for sure!

Now, real quick (I need to run out the door ... always when it gets good too!)

I'm adding a "strategic" element when it comes to BONUS'. I've created terrain cards that consist of a special area that will "activate" a player's BONUS if they capture it. The X/Y is also collocated with a VP.

Here's the snipet of the code change to REVIVE .... If the player's unit is on the tile at the beginning of the turn, it will switch the BONUS ICON to a "READY" state.
The Global I'm using resides in the scenario BSF (pre-battlesetup, set to 0).
This works! .... but I need to change it so the player doesn't have to "sit" on the tile every startturn, and I need to add "UNAVAILABE" when they don't own it.
This simulates an "aid station", thus allowing them to use the Revive bonus.

Code: Select all

// same as usual, can return -1, -2, or >=0 for disabled, not shown, or normal respectively
function CHECK_BONUSICON_REVIVE()
{
int ret ;

	ret = 0 ;
	if( (BonusTimerCheck(-1, "Revive") != 0) )
	{
		ret = -1 ;
	}
	if( (GetScriptGlobal("R_map", "aid") != 1) )
	{
		ret = -1 ;
	}
	
	return ret ;
}

// draw anything we want to draw on top of the usual rendering.
function UIBUTTON_BONUSICON_REVIVE(x, y)
{
	// we can use the normal one as we never set a gBombardment global so it will just show the turn delay etc
	BonusIconUI(x, y, "Revive") ;
}

// Do anything we want to happen before the icon is rendered
function UISETUP_BONUSICON_REVIVE()
{
	SetUITexture("revive_bonus.tga", "ffffffff") ;
}


// You can now have multiple STARTTURN_* functions all of which are called (don't rely on the order in which
// they are called though!)
function STARTTURN_BONUS_REVIVE()
{
int i ;
int id ;

	if(GetTurn()<=0)
	{
		// we start off ready to go
		SetGlobal("gCounter", 10000) ;
	}
	
	SetGlobal("gCounter", GetGlobal("gCounter")+1 ) ;
	
	if (GetCurrentSide() == 0)
	{	
		SetScriptGlobal("R_map", "aid", 0) ;
		
		// go through all the units on side 0
		for(i=0; i<GetUnitCount(0); i++)
		{
			// get the i'th unit
			id = GetUnitID(0, i) ;
			
			// is it dead or otherwise not active?
			if(IsUnitValid(id) == 1)
			{
				// is it on the tile?
				if( (GetUnitX(id) == GetScriptGlobal("R_map", "aidX")) && (GetUnitY(id) == GetScriptGlobal("R_map", "aidY")) )
				{
					SetScriptGlobal("R_map", "aid", 1) ;						
				}
			}
		}
	}
	if (GetCurrentSide() == 1)
	{	
		SetScriptGlobal("R_map", "aid", 0) ;
		
		// go through all the units on side 1
		for(i=0; i<GetUnitCount(1); i++)
		{
			// get the i'th unit
			id = GetUnitID(1, i) ;
		
			// is it dead or otherwise not active?
			if(IsUnitValid(id) == 1)
			{
				// is it on the tile?
				if( (GetUnitX(id) == GetScriptGlobal("R_map", "aidX")) && (GetUnitY(id) == GetScriptGlobal("R_map", "aidY")) )
				{
					SetScriptGlobal("R_map", "aid", 1) ;						
				}
			}
		}
	}	
}
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

OK - well, you can (in theory) use the Get/SetScriptGlobal from a bonus script to the scenario script (using the scenario name as the script name). But you could also just define the global in the bonus script, and rely on the fact is always gets set to zero on creation.

Then you can Get/Set easily from the scenario script as desired. As I say, I do something pretty similar in a script we are using for the demo.

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

Yes ... I see ... A little overkill on that one.

If you don't mind, I have one more question ..... "g" Globals ?
How can you have one BONUS script using a "g" global for both sides?
I would have thought these interfered with each other.
As you can see, I'm still globally challenged.

Thanks!
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

Hmmm - interesting point. The command will just find the first instance of the script, and some of them can be for each side (esp in MP for sure). I shall have a look and see what the best solution would be. For now, you could make the global a mask (e.g. use bit 0 to enable for for 0, and bit 1 to enable for side 1) and that would allow the functionality to work?

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

pipfromslitherine wrote:Hmmm - interesting point. The command will just find the first instance of the script, and some of them can be for each side (esp in MP for sure). I shall have a look and see what the best solution would be. For now, you could make the global a mask (e.g. use bit 0 to enable for for 0, and bit 1 to enable for side 1) and that would allow the functionality to work?
I think I figured out how a "g" global works, and why it can be jointly used. It appears, from my observations, that the "g" global is saved for the that side when the turn ends. Then, when it's the players turn again, it will load the last value. Currently, I don't see an application for "g" gloabls when generating a random map. So, if my theory is right, I might be able to use a "g" global in future apps.

Your "mask" idea is brilliant! Listen to this ...

Remember I said that the VP is collocated with the "aid station" terrain card? Well, the VP global logic was staring me right in my face! So, I didn't need to create a new global.
Below is the change I made to my new REVIVE_AID.bsf ... Also, I didn't need to add new code to the function STARTTURN_BONUS_REVIVE() ...

Code: Select all

// same as usual, can return -1, -2, or >=0 for disabled, not shown, or normal respectively
function CHECK_BONUSICON_REVIVE()
{
int ret ;

	ret = 0 ;
	if( (BonusTimerCheck(-1, "Revive") != 0) )
	{
		ret = -1 ;
	}
	if (GetScriptGlobal("R_map", "vp9") != GetCurrentSide())
	{	
		ret = -1 ;
	}	
	
	return ret ;
}
Now, I can use this same mask when controlling the Bonus icon button. For this, I had to make a new function, MerrBonusIconUI. This solves all my logic, allowing the bonus to become available and continues to be available without the players unit on the VP ... 8)

Code: Select all

// draw anything we want to draw on top of the usual rendering.
function UIBUTTON_BONUSICON_REVIVE(x, y)
{
	// we can use the normal one as we never set a gBombardment global so it will just show the turn delay etc
	MerrBonusIconUI(x, y, "Revive") ;
}

// deal with the logic for a bonus button
FUNCTION MerrBonusIconUI(x, y, name)
{
int wait ;
int turnDelay ;

	StartString() ;

	if( GetGlobal("gBombardment") > 0)
	{
		PrintString("IDS_BONUS_IMPACT") ;
		PrintStringLiteral(":-") ;
		PrintInt( GetGlobal("gBombardment") ) ;
		
	}
	else
	{
		turnDelay = GetBonusValue("Delay", GetCurrentSide(), name) ;

		wait = turnDelay - GetGlobal("gCounter") ;

		if((wait<=0) && ( GetScriptGlobal("R_map", "vp9") == GetCurrentSide() ) )
		{
			PrintString("IDS_BONUS_READY") ;
		}
		else
		{
			if((wait>0) && ( GetScriptGlobal("R_map", "vp9") == GetCurrentSide() ) )
			{
				PrintInt(wait) ;
				PrintStringLiteral(" ") ;
				PrintString("IDS_BONUS_TURNS") ;
			}
			else 
			{
				PrintStringLiteral(" ") ;
				PrintString("IDS_BONUS_UNAVAILABLE") ;
			}
		}
	}
	
	RenderString(x+2,y+22,6,"ffffffff", "ff000000") ;
	
	// now make a tooltip
	StartString() ;
	StartWorkString() ;
	PrintWorkStringLiteral("IDS_BONUS_") ;
	PrintWorkStringLiteral(name) ;
	PrintString( GetWorkString() ) ;
	PrintStringLiteral("\n") ;

	if( GetGlobal("gBombardment") > 0)
	{
		PrintString("IDS_BONUS_TT_IMPACT") ;
		PrintInt( GetGlobal("gBombardment") ) ;
	}
	else
	{
		turnDelay = GetBonusValue("Delay", GetCurrentSide(), name) ;

		wait = turnDelay - GetGlobal("gCounter") ;

		if((wait<=0) && ( GetScriptGlobal("R_map", "vp9") == GetCurrentSide() ) )
		{
			PrintString("IDS_BONUS_TT_READY") ;
		}
		else
		{
			if((wait>0) && ( GetScriptGlobal("R_map", "vp9") == GetCurrentSide() ) )
			{		
				PrintString("IDS_BONUS_TT_TURNS") ;
				PrintInt(wait) ;
			}
			else 
			{
				//PrintStringLiteral(" ") ;
				PrintString("IDS_BONUS_TT_UNAVAILABLE") ;
			}			
		}
	}	
	
	SetUITooltip() ;
}
Thanks a bunch for your help! This concludes Pip's lesson on globals ... :D
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

Heh - looks cool! :)

Technically, every instance of a script has its own set of globals - only universals can be shared across all scripts. Hmmm - looking at my notes here, I think I also just fixed a bug in Get/SetScriptGlobal, so if you see any oddness then that might be caused by that (basically it's going to struggle to work at all). Just spotted it. :(

Cheers

Pip
Merr
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 903
Joined: Mon Aug 16, 2010 2:00 pm

Post by Merr »

So far, my initial tests appear to work fine ....

I'll run a more aggressive test where the global changes hands more often as I feel your bug might be related to the logic not returning the correct value?

In the meatime, when you have a moment ... What oddities should I be looking for? (or is it too ugly to mention on public TV :P ).
pipfromslitherine
Site Admin
Site Admin
Posts: 9925
Joined: Wed Mar 23, 2005 10:35 pm

Post by pipfromslitherine »

I think it depends on the number of scripts loaded, and their type. Basically it was returning the wrong script instance, so would often not find globals you had expected to be created. I guess if you create from outside, into the other script (if that makes sense) then it would be there when you expect it, even if it was living in the wrong script instance.

Not sure that makes sense, but just letting you know that it really is broken (honest!) even though it might work in your case - so beware! :)

Cheers

Pip
Post Reply

Return to “Battle Academy : Tech Support”