Skip to main content

News

Topic: Sphere 1.5 errors for no reason. (Read 5319 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • Mooch
  • [*][*][*]
Sphere 1.5 errors for no reason.
Here's my main.js.
Code: [Select]
function Move(name, inputs) {
    if (!IsCommandQueueEmpty(name)) return;

    if (IsKeyPressed(inputs.up)) QueuePersonCommand(name, COMMAND_MOVE_NORTH, false);
    else if (IsKeyPressed(inputs.down)) QueuePersonCommand(name, COMMAND_MOVE_SOUTH, false);
    else if (IsKeyPressed(inputs.left)) QueuePersonCommand(name, COMMAND_MOVE_WEST, false);
    else if (IsKeyPressed(inputs.right)) QueuePersonCommand(name, COMMAND_MOVE_EAST, false);
}

var inputs = [];
inputs[0] = { up: KEY_UP, down: KEY_DOWN, left: KEY_LEFT, right: KEY_RIGHT };
inputs[1] = { up: KEY_W, down: KEY_S, left: KEY_A, right: KEY_D };
inputs[2] = { up: KEY_U, down: KEY_J, left: KEY_H, right: KEY_K };
inputs[3] = { up: KEY_NUM_8, down: KEY_NUM_2, left: KEY_NUM_4, right: KEY_NUM_6 };

var players = ["player1", "player2", "player3", "player4"];

function Update() {
    for (var i = 0; i < players.length; ++i) {
        Move(players[i], inputs[i]);
    }

function setPlayerPos() {
    SetPersonXYFloat("player1", 32, 32);
    SetPersonXYFloat("player2", 32, 128);
    SetPersonXYFloat("player3", 128, 32);
    SetPersonXYFloat("player4", 128, 128);
}

function game() {
    CreatePerson("player1", "spWar.rss", false);
    CreatePerson("player2", "spWar.rss", false);
    CreatePerson("player3", "spWar.rss", false);
    CreatePerson("player4", "spWar.rss", false);

    QueuePersonScript("player1", "setPlayerPos();", true);
    QueuePersonScript("player2", "setPlayerPos();", true);
    QueuePersonScript("player3", "setPlayerPos();", true);
    QueuePersonScript("player4", "setPlayerPos();", true);

    SetUpdateScript("Update()");
    MapEngine("testmap.rmp", 60);
}


It runs fine. However, if I edit it in any way, then revert it back to EXACTLY what you see above, even if I freshly copy-paste, I start getting stupid errors.

First I'll get "missing } after function body line 44"
If I add a } to the very end of the code, I'll then get
"Reference error game is not defined."

Really? Game is not defined?

Any possible idea what's going on?

Re: Sphere 1.5 errors for no reason.
Reply #1
It's not complaining that you need a close curly bracket on the last line, just that the parser reached the last line and there is still a bracketed block that hasn't been ended with a close curly bracket.

Check you Update function, it looks like it is missing a close curly bracket.

'Game is not defined' means, in this case, that well and truly game() is not defined as a global function. In this case it's because putting a close curly bracket on the last line makes the game() part of the scope of Update(), since putting a close curly bracket on the last line makes Update() span from the function declaration to the last line. Which includes all the functions that are defined after it.

Your indentation is very clean--which is excellent, it is what helps you see these errors quickly.
  • Last Edit: December 07, 2013, 06:53:30 am by Flying Jester

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Sphere 1.5 errors for no reason.
Reply #2
When I first used Sphere I'd do the same thing. I'd curse the screen for wondering why, then see my error moments later. Just to know I had a pretty rocky relationship with my monitor the first few months. :P (But that was so long ago)...
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Mooch
  • [*][*][*]
Re: Sphere 1.5 errors for no reason.
Reply #3

It's not complaining that you need a close curly bracket on the last line, just that the parser reached the last line and there is still a bracketed block that hasn't been ended with a close curly bracket.

Check you Update function, it looks like it is missing a close curly bracket.

'Game is not defined' means, in this case, that well and truly game() is not defined as a global function. In this case it's because putting a close curly bracket on the last line makes the game() part of the scope of Update(), since putting a close curly bracket on the last line makes Update() span from the function declaration to the last line. Which includes all the functions that are defined after it.


I...actually can believe I missed that. Thanks. And double thanks for the explanation. I see what happened now.

So strange, though, 'cause I don't remember deleting that curly bracket.

Your indentation is very clean--which is excellent, it is what helps you see these errors quickly.


It's Radnen's code. I just copy-pasted it, heh. I typically indent differently than most people (I find standard indentation difficult to read), which is perhaps why I didn't see the error.


When I first used Sphere I'd do the same thing. I'd curse the screen for wondering why, then see my error moments later. Just to know I had a pretty rocky relationship with my monitor the first few months. :P (But that was so long ago)...


I curse the screen, then give up and walk away, heh. I'll get there, though.

Re: Sphere 1.5 errors for no reason.
Reply #4

Your indentation is very clean--which is excellent, it is what helps you see these errors quickly.


It's Radnen's code. I just copy-pasted it, heh. I typically indent differently than most people (I find standard indentation difficult to read), which is perhaps why I didn't see the error.


So long as you can read it, and the intent is clear to you, indent however makes you comfortable  :)

  • Mooch
  • [*][*][*]
Re: Sphere 1.5 errors for no reason.
Reply #5
*finger pistols*

You know it.