It is currently Fri May 24, 2013 9:59 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Help converting a function from bmax to pb?
PostPosted: Sat Mar 10, 2012 8:51 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
I don't understand bmax syntax and i need a function converted so that i can use the leadwerks engine in pb.
The function converts a string to a vector. Can someone at least help convert part of it, i may be able to figure out the rest.

Code:
Function StringToVec3:TVec3(text:String, scale:Float = 1.0)
   Local t:TVec3 = Vec3(1)
   Local sarr:String[]
   sarr = text.split(",")
   If sarr
      If sarr.length > 0 t.x = Float(sarr[0]) * scale
      If sarr.length > 1 t.y = Float(sarr[1]) * scale
      If sarr.length > 2 t.z = Float(sarr[2]) * scale
   EndIf
   Return t
EndFunction


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sat Mar 10, 2012 9:11 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
They are passing numbers in comma separated strings --> "1.1,2.2,3.3"
Use the necessary PB functions( StringField(), len() ) to extract them.
You also have to return a pointer to the vector structure, not the structure.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sat Mar 10, 2012 9:23 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
Thanks, the problem is, the string that needs to be converted is Ä. Does this mean the command returning the string is not working?


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sat Mar 10, 2012 9:25 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
This is the structure in the leadwerks include file
Code:
Structure TVec3
  X.f
  Y.f
  Z.f
EndStructure


this is the procedure in the include file
Code:
Procedure Vec3(*Result.Tvec3, px.f = 0, py.f = #MAX_DEC, pz.f = #MAX_DEC)
  With *Result
    \X = px
    If py = #MAX_DEC
      \Y = px
    Else
      \Y = py
    EndIf
    If pz = #MAX_DEC
      \Z = px
    Else
      \Z = pz
    EndIf
  EndWith
  ProcedureReturn *Result
EndProcedure


and this is the command that returns the string
Code:
LEGetEntityKey(entity.i, hkey.s, defaultvalue.s = "")


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sat Mar 10, 2012 9:45 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
Ok, that is ascii 196. Are you compiling with unicode on?
It could be since:
Code:
If sarr.length > 0 t.x = Float(sarr[0]) * scale

They might only pass a scalar? Meaning, no Y or Z component.
So, 196 would get scaled and returned in \X.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sat Mar 10, 2012 10:03 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
unicode is definitely off


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sun Mar 11, 2012 12:57 am 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 932
Location: New York
This is not an exact "one-to-one" conversion, but it should give the same result.

Note as Skywalk said, it returns a pointer to a new vector, which is different from actually returning an object itself (in an object oriented language).

Code:
; 3D vector structure

Structure TVec3
  X.f
  Y.f
  Z.f
EndStructure


Procedure.i StringtoVec3(text.s, scale.f = 1.0)
  Protected *t.TVec3, s.s
 
  ; Allocate and pre-definte a new vector
  *t   = AllocateMemory(SizeOf(TVec3))
  *t\X = 1.0
  *t\Y = 1.0
  *t\Z = 1.0
 
  ; Extract x field
  s = StringField(text, 1, ",")
  If (s) : *t\X = ValF(s) * scale : EndIf
 
  ; Extract y field
  s = StringField(text, 2, ",")
  If (s) : *t\Y = ValF(s) * scale : EndIf
 
  ; Extract z field
  s = StringField(text, 3, ",")
  If (s) : *t\Z = ValF(s) * scale : EndIf
 
  ; Return pointer to new vector
  ProcedureReturn *t
EndProcedure



; Create and display an example vector

*myVec3.TVec3 = StringtoVec3("5.4,3.2,1.0", 2.0)

Debug "x y z ="
Debug StrF(*myVec3\X, 1)
Debug StrF(*myVec3\Y, 1)
Debug StrF(*myVec3\Z, 1)


It expects a string of 1 to 3 numbers separated by commas... if your other function is returning a single non-numeric character, then something is probably wrong there...


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sun Mar 11, 2012 12:24 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
Thanks for your efforts, it's not working because of the string returned.


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sun Mar 11, 2012 12:49 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1445
Location: Germany
Hi,

I think you have to use 'Static' instead of 'Protected' for t.
Else it makes no sense to return a pointer on it.

Bernd


Top
 Profile  
 
 Post subject: Re: Help converting a function from bmax to pb?
PostPosted: Sun Mar 11, 2012 5:07 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
Bernd, since PB does not clear AllocateMemory() in Procedures, the memory lives on!

If you are not using the return value directly in an expression, pass the Structure in the parameters list so you don't have to worry about memory leaks. :wink:

Code:
Structure TVec3
  X.f
  Y.f
  Z.f
EndStructure
; Safer code and no Global or memory leak.
Procedure.i StringtoVec3_1(text.s, *t.TVec3, scale.f = 1.0)
  *t\X = ValF(StringField(text, 1, ",")) * scale  ; No need for If len(text)!
EndProcedure
Define myV.TVec3
Debug StrF(myV\X)
StringtoVec3_1("5.4,3.2,1.0", @myV, 2.0)
Debug StrF(myV\X)

; Requires Memory Management or possible global variable
Procedure.i StringtoVec3_2(text.s, scale.f = 1.0)
  Protected *t.TVec3 = AllocateMemory(SizeOf(TVec3))
  *t\X = ValF(StringField(text, 1, ",")) * scale  ; No need for If len(text)!
  Debug "memaddr = " + Str(*t)
  ProcedureReturn *t    ; this memory is not cleared so it lives on!
EndProcedure
Define *myV.TVec3 = AllocateMemory(SizeOf(TVec3))
Debug "memaddr = " + Str(*myV)
Debug StrF(*myV\X)
*myV = StringtoVec3_2("5.4,3.2,1.0")
Debug StrF(*myV\X)

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye