Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Monsieur OUXX on Sun 29/12/2019 20:10:44

Title: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Monsieur OUXX on Sun 29/12/2019 20:10:44
Add a module to your game and paste this script into it.
Your game created in 3.4.x should compile in 3.5.x without complaining about the new Viewports system. Please note that you'll have to "replace all" System.ViewportHeight and System.ViewportWidth with System.ViewportHeight() and System.ViewportWidth().

Please note that I did that in exactly 5 minutes and it seemed to work with Tumbleweed (calin leafshade wink wink) but I didn't test thoroughly.
Also, if someone already posted that before then please ignore and assume that their version is better.



Code for the module's header:
Code (ags) Select

import int ViewportHeight(static System);
import int ViewportWidth(static System);
import int ScreenHeight(static System);
import int ScreenWidth(static System);
import int GetViewportX();
import int GetViewportY();
import void SetViewport(int x,  int y);
import void ReleaseViewport();




Code for the module's body:
Code (ags) Select

int ViewportHeight(static System)
{
  return Game.Camera.Height;
}

int ViewportWidth(static System)
{
  return Game.Camera.Width;
}


int ScreenHeight(static System)
{
  return Game.Camera.Height;
}

int ScreenWidth(static System)
{
  return Game.Camera.Width;
}

int GetViewportX()
{
  return Game.Camera.X;
}

int GetViewportY()
{
  return Game.Camera.Y;
}

void SetViewport(int x,  int y)
{
  Game.Camera.SetAt(x,  y);
}

void ReleaseViewport()
{
  Game.Camera.AutoTracking = true;
}



Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Crimson Wizard on Sun 29/12/2019 22:19:51
There's also a "Script compatibility level" option in General Settings that will return all the old functions back if you set it to previous version.
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: morph100 on Thu 14/08/2025 13:03:36
OK just picked this back up after 5 years off :-)

I've got this If statement:

  if ((ypos + autoheight + CDG_options.yscreenborder) > System.ViewportHeight(static System)) {
    ypos = System.ViewportHeight - autoheight - CDG_options.yscreenborder; 
  }


with this:

import int ViewportHeight(static System);

and i get:

DialogScript.asc(432): Error (line 432): Parse error in expr near 'static'


