Forum Replies Created
-
AuthorPosts
-
January 22, 2019 at 7:18 am in reply to: Reflection on constructors.. How to access method of new variant? #15991
Could you use the following?
instance:=ObjectFromName<class_name>(“class_name”)Creating an object just from a string, without telling Monkey2 what it is, is probably very hard if possible at all.
January 22, 2019 at 6:03 am in reply to: Reflection on constructors.. How to access method of new variant? #15988You can store any Object in a Variant, and because of that you have to tell Monkey2 what it is.
With casting you can tell Monkey2 that instance.a is an instance of class A:
[/crayon]Monkey123456789101112131415161718[crayon-5cb9b54b06b94304358769 inline="true" ]Class AMethod Test()Print "Success!"EndEndClass BField a:VariantEndFunction Main()Local instance:=New Binstance.a=New ACast<A>(instance.a).Test()EndUsing that way you could use different classes that implement the same methods:
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839[crayon-5cb9b54b06b9a990384241 inline="true" ]Interface ITestMethod Test()EndClass A Implements ITestMethod Test()Print "Success A!"EndEndClass B Implements ITestMethod Test()Print "Success B!"EndEndClass C Implements ITestMethod Test()Print "Success C!"EndEndClass TheClassField a:VariantEndFunction Main()Local instance:=New TheClassinstance.a=New ACast<ITest>(instance.a).Test()instance.a=New BCast<ITest>(instance.a).Test()instance.a=New CCast<ITest>(instance.a).Test()EndSame on macOS using monkey2/modules/reflection/tests/alltypes.monkey2
123456789101112131415Mx2cc version 1.1.15***** Making app '/Applications/Monkey2-v2018.09/modules/reflection/tests/alltypes.monkey2' (macos release x64 gcc mx) *****Parsing...Semanting...Translating...Compiling...Build error: System command failed:g++ -c -std=c++14 -mmacosx-version-min=10.9 -Wno-deprecated-declarations -Wno-tautological-pointer-compare -Wno-undefined-bool-conversion -Wno-int-to-void-pointer-cast -Wno-inconsistent-missing-override -Wno-logical-op-parentheses -Wno-parentheses-equality -O3 -DNDEBUG -I"/Applications/Monkey2-v2018.09/modules/" -I"/Applications/Monkey2-v2018.09/modules/monkey/native" -I"/Applications/Monkey2-v2018.09/modules/reflection/tests/" -I"/Applications/Monkey2-v2018.09/modules/bullet/bullet3-2.85.1/src/" -I"/Applications/Monkey2-v2018.09/modules/litehtml/litehtml/include/" -I"/Applications/Monkey2-v2018.09/modules/freetype/freetype-2.6.3/include/" -I"/Applications/Monkey2-v2018.09/modules/sdl2/SDL/include/" -I"/Applications/Monkey2-v2018.09/modules/zlib/zlib-1.2.11/" -DNDEBUG=1 -DBB_THREADS=1 -I"/Applications/Monkey2-v2018.09/modules/reflection/tests/alltypes.buildv1.1.15/macos_release_mx/build/" -MMD -MF"/Applications/Monkey2-v2018.09/modules/reflection/tests/alltypes.buildv1.1.15/macos_release_mx/build/r3e4eb90.cpp_r.deps" -o "/Applications/Monkey2-v2018.09/modules/reflection/tests/alltypes.buildv1.1.15/macos_release_mx/build/r3e4eb90.cpp_r.o" "/Applications/Monkey2-v2018.09/modules/mojox/mojox.buildv1.1.15/macos_release_mx/include/_r.cpp"[...]507 warnings and 10 errors generated.A generic class with an array would look like this:
Monkey123456789101112131415161718192021#Import "<monkey>"Using monkey..Class Name<T>Field arr:T[]EndFunction Main()Local i := New Name<Int>i.arr = New Int[10]Local f := New Name<Float>f.arr = New Float[10]Local b := New Name<Byte>b.arr = New Byte[10]EndYou can use the type Variant to set it to whichever object you want.
Monkey123456789101112131415161718192021222324252627282930313233343536373839#Import "<monkey>"Using monkey..Class NameField arr:Variant[]Field anything:VariantEndFunction Main()Local c := New Namec.arr = New Variant[10]c.anything = 11Print Int( c.anything )c.anything = "String"Print String( c.anything )c.anything = New UByte[10]c.arr[0] = New Int[10]c.arr[1] = New Float[4]c.arr[2] = New String[5]c.arr[0].SetArrayElement( 5, 15)Local len := c.arr[0].GetArrayLength()For Local i := 0 Until lenPrint Int( c.arr[0].GetArrayElement( i ) )NextEnd@jondecker76: Could it be that your expected results comes from routines that use SHA256() on ASCII strings? All std.digest functions in Monkey2 use 16-bit Unicode strings.
The SHA(), MD5(), SHA256() with Unicode string buffers are probably much different from using the same functions with ASCII buffers.
Strings in Monkey2 are 16-bit Unicode Strings.
Please try the following code to catch ASCII strings from DataBuffer and convert it to Unicode-Monkey2-Strings.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#Import "<std>"Using std..Function Main()Local db:DataBuffer=New DataBuffer(256)For Local i:=0 To 255db.PokeUByte(i,i)NextLocal test:=db.PeekString(0, StringFormat.ASCII)For Local i:= 0 To 255Print test[i] + " : " + Hex(test[i])NextEndEnum StringFormatASCII' UTF8' UTF16EndClass DataBuffer ExtensionMethod PeekString:String( offset:Int, type:StringFormat )Local ret:String = ""Select typeCase StringFormat.ASCIILocal len:Int = Self.Length - offsetIf len < 1 Then Return ""For Local i:Int = 0 Until lenret += String.FromChar( Self.PeekUByte(offset+i) )NextEnd SelectReturn retEndMethod PeekString:String( offset:Int, count:Int, type:StringFormat )Local ret:String = ""Local len :IntSelect typeCase StringFormat.ASCIIlen = Self.Length - offsetlen = Min(len,count)If len < 1 Then Return ""For Local i:Int = 0 Until lenret += String.FromChar( Self.PeekUByte(offset+i) )NextEnd SelectReturn retEndEnd0 To 255 requires a DataBuffer of 256 bytes.
Updated the code:
- added _updateInterval
If redrawing the resized window in realtime becomes too slow, we can now use ‘_updateInterval’ to change how often the window content is completely refreshed.
Monkey12_updateInterval = 1 ' window is updated at every resize step_updateInterval = 5 ' window content is only resized every 5th step, the other 4 steps the content is stretchedIf you are looking for a BlitzMax forum, it is at SyntaxBomb now.
The old BlitzMax forum is available read-only at mojolabs.
It is possible if you use your own class that extends Map.
With simple-type Arrays it is possible to initialize when declaring it:
Monkey123456789101112131415161718192021222324252627282930313233343536#Import "<std>"Using std..Global intArr1D := New Int[] ( 9, 8, 7, 6 )Global intArr2D := New Int[][] (New Int[]( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ),New Int[]( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ),New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 8, 8, 8, 8, 0, 0, 0 ),New Int[]( 0, 0, 8, 0, 0, 0, 0, 8, 0, 0 ),New Int[]( 0, 8, 0, 0, 0, 0, 0, 0, 8, 0 ),New Int[]( 8, 0, 0, 0, 0, 0, 0, 0, 0, 8 ) )Global strArr1D := New String[] ( "s1", "s2", "s3", "s4" )Global strArr2D := New String[][] (New String[]("a1", "a2"),New String[]("b1", "b2"),New String[]("c1", "c2") )Function Main()Print intArr1D[ 2 ] ' 7Print intArr2D[ 0 ][ 7 ] ' 7Print intArr2D[ 1 ][ 1 ] ' 8Print strArr1D[ 2 ] ' s3Print strArr2D[ 2 ][ 1 ] ' c2EndNow we can use that principle and write a generic class that extends Map<K,V>, let’s call it MapEx<K,V>.
We then add a constructor (“New”) that takes an array of Value+Key pairs for initializing the Map.
That would look like this:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#Import "<std>"Using std..Global test:= New MapEx<String,String>(New MapExPair<String,String> [](New MapExPair<String,String>("test1","Hello 1"),New MapExPair<String,String>("test2","Hello 2") ) )Function Main()Print test["test1"]Print test["test2"]Print test["test3"]Local newMap1 := MapEx<String,Int>.Create()Local newMap2 := MapEx<String,Int>.Create(New MapExPair<String,Int>[](New MapExPair<String,Int>("value 1",12) ) )Print newMap2["value 1"]End'' MapExPair is a pair of Key + Value'Class MapExPair<K,V>Field key:KField value:VMethod New( _key:K, _value:V )key = _keyvalue = _valueEndEnd'' Extended Map class with Initializers'Class MapEx<K,V> Extends Map<K,V>Method New()Super.New()EndMethod New( values:MapExPair<K,V>[] )For Local value := Eachin valuesSelf[value.key] = value.valueNextEndFunction Create:MapEx<K,V>()Return New MapEx<K,V>()EndFunction Create:MapEx<K,V>( values:MapExPair<K,V>[] )Return New MapEx<K,V>( values )EndEndOkay, that works. But that constructs for initializing the Map are very big.
By using two ‘Alias’ we can shorten it:
Monkey12Alias MyMap:MapEx<String,String>Alias MyPair:MapExPair<String,String>With those two Alias, we can now write a simpler global Map initialization:
Monkey12345Global test:= New MyMap( New MyPair[] (New MyPair("test1","Hello 1"),New MyPair("test2","Hello 2"),New MyPair("test3","Hello 3"),New MyPair("test4","Hello 4") ) )Looks better, doesn’t it?
Here the final code:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#Import "<std>"Using std..Alias MyMap:MapEx<String,String>Alias MyPair:MapExPair<String,String>Global test:= New MyMap( New MyPair[] (New MyPair("test1","Hello 1"),New MyPair("test2","Hello 2"),New MyPair("test3","Hello 3"),New MyPair("test4","Hello 4") ) )Function Main()Print test["test1"]Print test["test2"]Print test["test3"]Print test["test4"]Local newMap1 := MyMap.Create()Local newMap2 := MyMap.Create(New MyPair[](New MyPair("value 1","string 1") ) )Print "-----"Print newMap2["value 1"]End'' MapExPair is a pair of Key + Value'Class MapExPair<K,V>Field key:KField value:VMethod New( _key:K, _value:V )key = _keyvalue = _valueEndEnd'' Extended Map class with Initializers'Class MapEx<K,V> Extends Map<K,V>Method New()Super.New()EndMethod New( values:MapExPair<K,V>[] )For Local value := Eachin valuesSelf[value.key] = value.valueNextEndFunction Create:MapEx<K,V>()Return New MapEx<K,V>()EndFunction Create:MapEx<K,V>( values:MapExPair<K,V>[] )Return New MapEx<K,V>( values )EndEndHi,
loading a Mesh and a Texture is basically easy:
Monkey1234567891011121314151617181920212223242526272829#Import "<mojo3d>"#Import "<mojo3d-loaders>" ' for extended file formats'' Load a mesh'Local mesh:Mesh = Mesh.Load( "asset::model.obj" )If mesh <> Null'' mesh loaded'' now create a material/texture for it.'' load a texture:Local material:PbrMaterial = PbrMaterial.Load( "asset::texture.jpg" )' or create a material yourself, if you don't have a texture'material = New PbrMaterial( Color.Red, .1, .1 )' change the material properties if you like'material.MetalnessFactor = 0.1'material.RoughnessFactor = 0.1'material.ColorFactor = Color.Yellow'material.AmbientFactor = Color.Chromium'' Create a Model from mesh + material'Local model:Model = New Model( mesh, material )'' READY.'EndIfIn short:
Monkey123456#Import "<mojo3d>"#Import "<mojo3d-loaders>" ' for extended file formatsLocal mesh := Mesh.Load( "asset::model.obj" )Local material := PbrMaterial.Load( "asset::texture.jpg" )Local model := New Model( mesh, material )This seems to be outdated.
It is not synchronized to Monkey2:
This branch is 28 commits ahead, 72 commits behind blitz-research:develop.Would it be a problem to make pull requests directly to blitz-research/monkey2?
Okay, this .md2 model has no useable animations in it.
Maybe the file format is too old.Here is a test with 3 working models:
– http://monkeycoder.co.nz/forums/topic/loading-animated-3d-models/ -
AuthorPosts