You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found a strange behavior of the execution context in a script.
The following example works fine when it is executed directly in squirrel 3.1, but it fails when it is executed in the game with the following message AN ERROR HAS OCCURRED [the index 'Point' does not exist]
Steps to reproduce
Steps to reproduce the strange behavior:
Have a .nut script that has the following code:
classPoint {
x =null;
y =null;
constructor(_x, _y) {
x = _x;
y = _y;
}
}
classRectangle {
topLeftPoint =null;
bottomRightPoint =null;
constructor() {
topLeftPoint =Point(0, 0);
bottomRightPoint =Point(100, 100);
}
}
rectangle <- Rectangle();
Craete a map that references our script by logic_script entity and run the map.
See the following error in the console AN ERROR HAS OCCURRED [the index 'Point' does not exist]
Expected behavior
The expected behavior is to run the map and the Rectangle class is successfully instantiated.
Additional context, that may help for better understanding the problem
After hours of reading, debugging and testing I reproduce the same error in pure squirrel (v3.1), and it looks like the core of the problem should be in the execution context.
Reproduced in Squirrel v3.1 - Nested execution context
CustomScope <- {}
classCustomScope.Point {}
classCustomScope.Circle {
position =null;
constructor() {
// Here the Point does not exists either in current environment (this) and root table(::).// It exists ony in the CustomScope table
position =Point(0, 0);
}
}
functionCustomScope::Run() {
// Here the Point and Circle exists, because the current environment(this) is the CustomScope.local point =Point();
local circle =Circle();
}
CustomScope.Run();
Reproduced in Squirrel v3.1 - Execute a file in nested execution context
## execution codeCustomScope <- {}
functionCustomScope::Run() {
dofile("file-to-include.nut"); // load, compile and execute a code in the CustomScpoe execution context
}
CustomScope.Run();
## file-to-include.nutclassPoint {}
classCircle {
position =null;
constructor() {
// Here the Point does not exists either in current environment (this) and root table(::).// It exists ony in the CustomScope table
position =Point(0, 0);
}
}
// Here the Point and Circle exists, because the current environment(this) is the CustomScope.local point =Point();
local circle =Circle();
Tested in Mapbase VScript - Workaround for the issue
// define the script's scope in the root table, in order to access it in any execution context
::MyScriptScope <- this;
classPoint {
x =null;
y =null;
constructor(_x, _y) {
x = _x;
y = _y;
}
}
classRectangle {
topLeftPoint =null;
bottomRightPoint =null;
constructor() {
// access the Point class by accessing the MyScriptScope, which is defined in the root table
topLeftPoint = ::MyScriptScope.Point(0, 0);
bottomRightPoint = ::MyScriptScope.Point(100, 100);
}
}
rectangle <- Rectangle();
printl(rectangle);
The text was updated successfully, but these errors were encountered:
VZhelev
changed the title
[VSCRIPT]
[VSCRIPT] Class defined in the script' scope is not accessible from another class
Jan 20, 2022
VZhelev
changed the title
[VSCRIPT] Class defined in the script' scope is not accessible from another class
[VSCRIPT] Class defined in the script' scope is not accessible in another class in the same scpoe
Jan 20, 2022
This is the expected behaviour. The scope your script is running is not a delegate to your classes for them to fallback to, but every context falls back to the root table (since Squirrel 3.0).
Scripts executed inside entities are placed in a table in the root table. Assume T is the entity scope, A, B, C and D are defined inside your script file.
There are no delegations here. A and B are independent tables, C and D are independent classes. They can only access themselves and the root. T.B.fn() and T.D.fn() will fail because they don't see other variables inside T.
For tables to access their 'siblings' inside T, you need to define T as their delegate.
A<-{}
B <-{functionfn(){printl(A)}}.setdelegate(this)
Now B.fn() will work.
For classes, you will have to inherit, and access the base class with base.
C<-class{}
D <-classextends C { function fn(){printl(base)}}
Class inheritance is more restrictive, so you can simply declare the variables they need to access using local. The solution to your problem becomes:
Description
I found a strange behavior of the execution context in a script.
The following example works fine when it is executed directly in squirrel 3.1, but it fails when it is executed in the game with the following message AN ERROR HAS OCCURRED [the index 'Point' does not exist]
Steps to reproduce
Steps to reproduce the strange behavior:
Expected behavior
The expected behavior is to run the map and the Rectangle class is successfully instantiated.
Additional context, that may help for better understanding the problem
After hours of reading, debugging and testing I reproduce the same error in pure squirrel (v3.1), and it looks like the core of the problem should be in the execution context.
The text was updated successfully, but these errors were encountered: