Adding a field to a Type during runtime
BlitzMax Forums/BlitzMax Programming/Adding a field to a Type during runtime
| ||
Can I do this with Reflection? |
| ||
No. Not even without. |
| ||
Why do you need to do this? |
| ||
I wanted to outsource parameters for my gameobjects in ini files. Then I could simply add a new line to the ini file and the field would be created automatically. |
| ||
Type MyType Field Params:TList Field Values:TList End Mytype This might help. Or any variation upon this, like an array of "Object" or something like that. You may need/want to hold another array/list for the param types (PT_INT, PT_FLOAT, PT_OPBJECT, PT_MY_OTHER_TYPE, etc.) |
| ||
TList ? yeuck... Or some kind of associative array - like a TMap, where you automatically have the name/value paired relationship. Although at some point, don't you need to refer to the entry in some way? Which implies some code to refer to it? (unless of course you are also using scripting, in which case you can add new code on-the-fly too :-) |
| ||
Here is a simple example of Brucey's suggestion in practice.Strict Type TCustom Field _params:TMap = New TMap Method read_int:TInt(param:String) Return TInt(_params.ValueForKey(param)) End Method Method write_int(param:String, value:TInt) _params.Insert(param, value) End Method End Type Local player:TCustom = New TCustom player.write_int("score", TInt.Create(5)) player.write_int("lives", TInt.Create(3)) Print "Player has " + player.read_int("lives").value + " lives." Type TInt Field value:Int Function Create:TInt(v:Int) Local i:TInt = New TInt i.value = v Return i End Function End Type This solution might be slow if used for performance-sensitive areas of code. I wanted to outsource parameters for my gameobjects You would be better off just programming these fields in normally. What practical gain do you think this will provide? in ini files. Then I could simply add a new line to the ini file and the field would be created automatically. |
| ||
man i love php arrays..... i'll just leave it at that. |
| ||
Edit: Huh, that wasn't supposed to happen. |