POV-Ray : Newsgroups : povray.newusers : How to use #Macro like a function ? Server Time
1 Nov 2024 06:16:54 EDT (-0400)
  How to use #Macro like a function ? (Message 1 to 7 of 7)  
From: Gyscos
Subject: How to use #Macro like a function ?
Date: 29 Jun 2010 14:35:00
Message: <web.4c2a3bf6d73f9cb9e3006550@news.povray.org>
Hi !

I would like to make some functions. But I found POV-Ray's functions very
limited : it seems it is not possible to use directives inside.
So I decided to use macro instead, since I want to use them once at parse time
only.

From what I had understood, the #macro works pretty much as if it replaced the
call with the content of the macro.

However, I'm trying to make macro that return a float value, so I tried
something like :

#macro MyMacro(arg)

  #if (arg > 1)
    5
  #else
    10
  #end

#end

But it doesn't work...
I saw some exemples like that :


#macro MyMacro(arg)

  #if (arg > 1)
    #declare X=5
  #else
    #declare X=10
  #end

#end

But it doesn't seem right, I don't like toying with global var from inside
functions like this, I feel like I could overwrite some previous val...

Is there a clean way to do this kind of things ?
Thanks :)


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: How to use #Macro like a function ?
Date: 29 Jun 2010 14:45:07
Message: <4c2a3f33$1@news.povray.org>
On 29.06.10 20:31, Gyscos wrote:
> Is there a clean way to do this kind of things ?
> Thanks :)

You might want to try the documentation: "2.2.2.8.4 Returning a Value Like a 
Function" <http://www.povray.org/documentation/view/3.6.1/243/>

Also, check the sample files and includes provided with POV-Ray, they 
contain lots and lots of examples.

	Thorsten


Post a reply to this message

From: Gyscos
Subject: Re: How to use #Macro like a function ?
Date: 29 Jun 2010 15:40:00
Message: <web.4c2a4b875abfbec8e3006550@news.povray.org>
Wow, I understand what didn't work...
Well, partly...

I used the macro in a declare :

#declare MyVar = MyMacro(MyStuff);

But POV-Ray parsed the macro and found some #if and #else BEFORE the expected
semi-colon...

:-/

So I have to put the semi-colon IN the macro, and not in the #declare line...
:(

I think I'll use extra arguments to return the value instead...


Post a reply to this message

From: clipka
Subject: Re: How to use #Macro like a function ?
Date: 29 Jun 2010 16:09:49
Message: <4c2a530d$1@news.povray.org>
Am 29.06.2010 21:37, schrieb Gyscos:
> Wow, I understand what didn't work...
> Well, partly...
>
> I used the macro in a declare :
>
> #declare MyVar = MyMacro(MyStuff);
>
> But POV-Ray parsed the macro and found some #if and #else BEFORE the expected
> semi-colon...
>
> :-/
>
> So I have to put the semi-colon IN the macro, and not in the #declare line...
> :(
>
> I think I'll use extra arguments to return the value instead...

This one will do:

   #macro MyMacro(arg)
     #if (arg > 1)
       #local RetVal = 5;
     #else
       #local RetVal = 10;
     #end
     RetVal
   #end

Note that the value to be returned is stored in a local variable first, 
which is then "returned" at the very end of the macro.

Extra arguments as return values are pretty cumbersome, as you'll always 
have to make sure that the respective variable is defined properly 
before being passed to (and back from) the macro.


Post a reply to this message

From: Gyscos
Subject: Re: How to use #Macro like a function ?
Date: 30 Jun 2010 01:25:00
Message: <web.4c2ad3fd5abfbec8e3006550@news.povray.org>
Wow, cool :)


Post a reply to this message

From: kurtz le pirate
Subject: Re: How to use #Macro like a function ?
Date: 2 Jul 2010 08:35:32
Message: <4c2ddd14@news.povray.org>

news:4c2a530d$1@news.povray.org...
> Am 29.06.2010 21:37, schrieb Gyscos:

> ... #macro MyMacro(arg)
>     #if (arg > 1)
>       #local RetVal = 5;
>     #else
>       #local RetVal = 10;
>     #end
>     RetVal
>   #end
> ...

#macro MyMacro(arg)
    (arg>1)?5:10
#end



-- 
klp


Post a reply to this message

From: kurtz le pirate
Subject: Re: How to use #Macro like a function ?
Date: 3 Jul 2010 02:40:19
Message: <kurtzlepirate-7D45E3.08401803072010@news.povray.org>
In article <4c2ddd14@news.povray.org>,
 "kurtz_le_pirate" <kur### [at] yahoofr> wrote:

> #macro MyMacro(arg)
>     (arg>1)?5:10
> #end

ooops...

  #macro MyMacro(arg)
    ((arg>1)?5:10)
  #end
  

-- 
klp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.