can anyone help me, to say im rusty is an understatement
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Crimson Wizard on Thu 14/08/2025 13:16:37
Quote from: morph100 on Thu 14/08/2025 13:03:36if ((ypos + autoheight + CDG_options.yscreenborder) > System.ViewportHeight(static System)) {

The expression "System.ViewportHeight(static System)" in this line should be just "System.ViewportHeight()".

The "(static System)" part is used only when declaring an extender function, but not when calling one.
https://adventuregamestudio.github.io/ags-manual/ExtenderFunctions.html#static-extenders
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: morph100 on Thu 14/08/2025 14:24:23
But then i get

( expected


  if ((ypos + autoheight + CDG_options.yscreenborder) > System.ViewportHeight()) {
    ypos = System.ViewportHeight - autoheight - CDG_options.yscreenborder; 
  }
  else if (ypos < CDG_options.yscreenborder) ypos =  CDG_options.yscreenborder;
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Crimson Wizard on Thu 14/08/2025 14:54:48
Which exactly line do you have this error on? Is it really the exact error message that you are getting? You can copy the error message by selecting it in the output window, right click - copy selected.

Also please clarify:
1. Which version of AGS are you using?
2. Which are the settings "Script API version" and "Script compatibility level" in General Settings?
3. Are you using these helper functions posted above?
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Khris on Sat 16/08/2025 11:22:27
The manual lists it as:
readonly static int System.ViewportHeight;i.e. it's not even a function; it's just an int.

Since 3.5.0 it's also obsolete; the proper way is to simply use Screen.Height:

  if (ypos + autoheight + CDG_options.yscreenborder > Screen.Height) {
    ypos = Screen.Height - autoheight - CDG_options.yscreenborder;
  }
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Crimson Wizard on Sat 16/08/2025 15:37:52
Quote from: Khris on Sat 16/08/2025 11:22:27The manual lists it as:
readonly static int System.ViewportHeight;i.e. it's not even a function; it's just an int.

Khris, this thread suggests a "compatibility" script module, where these attributes are turned into extender functions (see the first post). The extender attributes are not supported in 3.* versions, that's why the OP made them functions, I think.
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Khris on Sun 17/08/2025 10:09:04
Right; I should've scrolled up first :-D

But do we know that @morph100 is actually using the module/code? If it's just a few lines that have to be amended even with the module, using it seems kind of pointless.
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: morph100 on Mon 18/08/2025 11:01:17
I got the error on the first line of the IF statement.

Using latest version

Script settings all set to latest version.

Do I just need to scrap this bit and re-do it using the latest methods? What's a good resource to revise and look at.

I'm on holiday at the minute, back in a week so can read up but can't test anything unfortunately
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: Crimson Wizard on Mon 18/08/2025 15:33:28
Quote from: morph100 on Mon 18/08/2025 11:01:17I got the error on the first line of the IF statement.

Are you using the helper functions posted in the beginning of this thread, or no?
This forum thread is dedicated to having these functions in your script, as a replacement to the old engine functions. If you do not, then of course the suggested code won't work.

The three common options that you have:

1. Change "Script compatibility level" to the older version, as explained here:
https://adventuregamestudio.github.io/ags-manual/GeneralSettings.html#backwards-compatibility
This will enable old functions back.

or

2. Replace the use of the old functions with the new ones. There's a table of replacement here:
https://adventuregamestudio.github.io/ags-manual/ObsoleteScriptAPI.html

or

3. Actually copy the helper functions posted in this forum thread (first post) to your game. Better make a separate script module for them and have that module on the top of your script list (so that other scripts could use them). Then you still have to adjust your scripts, adding "()" function call for things like "System.ViewportHeight" and similar.

Personally, I can't recommend the 3rd option, because from the looks of it these helper functions are not written entirely correctly, they may fail in certain cases.
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: eri0o on Mon 18/08/2025 16:15:36
The camera script API in the manual gives some explanation on how they work

https://adventuregamestudio.github.io/ags-manual/Camera.html

You can imagine something like, your rooms can be "captured" with a camera and they are then be projected to a viewport. You can only see viewports on the screen.

By default AGS sets a camera to the size of the game resolution and "attaches" it to a viewport that also has the same size.
Title: Re: Quick Viewport helpers for upgrading from 3.4.x to 3.5 or higher
Post by: morph100 on Wed 20/08/2025 07:07:02
Quote from: Crimson Wizard on Mon 18/08/2025 15:33:28
Quote from: morph100 on Mon 18/08/2025 11:01:17I got the error on the first line of the IF statement.

Are you using the helper functions posted in the beginning of this thread, or no?
This forum thread is dedicated to having these functions in your script, as a replacement to the old engine functions. If you do not, then of course the suggested code won't work.

The three common options that you have:

1. Change "Script compatibility level" to the older version, as explained here:
https://adventuregamestudio.github.io/ags-manual/GeneralSettings.html#backwards-compatibility
This will enable old functions back.

or

2. Replace the use of the old functions with the new ones. There's a table of replacement here:
https://adventuregamestudio.github.io/ags-manual/ObsoleteScriptAPI.html

or

3. Actually copy the helper functions posted in this forum thread (first post) to your game. Better make a separate script module for them and have that module on the top of your script list (so that other scripts could use them). Then you still have to adjust your scripts, adding "()" function call for things like "System.ViewportHeight" and similar.

Personally, I can't recommend the 3rd option, because from the looks of it these helper functions are not written entirely correctly, they may fail in certain cases.


Thanks that's really useful I think I'll use option 2 to rewrite my scripts in the new format. I'd rather bring it up to date than just run in an old compatible mode