Page 1 of 1 1
Topic Options
#122435 - 2004-07-08 11:21 PM Creating a UDF function
Microcyb Offline
Getting the hang of it

Registered: 2002-12-30
Posts: 95
Loc: Hell
OK first attempt here to make a custom function.

What I am trying to do is populate mutiple lines with random values, and trying to create a function, I recieve no values.

Rays 0, 30, 10, 3.0, 0x00EFEFFF, 1.0
Rays 3, 25, 8, 2.5, 0x00EFEFFF, 1.9

Code:
 

srnd(@TICKS*@MSECS*2)
random_var($NumPolys,$NumStreaks,$OuterRadius,$InnerOpacity,$OuterOpacity,$ColourTemp)



WriteLine(5, '# Rays D, NumPolys, NumStreaks, OuterRadius, Colour, ColourTemp'+Chr(13))
WriteLine(5, 'Rays '+ $NumPolys + ', '+ $NumStreaks +', '+ $OuterRadius +', '+ $OuterOpacity +', 0x00'+ $KeyColor[$dd] +', '+ $ColourTemp +''+Chr(13))
WriteLine(5, Chr(13))



function random_var($NumPolys,$NumStreaks,$OuterRadius,$InnerOpacity,$OuterOpacity,$ColourTemp)
$dd = 5 * rnd(30)
formatnumber($NumPolys,0.00) = 0.0
$NumPolys = 1.1 * rnd(10)
$NumStreaks = rnd(20)
$OuterRadius = rnd(10)
$InnerOpacity = 1.1 * rnd(10)
$OuterOpacity = 1.1 * rnd(10)
endfunction




Edited by Microcyb (2004-07-08 11:22 PM)
_________________________
www.microcyb.com

Top
#122436 - 2004-07-08 11:32 PM Re: Creating a UDF function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Not even close.

Did you read the FAQ?
How to write a UDF

You are trying to pass a bunch of empty vars expecting them to somehow be returned. That is not how it works.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122437 - 2004-07-09 02:24 AM Re: Creating a UDF function
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
This is closer to being a proper function. It returns an array of values.

$ArrayOfValues = Random_Var()

Code:
for each $value in Random_Var()

? $value
next

function Random_Var()

Dim $NumPolys,
$NumStreaks,
$OuterRadius,
$InnerOpacity,
$OuterOpacity,
$dd

srnd(@TICKS*@MSECS*2)
$dd = 5 * rnd(30)
$NumPolys = formatnumber(1.1 * rnd(10),2)
$NumStreaks = rnd(20)
$OuterRadius = rnd(10)
$InnerOpacity = 1.1 * rnd(10)
$OuterOpacity = 1.1 * rnd(10)
$Random_Var = $NumPolys,$NumStreaks,$OuterRadius,$InnerOpacity,$OuterOpacity
endfunction





Edited by Howard Bullock (2004-07-09 06:04 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#122438 - 2004-07-09 05:37 AM Re: Creating a UDF function
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
KiXtart does not support ByRef parameters, only ByVar, thus it can only return parameters as part of the function result.
_________________________
There are two types of vessels, submarines and targets.

Top
#122439 - 2004-07-09 03:31 PM Re: Creating a UDF function
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
you are close....

Code:

random_var()

WriteLine(5, '# Rays D, NumPolys, NumStreaks, OuterRadius, Colour, ColourTemp'+Chr(13))
WriteLine(5, 'Rays '+ $NumPolys + ', '+ $NumStreaks +', '+ $OuterRadius +', '+ $OuterOpacity +', 0x00'+ $KeyColor[$dd] +', '+ $ColourTemp +''+Chr(13))
WriteLine(5, Chr(13))

function random_var()
dim $dd
$dd = 5 * rnd(30)
formatnumber($NumPolys,0.00) = 0.0
$NumPolys = 1.1 * rnd(10)
$NumStreaks = rnd(20)
$OuterRadius = rnd(10)
$InnerOpacity = 1.1 * rnd(10)
$OuterOpacity = 1.1 * rnd(10)
endfunction


Top
#122440 - 2004-07-09 04:18 PM Re: Creating a UDF function
Microcyb Offline
Getting the hang of it

Registered: 2002-12-30
Posts: 95
Loc: Hell
Ah thank you.
Going to now sit down and make sure I follow the manual and the examples you all gave.
_________________________
www.microcyb.com

Top
#122441 - 2004-07-09 04:32 PM Re: Creating a UDF function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Close you say? What about scope? Debatable as a best practice.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122442 - 2004-07-09 05:58 PM Re: Creating a UDF function
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Quote:

Close you say? What about scope? Debatable as a best practice.




I dont understand what you mean.....

looks to me like he has a set number of global variables that he wants to randomize in the fly. a function used in the above method is perfect for that. A function does not always have to return data. It is cheap efficiant use of code.

Bryce

Top
#122443 - 2004-07-12 10:14 AM Re: Creating a UDF function
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It's a question of good coding practice.

Functions should (as far as possible) be black boxes. You supply input, you get output. All variables used within the function should be in the local scope - there are a couple of exceptions but this rule should hold generally true.

Having the UDF change the environment of the parent process is generally a bad idea, as it leads to hard to diagnose faults and makes the code far less portable. It also forces the parent process to declare variables in the global scope.

Howards solution safely returns the multiple values as an array, which is a little trickier to handle but avoids the scope issues. I'd add some helper constants to define what the array elements held to make the code easier to read when they are referenced.

In this case if the values are only every going to be used to write the line to a file a single formatted string could have been returned instead, but it's not clear if this is the case.

Top
#122444 - 2004-07-12 10:25 AM Re: Creating a UDF function
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, ja, black boxes.
I vote for that.

http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=83493&page=0&view=collapsed&sb=5&o=186&fpart=1
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.152 seconds in which 0.08 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org