New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More SAO related changes (revised) #9399
Conversation
df01330
to
d73b861
Compare
Can you please elaborate what this PR is good for? |
What does this mean, “unusable” and “invalid”? What happens when a rogue mod tries to use the object anyway? Does the ObjectRef magically become Whatever the answer is, this text needs improvement as I don't fully understand it. |
Yes, the rest is random bugfixes and other things that were necessary.
It means all methods return
This would be cool (or perhaps not), but no, that is not the case.
I briefly looked into that but that wasn't easy to do in the code. Calling object methods after
Possible bugs where? |
d73b861
to
c95f5d2
Compare
Ah. This definitely needs to go in the documention because if ALL functions can suddenly return nil (because a different mod removed the object to which I have a reference to), this will catch many mods by surprise, and is a bug factory. Please add a sentence somewhere that says something like: “Note: All these functions return There's another question about I propose to add an
Uh, forget it. My question has been answered. |
To clarify: I propose to add the “functions return |
But here's the thing. If that happens either you or the other mod has already made the fundamental mistake of storing ObjectRefs.
Yes.
I consider such defensive programming unnecessary and adding such a method could in fact cause the opposite. Unless you violate the scoping rules for ObjectRefs, there is absolutely no reason to test if an ObjectRef is valid because you can rely on it being valid. |
The documentation doesn't really say that this isn't allowed, right? :P
Today I heard for the first time that there are scoping rules. :D I always suspected something like that, but I can't remember it being written out explicitly anywhere, and I can't find it with a quick Ctrl-F in Lua API docs are suspiciously sparse about what you can (and can't) do with ObjectRefs. |
I have to agree with Wuzzy. To suggest that someone made the "fundamental mistake of xyz" when xyz isn't officially documented anywhere doesn't seem fair. I remember the first time I learned about this was on a random forum reply from Rubenwardy. People can't reasonably be expected to know every caveat of a complex system, only what they are told :P |
I was actually wrong, and just repeating what I was told. Storing objectrefs isn't that bad as they are gracefully cleared and won't have segfaults. However, you need to account for invalid objectrefs and they won't become valid again if the object is emerged |
Maybe not inside the ObjectRef code itself, but breaking implicit assumptions could certainly still segfault the engine until now (#9387) or 2016 (b8aed9d) or 2015 (ce8a9ed). This is also not the case for anonymous ObjectRefs, though I couldn't find where these are used or needed.
There is no API function for this (though several fulfill equivalent purpose) and not having one is preferable IMO. Unfortunately some mods (such as advtrains) are forced to touch internals like |
So what is a valid scope for ObjectRefs? I'm not sure what you even mean. Do you mean “scope” as in Lua? Please answer the questions in
But |
^ This statement right here. Please add that to the documentation. It would be extremely beneficial for modders to know. Full disclosure and all. |
Hmm, this is kind of a problem. Maybe this stance on ObjectRef reuse is not viable (and not necessary anyway since #9390). |
c95f5d2
to
f719530
Compare
Removed the |
f719530
to
7d1b685
Compare
Very thorough explanation! Thanks so much for adding! |
Just to make sure I understand this right: As long I use an ObjectRef inside the same callback ( |
Assuming "inside" and "outside" mean the same thing I am thinking of, then yes. |
Great! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trivial change needed, works.
Keeping the ObjectRefs around in a table isn't ideal and this allows removing the somewhat nonsensical is_player_connected() added in 86ef7147.
7d1b685
to
20186eb
Compare
@sfan5 Why is that? Why intentionally? A function to check if an object is still active would be extremely useful, but because it doesn't exist currently my hack is calling get_yaw(). But there's nothing, so ObjectRefs will keep being saved, recommended or not. |
(This answer focuses on LuaEntities, it should be obvious why there is zero need to store player ObjectRefs.)
The reason is that objects can unloaded and loaded again at any point, so your ObjectRef will be invalid despite the "same" object still existing. This may or may not be what you want and will come to a surprise on unsuspecting modders who store ObjectRefs.
My opinion is that until the engine provides a way to properly keep track of another object, working around this by storing ObjectRefs should be discouraged.
There is another way actually.
You can index |
You wrote that it is intentional that there's no method to test if an ObjectRef is valid. I don't mind if storing ObjectRefs is discouraged, but taking effort not to provide a method to test the validity is another story. |
Providing such a method would encourage storing ObjectRefs and also be a commitment to support that API in the future. |
Yes, that what I've been using, but checking object validity isn't their purpose and that behavior can change anytime on other merits. Anyway I don't see why there seems to be such a problem, checking validity is the first thing one learns when working with stored refs, that's no rocket science. |
Hmm, I'd argue that the get_luaentity() method would be a perfectly suitable and even coherent way to check whether an object is valid or not. If you can't get the LuaEntity, then the object must not exist. |
Various bug & documentation fixes related to entities.
DO NOT SQUASH
To do
This PR is a Ready for Review.