Jump to content


Photo

[solved] Passing variables from GUI to script and vice versa

guis gui script

9 replies to this topic

#1 motorsep

motorsep

    Advanced Member

  • Member
  • PipPipPip
  • 884 posts

Posted 09 January 2015 - 10:46 AM

Do you know by chance how to pass values of variables from script to GUI ?

 

 

This GUI I have been working on is for a self-driving vehicle. Player would have to find a fuel cell (power cell) item to place into the vehicle before he can get a ride.

So basically when player trips a trigger entering the vehicle, a script would check for a variable "vehicleIsReady" and if it's 0 (player never placed power cell into vehicle), my GUI would display a red screen saying "out of fuel" and if vehicleIsReady is set to 1, then normal GUI will be displayed (as seen on the video).

How would I do that? Currently I can set variables in GUI code as set "Desktop::variable" "value"; and check if ( "Desktop::variable" == value ) {} but that's for internal to GUI variables/values. I don't know how to pass script variable into GUI using gui_parms, if in some other way.

 

Please help.

Thanks beforehand.


Edited by motorsep, 12 January 2015 - 12:47 AM.


#2 Tels

Tels

    Mod hero

  • Member
  • PipPipPipPipPip
  • 15024 posts

Posted 09 January 2015 - 11:21 AM

Do these help:

 

    // TODO: load data from an XD file
    // set the number of lines:
    $player1.setGuiInt(gui, "msg_lines", getIntKey("lines"));

    // set the text for the GUI
    $player1.setGuiStringFromKey(gui, "msg_text", self, "text");

    // set number of lines
    $player1.setGuiInt(gui, "msg_lines", getIntKey("lines"));

 

? I'm not sure if "setGUIInt" etc are available in your engine.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." -- George Bernard Shaw (1856 - 1950)

"Remember: If the game lets you do it, it's not cheating." -- Xarax

#3 motorsep

motorsep

    Advanced Member

  • Member
  • PipPipPip
  • 884 posts

Posted 09 January 2015 - 11:38 AM

I looked into our _events.script and the only things we got are:


scriptEvent     void setGuiParm( string key, string value );

scriptEvent     void setGuiFloat( string key, float value );

// WTF?!
scriptEvent void        setGui( float guiNum, string gui );
scriptEvent void        precacheGui( string gui );
scriptEvent string      getGuiParm( float guiNum, string key );
scriptEvent float       getGuiParmFloat( float guiNum, string key );
scriptEvent void        guiNamedEvent( float guiNum, string event );

the stuff under // WTF? comment is from D3XP and that had no comments / explanation for it. So I am not sure how to use it.

 

I guess I need to make some tests on small simple gui first :)



#4 Tels

Tels

    Mod hero

  • Member
  • PipPipPipPipPip
  • 15024 posts

Posted 09 January 2015 - 01:22 PM

setGUIParm and setGUIFloat should be doing what you want, it sets the key "xyz" to the value "foo" (or 1.0).

 

getGUIParm works the reverse, given the GUI index number (which you get when you create the GUI, it is basically a handle), and a key like "xyz", you get the value back.

 

To see an example, look at tdm_message.gui - it contains references to "gui::mdg_text" (the variable is visible outside the GUI as "msg_text", not sure if "gui::msg_text" would also work). Its value will be transfered to the "text" property of the "message" window on load of the GUI in the onTime 0 block.

 

You could use any other value and manipulate any other value, like foreground color, font etc etc.


Edited by Tels, 09 January 2015 - 01:24 PM.

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." -- George Bernard Shaw (1856 - 1950)

"Remember: If the game lets you do it, it's not cheating." -- Xarax

#5 motorsep

motorsep

    Advanced Member

  • Member
  • PipPipPip
  • 884 posts

Posted 09 January 2015 - 01:39 PM

Aye, thanks, I'll poke at it.



#6 motorsep

motorsep

    Advanced Member

  • Member
  • PipPipPip
  • 884 posts

Posted 10 January 2015 - 11:55 AM

Apparently, and I don't know if TDM changed how things work, the only way to pass gui_parms (those are the only variables passable between GUI and script) back to GUI is to have onEven { } code in the GUI ( or onNamedEvent {} ). Do I need to put it into Desktop windowDef or anywhere I please?

 

What is onNamedEvent { }  ? How to I define named event ?



#7 motorsep

motorsep

    Advanced Member

  • Member
  • PipPipPip
  • 884 posts

Posted 12 January 2015 - 12:44 AM

Done!

 



#8 demagogue

demagogue

    Mod hero

  • Active Developer
  • PipPipPipPipPip
  • 5245 posts

Posted 12 January 2015 - 01:47 AM

Congratulations on figuring it out.

I wouldn't have known how to do this either, but now that I do it gives me ideas... ;)


Posted Image

#9 SteveL

SteveL

    Hero Coder

  • Active Developer
  • PipPipPipPip
  • 3669 posts

Posted 12 January 2015 - 04:05 AM

Cool. Good to know it works. 



#10 motorsep

motorsep

    Advanced Member

  • Member
  • PipPipPip
  • 884 posts

Posted 12 January 2015 - 09:06 AM

Well, it didn't work with gui::variable. I had to use gui::gui_parm1 (2, 3, etc.) to toss around strings/floats. Also GUI won't accept input from the script without onEvent or onNamedEvent. The latter is preferable, as onEven runs every frame and also registers mouse leaving/re-entering GUI (Desktop). So refocusing on GUI will trigger whatever you have in the onEvent {} block. So I think onNamedEvent is what should be used all the time, except for cases when focusing on GUI should trigger something.





Reply to this topic



  



Also tagged with one or more of these keywords: guis, gui, script

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users