Skip to content

Instantly share code, notes, and snippets.

@AustinBrunkhorst
Created March 25, 2018 00:02
Show Gist options
  • Save AustinBrunkhorst/402f85b21539250532da613ec02bd00f to your computer and use it in GitHub Desktop.
Save AustinBrunkhorst/402f85b21539250532da613ec02bd00f to your computer and use it in GitHub Desktop.
C++ Reflection | Functions
struct SoundEffect
{
float volume;
void Load(const std::string &filename);
};
int main(void)
{
Type soundEffectType = typeof( SoundEffect );
Field volumeField = soundEffectType.GetField( "volume" );
// the runtime supports overloading, but by default returns the first overload
Method loadMethod = soundEffectType.GetMethod( "Load" );
// creates an instance of a sound effect
Variant effect = soundEffectType.Create( );
// effect.volume is now 85
volumeField.SetValue( effect, 85.0f );
// 85
volumeField.GetValue( effect );
// effect.Load is called
loadMethod.Invoke( effect, std::string { "Explosion.wav" } );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment