Change all var to camel case

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AkMor

Is there a way to change all snake cases to camel cases for variables and node names in the whole project?
We initially didn’t decide on conventions, and the naming conventions are starting to mix.

I am not familiar with what a snake and camel case is, but if you have script open in the Editor, you do a Find/Replace for all scripts in the document by doing:

  • Click “Search”
  • Click “Find in Files…” in the drop down menu
  • Enter your search string in the ‘Find’ field
  • Select “Replace…”
  • and then enter your replacement text to replace all occurrences

Doing the above approach blindly is dangerous, since you might introduce undetected syntax issues (or worse, logic error that don’t make a compilation error) in your scripts. So I suggest you review the preview pane at the bottom before replacing everything (it will show you all text instances that match your query that will be replaced)

godot_dev_ | 2022-08-05 19:52

Thank you, this is useful up to a couple hundred variables since I have to check each variable if it is safe to change, but it becomes unfeasible if there are thousands or even tens of thousands of variables. I wish there were a option in the godot UI to change the cases, or a script or vim-macro that does it automatically.

AkMor | 2022-08-06 09:50

:bust_in_silhouette: Reply From: wyattb

Google how to do godot refactoring. Godot does not have a built in ability to do that as far as i know but you may get some ideas.

This is unfeasible when there are tens of thousands of variables. The only thing I need is to change it automatically to camel cases, not refactor the whole project.

AkMor | 2022-08-06 19:54

Renaming variables is considered refactoring. If you have thousands then it’s worth writing a script (you can use godot) to locate all the snake variables and replace with camel case.

What I would do, is create a script that reads the gd files line by line looking for var and creating a dictionary sorted descending order. Then using the dictionary find all the matching variables in the gd code and replace with camelcase outputting to another file.

You can use regex class to make it easier to find the string variables. RegEx — Godot Engine (stable) documentation in English

Then repeat for other files such as tscn and project.godot

wyattb | 2022-08-06 21:24

That worked, thank you.

AkMor | 2022-08-07 08:45