Skip to content
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

Allow overriding how scripted objects are converted to strings #27886

Merged
merged 1 commit into from
May 20, 2019
Merged

Allow overriding how scripted objects are converted to strings #27886

merged 1 commit into from
May 20, 2019

Conversation

LeonardMeagher2
Copy link
Contributor

@LeonardMeagher2 LeonardMeagher2 commented Apr 10, 2019

solves #26796

  • ADD String to_string() method to Object which can be overriden by scripts
  • ADD String to_string(r_valid) method to ScriptInstance to allow langauges to control how scripted objects are converted to strings
  • IMPLEMENT to_string for GDScriptInstance, VisualScriptInstance, and NativeScriptInstance
  • ADD Documentation about Object.to_string
  • Changed Variant::operator String to use obj->to_string()

@akien-mga
Copy link
Member

Please edit the commit log to be more explicit, the first line of a Git commit log is it's title and should be a short but clear indication of what the commit does.

core/variant.cpp Outdated Show resolved Hide resolved
@LeonardMeagher2 LeonardMeagher2 changed the title Allow overriding how objects are converted to strings Allow overriding how scripted objects are converted to strings Apr 10, 2019
@LeonardMeagher2
Copy link
Contributor Author

LeonardMeagher2 commented Apr 10, 2019

oops forgot to add my code

Copy link
Contributor

@neikeq neikeq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should cache the StringName for "to_string" (check core_string_names.cpp). Otherwise, it looks good.

@LeonardMeagher2
Copy link
Contributor Author

@neikeq, I'll get this done during my lunch break today

@Calinou
Copy link
Member

Calinou commented Apr 10, 2019

Should the method be named to_string or _to_string, considering it's a function that can be overridden?

@neikeq
Copy link
Contributor

neikeq commented Apr 10, 2019

@Calinou There are inconsistencies about this. Have a look at #19209.

@LeonardMeagher2
Copy link
Contributor Author

LeonardMeagher2 commented Apr 10, 2019

@Calinou I felt like omitting the _ made sense regardless of it being overridden since it always returns something. You can do obj.to_string() and I wouldn't expect people to do obj._to_string() in their code.

My perspective about using the underscore is it appears most virtual functions that don't have the underscore are meant to act like an interface where you can request some information or tell it to do something, and virtual functions that have an underscore are usually responding to or expecting input from some process. It's all very confusing haha

@LeonardMeagher2
Copy link
Contributor Author

LeonardMeagher2 commented Apr 10, 2019

@neikeq I've added to_string to CoreNameStrings hopefully I did that right.

Edit:
Looks like I'll need to change what line I add the core_string_names header according to the clang-format rules

@LeonardMeagher2
Copy link
Contributor Author

Ran clang-format, we should be good to go now.

mhilbrunner
mhilbrunner previously approved these changes Apr 15, 2019
Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be bound with BIND_VMETHOD so that it is properly identified as virtual in the documentation (see _get_property_list in object.cpp, or all the virtual callbacks in node.cpp).

The documentation should also be improved to mention that the method can be overridden from script to customize the String representation of objects. And I would describe what the default representation means ([name:rid]).

Finally, I know that it's not done consistently but I think we do favor putting a leading underscore on virtual methods that can be overridden. See e.g. how get_minimum_size calls _get_minimum_size from scripts if overridden in control.cpp. So you would still call to_string() in your code (or it will be called behind the hood by print()), but you can customize it by overriding _to_string().

@LeonardMeagher2
Copy link
Contributor Author

@akien-mga I've implemented you requested changes.

Copy link
Contributor

@karroffel karroffel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NativeScript parts looks fine

@akien-mga akien-mga dismissed their stale review April 30, 2019 08:43

Changes done.

doc/classes/Object.xml Outdated Show resolved Hide resolved
doc/classes/Object.xml Outdated Show resolved Hide resolved
doc/classes/Object.xml Outdated Show resolved Hide resolved
Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems mostly good to me, just a few nitpicks on documentation.

solves #26796

- ADD `String to_string()` method to Object which can be overriden by `String _to_string()` in scripts
- ADD `String to_string(r_valid)` method to ScriptInstance to allow langauges to control how scripted objects are converted to strings
- IMPLEMENT to_string for GDScriptInstance, VisualScriptInstance, and NativeScriptInstance
- ADD Documentation about `Object.to_string` and `Object._to_string`
- Changed `Variant::operator String` to use `obj->to_string()`
@LeonardMeagher2
Copy link
Contributor Author

LeonardMeagher2 commented May 3, 2019

@akien-mga I've made changes to the documentation. Let me know if you'd prefer a better example, or that I use this kind of wording.

which will be used both when calling [method to_string] manually but also in [method @GDScript.print] and similar.

@akien-mga akien-mga merged commit defd960 into godotengine:master May 20, 2019
@akien-mga
Copy link
Member

Thanks a lot for your contribution! :)

@LeonardMeagher2
Copy link
Contributor Author

Amazing! thank you @akien-mga

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants