My global script suddenly no longer works

Started by Jordanowen42, Sun 12/05/2024 06:32:29

Previous topic - Next topic

Jordanowen42

Hello all-

I recently resumed working on my game after a break of several months and when I ran the game I got an unexpected global script error:

GlobalScript.asc(3): Error (line 3): expected variable or function after import, not 'ac'

Here's the code:

Code: ags
// main global script file

import ac;
import ac.Keyboard;


Any help would be greatly appreciated!



Crimson Wizard

The above script makes no sense in AGS, could you clarify what were you trying to do there?

Perhaps delete or comment these lines and see what happens then?

Khris

import lines are supposed to go into the header; they also require the type.

Like this:

Code: ags
// header
struct AudioChannels {
  AudioChannel* Keyboard;
};

import AudioChannels ac;

// main script

AudioChannels ac;
export ac;

You can know use a line like this in a room script:
Code: ags
  // inside some function
  ac.Keyboard = aKeyboard.Play();

Jordanowen42

Quote from: Crimson Wizard on Sun 12/05/2024 10:26:48The above script makes no sense in AGS, could you clarify what were you trying to do there?

Perhaps delete or comment these lines and see what happens then?

I didn't write the code- it seems to be something AGS set up automatically.

Snarky

#4
Quote from: Jordanowen42 on Mon 13/05/2024 09:07:22I didn't write the code- it seems to be something AGS set up automatically.

Not likely. The only way that could happen "automatically" is if it was part of a template you chose when you started the project, or a module you imported. It's certainly not part of any standard template (since it is invalid AGS code), and if it was part of a (broken) module, it wouldn't appear in GlobalScript.

And in any case, if it had been there the whole time, you'd have had the same error the whole time. So almost certainly, you either wrote or edited this code, and simply forgot about it. (In your first forum post you mention using ChatGPT to test your code; this is the kind of invalid AGS code that ChatGPT might very likely generate, so I suspect you got it from there and copy-pasted it into your script.)

Edit: Unless you have something like the example struct Khris gave, the lines are probably nonsense, and I'd just delete them.

Jordanowen42

Hello again-

So I pulled up a backup of the game that I saved to a flash drive months ago and haven't touched since... and it also has this global script that the AGS editor can no longer work with. I honestly don't understand what's happening here- I'm very much a noob on this and haven't messed with the global script at all so I don't know to make of it. Here is my entire global script. Same error message as above.

If anyone can make sense of this, I would appreciate it.

Kind regards,
-J

Code: ags

// main global script file

import ac;
import ac.Keyboard;


// Define global variables here



// called when the game starts, before the first room is loaded

// put anything you want to happen every game cycle in here
function repeatedly_execute() 
{
}

// put here anything you want to happen every game cycle, even when the game is blocked
function repeatedly_execute_always() 
{
  
{
  // Check if the space bar is pressed
  if (Keyboard.SpaceKey)
  {
    // Show or hide the GUI depending on its current visibility
    if (GUI.Visible)
    {
      GUI.Visible = false; // hide the GUI
    }
    else
    {
      GUI.Visible = true; // show the GUI
    }
  }
}
}

// called when a key is pressed. keycode holds the key's ASCII code
function on_key_press(eKeyCode keycode) 
{
  if (IsGamePaused()) keycode = 0; // game paused, so don't react to keypresses
  
  if (keycode == eKeyCtrlQ) QuitGame(1); // Ctrl-Q
  if (keycode == eKeyF9) RestartGame(); // F9
  if (keycode == eKeyF12) SaveScreenShot("scrnshot.pcx");  // F12
  if (keycode == eKeyCtrlS) Debug(0,0); // Ctrl-S, give all inventory
  if (keycode == eKeyCtrlV) Debug(1,0); // Ctrl-V, version
  if (keycode == eKeyCtrlA) Debug(2,0); // Ctrl-A, show walkable areas
  if (keycode == eKeyCtrlX) Debug(3,0); // Ctrl-X, teleport to room
  
}

function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
  if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
  {
  }
  else if (button == eMouseLeft) 
  {
    Room.ProcessClick(mouse.x,mouse.y, mouse.Mode);
  }
  else // right-click, so cycle cursor
  {   
    mouse.SelectNextMode();
  }
}

function dialog_request(int param) {
}



eri0o

#6
This doesn't look like AGS Script code there, there is a nesting that doesn't make sense, there's a way to read keyboards that also doesn't make sense. If you are trying to use any AI models they are not going to be able to write AGS Script code.

There's no Keyboard struct, so that check doesn't make sense and should be removed.

There's no need for deep nesting so those open and closing curly brackets that aren't attached to anything can be removed.

There's no GUI object as GUI is the name of the static struct for GUIs, so that code is nonsense too and should be removed.

Snarky

#7
Quote from: Jordanowen42 on Thu 16/05/2024 06:24:26If anyone can make sense of this, I would appreciate it.

Is this the complete GlobalScript? There is almost nothing here; it looks (bugs aside) like it was only just started.

So if you can't remember or make sense of how it ended up like this, it might make sense to just start over.

As for fixing it, just delete the two import lines at the top and the repeatedly_execute_always() block. Those are the bits that look like AI-generated invalid AGS code. (It appears to be an attempt to display or hide a GUI when a player presses space, but it's all wrong.) The rest looks like default code that comes with one of the templates.

Jordanowen42

Thanks for the insights everybody. I think the advice to just start over is wise. Scout's honor I haven't used AI on the global script nor have I changed it in any way so I'm really not sure what happened- I'm looking for a star over Bethlehem at this point.

Anyway, I did try using AI to initially write my room scripts and, no surprise, it was total crap. Easier to just learn to code.

Thanks again for all your help.

Jordanowen42

PROBLEM SOLVED!

And the solution is a lot lamer than what I'm proud to admit: having not looked at the project for several months I didn't realize that I was opening an earlier build of the first seven rooms with crap scripting. I just pulled up the actual game and it's running fine- all forty (!) rooms (and counting) of it.

Back on the horse and riding fast.

SMF spam blocked by CleanTalk