Raydium 3D Game Engine

Official forum for everything about Raydium, ManiaDrive, MeMak, ...
It is currently Thu Mar 28, 2024 9:40 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jan 22, 2010 2:29 am 
Offline

Joined: Thu Oct 09, 2008 5:49 pm
Posts: 69
Hey, I am trying to make game in python and am currently stuck on trying to use buttons.
Here is an example of what I mean:


--Working code--
Code:
#!/usr/bin/python
import raydium
import sys

#this function would do something when button is pressed
def button():
   print "Doing something.."


def create_gui():
   #load theme
   raydium.raydium_gui_theme_load("raydium_chat.gui")
   #create gui window
   handle=raydium.raydium_gui_window_create("menu",30,30,30,15)

   #create a button
   #link it to the function button()

   #this is the problem, if you call just "button" then it errors
   #if you call just "button()" then it calls button() at startup but never when it is clicked

   raydium.raydium_gui_button_create("login",handle,30,30,"Login",button())
   #raydium.raydium_gui_button_create("login",handle,30,30,"Login",button)

def display():
   #start menu   
   if raydium.cvar.raydium_key_last==1027:
      raydium.exit(0)
   raydium.raydium_clear_frame()
   raydium.raydium_rendering_finish();


raydium.raydium_init_args(sys.argv);
raydium.raydium_window_create(640,480,raydium.RAYDIUM_RENDERING_WINDOW,"Button test");
raydium.raydium_window_view_update();

#setup gui
create_gui()
raydium.raydium_gui_show()

raydium.SetMainLoopPython(display);


as you can see the function button() will be called when button is clicked, however it only calls at startup and never once button is clicked.

if you make the line:
raydium.raydium_gui_button_create("login",handle,30,30,"Login",button)

instead of:

raydium.raydium_gui_button_create("login",handle,30,30,"Login",button())

it will error with this:

Traceback (most recent call last):
File "test.py", line 38, in <module>
create_gui()
File "test.py", line 23, in create_gui
raydium.raydium_gui_button_create("login",handle,30,30,"Login",button)
TypeError: in method 'raydium_gui_button_create', argument 6 of type 'void *'

So question is, am I doing something wrong? how do I fix it? :wink:

Thanks!


Top
 Profile  
 
PostPosted: Fri Jan 22, 2010 6:28 pm 
Offline
User avatar

Joined: Sun Mar 16, 2003 2:53 am
Posts: 2591
Location: gnniiiii (Scrat)
Good question !

The trouble is simple : you can't use callbacks with bindings. The idea of a callback for the C language is to give a function (A) address to another function (B), so this function (B) can jump at this address when it's needed (here, when the user clicks the button).

With bindings, you can't be sure that the language supports function addresses, and even if it's the case, functions does not work the same (stack is not prepared the same way, data types are not the same and should be converted, as for all functions in the bindings, ...).

Long story short, you can't use callbacks. That's why we've add hacks like "SetMainLoopPython()" in python bindings.

The bad news is that Raydium use quite a lot of callbacks, so some features of the engine will not be available with bindings. The good news is that we can add workarounds, and that it's already the case for GUI buttons ! See the raydium_gui_button_clicked(), that will return if any button was clicked during the last frame.

Ref for this function: http://wiki.raydium.org/wiki/RaydiumApi ... on_clicked

edit: link is now persistent.


Top
 Profile  
 
PostPosted: Sat Jan 30, 2010 11:03 am 
Offline

Joined: Thu Oct 09, 2008 5:49 pm
Posts: 69
Thank you! I thought that was the correct way but I was hoping there would be a callback... :P

I Shall use raydium_gui_button_clicked() for my python gui experiments then :)

Thank you.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 36 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
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group