Last active
July 3, 2018 16:09
-
-
Save willnationsdev/ada1cfe93d66dd60843fc021660c0634 to your computer and use it in GitHub Desktop.
Helping someone simplify code 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
# you should rename all your bullet properties from "first_bullet" to "bullet_1", etc. | |
func _on_equipment_weapon_equipped(weapon_data, weapon_slot): | |
if weapon_data.id <= 0: return | |
get_parent().set("bullet_" + str(weapon_slot), Global_equipment.get(weapon_data.sub_type_a.replace("_weapon", "_ammo"))) | |
# This method relies entirely on naming conventions, so it is much more fragile to change. | |
# However, this is easily the simplest possible way of reducing the amount of code involved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
const bullets = [] # populate with "first_bullet", "second_bullet", etc. | |
const weapon_func_map = { | |
# setup getters for the *_ammo properties in the global | |
"energy_weapon": funcref(Global_equipment, "get_energy_ammo"), | |
"ranged_energy_weapon": funcref(Global_equipment, "get_ranged_energy_ammo" | |
} | |
func _on_equipment_weapon_equipped(weapon_data, weapon_slot): | |
if weapon_data.id <= 0: return | |
# this is ultimately a much more data-driven approach since the code | |
# will now still work even if the underlying data changes content. | |
for a_bullet in bullets: | |
get_parent().set(a_bullet, weapon_func_map[weapon_data.sub_type_a].call_func()) | |
# if you DID use a naming convention for the bullets, then you could do... | |
for i in range(num_bullets) | |
get_parent().set("bullet_" + str(i + 1), weapon_func_map[weapon_data.sub_type_a].call_func()) |
I think the title should be "Helping someone reduce amount of code" instead 😄
lol, so true. It's really terrible code. But he wanted conciseness! XD
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I conjoined the replace / concatenation operations at the end of line 6,