You could put the whole code into a function main() if you want. If you
have a fixed set of Foos you can also declare them const (`const foo =
Foo(0.,0.), since you would only change the fields in foo.
- Alex.
On Thursday, 7 May 2015 09:22:00 UTC+2, Elburz Sorkhabi wrote:
>
> Thanks for the insight, very much appreciated.
>
> I had considered doing something similar with a type, but what that be on
> par with global variables in terms of performance? I read in the docs that
> global variables are frowned up.
>
> On Thursday, May 7, 2015 at 3:15:28 AM UTC-4, Alex wrote:
>>
>> Hi!
>>
>> Strangely enough (?) it seems one can't pass normal functions
>> to GLFW.Set...Callback. But if you just want to store the positions in some
>> object (maybe to change its properties) you can do something like this
>>
>> # some type which needs positions
>> type Foo
>> x::Float64
>> y::Float64
>> end
>>
>> setpos!(foo::Foo, x, y ) = (foo.x = x; foo.y = y; foo)
>>
>> # initialize a Foo
>> foo = Foo(0.,0.)
>>
>>
>> # this is the callback function which updates the coordinates in foo
>> function poscb(foo::Foo, x, y)
>> println("cursor pos: $x, $y")
>>
>> setpos!(foo, x, y)
>>
>> nothing
>> end
>>
>>
>> # ...
>>
>>
>> GLFW.SetCursorPosCallback(window, (x,y)->poscb(foo, x,y))
>>
>>
>> # ...
>>
>> In the main loop you can now draw/use foo with updated positions.
>>
>> Hope it helps,
>>
>> Alex.
>>
>> PS: The question about the Julian way of doing event processing/callbacks
>> is quite interesting. Reactive.jl is pretty cool, but I don't know if there
>> is a "consensus" that it is the recommended or preferred way of doing this.
>>
>>
>> On Wednesday, 6 May 2015 22:49:00 UTC+2, Elburz Sorkhabi wrote:
>>>
>>> Hey there, about a week into Julia now and really enjoying it.
>>>
>>> I have a quick question that I feel is pretty simple but I can't seem to
>>> find any good examples in the docs or by looking through other peoples
>>> code, and I'm quite new to programming callbacks in general.
>>>
>>> I'm trying to get keyboard and mouse inputs from this GLFW.jl saved into
>>> a variable:
>>>
>>> https://github.com/JuliaGL/GLFW.jl/blob/master/examples/callbacks.jl
>>>
>>> In this example callbacks are used to update and println() the values. I
>>> tried a few different things like replacing the println() with another
>>> function that would assign the values to some variables, as well as just
>>> assigning variables after the ->, but I must be missing something simple as
>>> those didn't seem to work properly.
>>>
>>> What I'd really like is to be able to write something like :
>>>
>>> mouseCoordinates = GLFW.SetCursorPosCallback(window, (x, y))
>>>
>>> and have a 2 element set with the x and y position easily accessible.
>>>
>>> For reference here is the implementation of the SetCursorPosCallback
>>> function (near the bottom of this page):
>>>
>>> https://github.com/JuliaGL/GLFW.jl/blob/master/src/glfw3.jl
>>>
>>> And what seems to be happening is that the function is being passed into
>>> a macro here:
>>>
>>> https://github.com/JuliaGL/GLFW.jl/blob/master/src/util.jl
>>>
>>> Any help even just walking me through it would be greatly appreciated.
>>>
>>>