I'm trying to define variables using default:
Python:
# Define variables.
default x1 = 0
default x2 = 0
default x3 = 0
default x3 = 0
# The script of the game goes in this file.
I'm placing the variables at the very top of the script file, and when I try to run the game is giving me an error:
File "game/script.rpy, line 8: expected statement.
Line 8 has the "# The script of the game goes in this file." which is just a comment.
What am I doing wrong?
Based on this code... as far as I can see, nothing is wrong - at least not structurally.
You do have TWO
default x3 = 0
lines. Why that would generate that particular error, I don't know. When I add that with RenPy 7.3.4, I get a different error.
It is perfectly fine to have those
default
lines right at the top of the code, as you've described.
(Well, after you remove the duplicate line).
I just want to set the variables value to 0 so I can add or subtract later on.
To risk confusing matters... you don't technically need
default
to do that. But it is still a really, really good idea to code it this way.
In theory, you could do:
$ x1 = 0
$ x1 += 1
... which would create the x1 variable with a value of 0 and then add 1 to it.
However the problem is save file compatibility.
If in a later patch you added
$ x5 = 0
and someone loaded a save file from after where that line of code could have been executed,
x5
wouldn't exist - because RenPy hasn't run that line of code for that particular player. Which is one of the reasons why
default
was added. It is executed at init time and so the value ALWAYS exists (and so doesn't cause runtime errors).
As far as your original problem... I'm going to guess it's that 2x "x3" definition - even though I got a different error.
The only other thing that comes to mind would be something like a misplaced {line feed} or {carriage return} character in there somewhere. Some editors, especially in Linux/Unix don't always use both {LF}{CR} at the end of each line. Could be some weird thing where RenPy sees two lines merged together. I really doubt this one, but it came to mind.
I note by your first update, you've removed the duplicate x3 and added x4 and x5... and things are fixed. I'm going to suggest that, rather than where you moved it to as being the reason it works from that point.