Adding a field to a Type during runtime

BlitzMax Forums/BlitzMax Programming/Adding a field to a Type during runtime

Volker(Posted 2010) [#1]
Can I do this with Reflection?


beanage(Posted 2010) [#2]
No. Not even without.


Warpy(Posted 2010) [#3]
Why do you need to do this?


Volker(Posted 2010) [#4]
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.


_JIM(Posted 2010) [#5]
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.)


Brucey(Posted 2010) [#6]
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 :-)


Czar Flavius(Posted 2010) [#7]
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
in ini files. Then I could simply add a new line to the ini file
and the field would be created automatically.
You would be better off just programming these fields in normally. What practical gain do you think this will provide?


Leon Drake(Posted 2010) [#8]
man i love php arrays..... i'll just leave it at that.


N(Posted 2010) [#9]
Edit: Huh, that wasn't supposed to happen.