[PD-dev] help coding an object, I need it cause I don't know better

Alexandre Torres Porres porres at gmail.com
Tue Oct 10 03:53:27 CEST 2023


Ok, I solved it by just using the value functions that, as it turns out,
are available in m_pd.h :)

it all works, but I wanted to expand the object and also make it take
symbols, which won't be possible now, but it will be if [value] is expanded
to take symbols as well, and there's a PR for it, so I can then annoyingly
insist on it instead.

This works better as the external now is perfectly integrated with [value]
and can also take values that are set in [expr] for instance.

here's the code, pretty much simpler now as well. Thanks!

// porres 2023


#include "m_pd.h"


*static* t_class *var_class;


*typedef* *struct* _var{

    t_object    x_obj;

    t_int       x_n;    // number of given vars as arguments

    t_symbol  **x_sym;  // variable names

}t_var;


*static* *void* var_bang(t_var *x){

    t_atom at[x->x_n];

    *for*(*int* i = 0; i < x->x_n; i++){

        t_float f;

        *int* flag = value_getfloat(x->x_sym[i], &f);

        SETFLOAT(at+i, !flag ? f : 0);

    }

    outlet_list(x->x_obj.ob_outlet, &s_list, x->x_n, at);

}


*static* *void* var_list(t_var *x, t_symbol *s, *int* ac, t_atom *av){

    s = *NULL*;

    *if*(!ac)

        var_bang(x);

    *else* *for*(*int* i = 0; i < (ac > x->x_n ? x->x_n : ac); i++)

        value_setfloat(x->x_sym[i], atom_getfloat(av+i));

}


*static* *void* var_free(t_var *x){

    *for*(*int* i = 0; i < x->x_n; i++)

        value_release(x->x_sym[i]);

    freebytes(x->x_sym, x->x_n * *sizeof*(*x->x_sym));

}


*static* *void* *var_new(t_symbol *s, *int* ac, t_atom *av){

    s = *NULL*;

    t_var *x = (t_var *)pd_new(var_class);

    x->x_sym = getbytes(*sizeof*(t_symbol) * (x->x_n = (!ac ? 1 : ac)));

    *if*(!ac)

        value_get(x->x_sym[0] = &s_);

    *else* *for*(*int* i = 0; i < x->x_n; i++)

        value_get(x->x_sym[i] = atom_getsymbol(av+i));

    outlet_new(&x->x_obj, &s_list);

    *return*(x);

}


*void* var_setup(*void*){

    var_class = class_new(gensym("var"), (t_newmethod)var_new,

        (t_method)var_free, *sizeof*(t_var), 0, A_GIMME, 0);

    class_addbang(var_class, var_bang);

    class_addlist(var_class, var_list);

}

Em seg., 9 de out. de 2023 às 21:11, Alexandre Torres Porres <
porres at gmail.com> escreveu:

> Em seg., 9 de out. de 2023 às 20:27, Alexandre Torres Porres <
> porres at gmail.com> escreveu:
>
>> but printing and debugging, it does say and show it was in fact created...
>>
>
> here's to illustrate, I load the patch with a single [var a b c] object,
> and I'm printing at creation time that it's creating 'a', 'b' and 'c' which
> didn't exist before. If I duplicate the object, it says it's creating 'a'
> again as if it didn't already exist.. copy again and you get yet the same
> thing. Now if I delete the first object it shows that error where it
> couldn't find the variable to free it, as it really never existed. If I do
> "control z" and recreated the object, it recreates yet once again variable
> 'a'...
>
> [image: Screen Shot 2023-10-09 at 21.07.39.png]
>
> I don't know how to test any further and it really seems there's NOTHING
> wrong with it with the code :)
>
> thanks
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puredata.info/pipermail/pd-dev/attachments/20231009/4381a592/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2023-10-09 at 21.07.39.png
Type: image/png
Size: 94536 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-dev/attachments/20231009/4381a592/attachment-0001.png>


More information about the Pd-dev mailing list