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
Unusual using classes in VSquirrel 2+ for Portal 2 modding can lead to critical save/load failures, effectively breaking the game. Several specific scenarios involving class interactions consistently trigger these issues.
Problem 1: Inheritance
Even simple inheritance can cause Portal 2 to crash on save/load.
Example:
classfoo {
functionsomething() printl("test")
}
classbarextendsfoo {
functiondoing() printl("bye bye my game :<")
}
While the inheritance mechanism itself works during runtime, attempting to load a saved game state containing these classes results in a crash.
Problem 2: Circular References Within Classes
Mutual references between class instances, even when using weakref, can still lead to crashes during save/load or cause the VSquirrel VM to break down. This is likely due to how references are handled during the serialization process, which fails to properly account for these circular dependencies.
Example:
classNode {
nextNode =null
prevNode =null
}
n1 <- Node()
n2 <- Node()
n1.nextNode= n2 // .weakref() does not help
n2.prevNode= n1 // .weakref() does not help
This results in errors like:
Failed to restore a Squirrel object of type <....>
Failed to restore a Squirrel object of type <....>
AN ERROR HAS OCCURED [null cannot be used as index]
CALLSTACK
*FUNCTION [VSquirrel_OnCreateScope()] unnamed line [166]
LOCALS
[result] NULL
[outer] NULL
[name] NULL
[this] NULL
Problem 3: Free Variables Within Class Methods
Using free variables within class methods in specific ways, particularly when they reference the class instance itself, can also lead to save/load errors.
Example:
classFoo {
handler =null
someInfo =nullconstructor(info) {
this.someInfo= info
}
functionRunHandler() return this.handler()
}
test <- Foo("secret information, lol")
test.handler=function() : (test) {
// something logicprintl("Aha! I know your info!!")
printl(test.someInfo)
}
Final Notes and Workarounds:
I understand that classes in VScripts are not widely used in Portal 2 modding, and therefore, these issues might not be prioritized for fixing, which is completely reasonable. However, I'd like to offer some workarounds for those who do encounter these issues:
Inheritance: Instead of using inheritance, consider using composition or replacing the class with a table and a clone method for creating copies.
Mutual References: Use a table with delegation:
::qcam <- {}
functionqcam:new(owner) {
local res = delegate qcam : {
owner = owner,
mode =null
}
res.mode=custommode(res)
return res
}
classcustommode {
camRef =nullconstructor(cam) {
this.camRef= cam
}
}
z <- qcam.new("test")
Or implement a global storage system, as in this example:
I don't quite understand why this issue was moved to this repository, which is specifically related to the Linux native port, as I understand it. It seems that the Source-1-Games repository would be a more appropriate place for this problem, considering the nature of the issue.
Could you please clarify the reasoning behind this decision?
Description:
Unusual using classes in VSquirrel 2+ for Portal 2 modding can lead to critical save/load failures, effectively breaking the game. Several specific scenarios involving class interactions consistently trigger these issues.
Problem 1: Inheritance
Even simple inheritance can cause Portal 2 to crash on save/load.
Example:
While the inheritance mechanism itself works during runtime, attempting to load a saved game state containing these classes results in a crash.
Problem 2: Circular References Within Classes
Mutual references between class instances, even when using weakref, can still lead to crashes during save/load or cause the VSquirrel VM to break down. This is likely due to how references are handled during the serialization process, which fails to properly account for these circular dependencies.
Example:
This results in errors like:
Problem 3: Free Variables Within Class Methods
Using free variables within class methods in specific ways, particularly when they reference the class instance itself, can also lead to save/load errors.
Example:
Final Notes and Workarounds:
I understand that classes in VScripts are not widely used in Portal 2 modding, and therefore, these issues might not be prioritized for fixing, which is completely reasonable. However, I'd like to offer some workarounds for those who do encounter these issues:
Inheritance: Instead of using inheritance, consider using composition or replacing the class with a table and a
clone
method for creating copies.Mutual References: Use a table with delegation:
Or implement a global storage system, as in this example:
Free Variables: Avoid using free variables in this particular way.
Radical Solution: Disable saving entirely with
map_wants_save_disable 1
, and you won’t have to deal with these issues! :DThe text was updated successfully, but these errors were encountered: