Skip to content

Instantly share code, notes, and snippets.

@willnationsdev
Last active July 3, 2018 16:09
Show Gist options
  • Save willnationsdev/ada1cfe93d66dd60843fc021660c0634 to your computer and use it in GitHub Desktop.
Save willnationsdev/ada1cfe93d66dd60843fc021660c0634 to your computer and use it in GitHub Desktop.
Helping someone simplify code 1
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.
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())
@willnationsdev
Copy link
Author

I conjoined the replace / concatenation operations at the end of line 6,

@Willowblade
Copy link

I think the title should be "Helping someone reduce amount of code" instead 😄

@willnationsdev
Copy link
Author

@Willowblade

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