another silly question about scripting :)

I realized now that a lot of people do not create vars for the objects or UI objects before using them, but they simply create the object on the fly

Gui.button(rect(X, Y, A, B), "Fire")

Is possible to use something like that instead, or the compiler will slow down the game, due the fact that i am instantiating an object and keeping the memory busy for that object until i destroy it ? ( I assume that when i send the destroy object command, the object is destroyed and the memory deallocated instantaneously; unless you use the GC…sorry but i work in C++, not in Java)

var Shoot_Btn : Gui.button(rect(X, Y, A, B), "Fire") = false;

if (Shoot_Btn == true)
{
    //do something; shoot the bullet
}

I am asking since is hard to change years and years of habits, so if i can continue to use what i already use (as far as coding conventions and workflow) i would be an happy camper :smile:

Thanks!

Well, the thing is, the GUI functions are meant to be called every frame. They aren’t variables you can mold and play with unless you use the GUI objects inside the Unity editor.

It’s called Immediate Mode GUI… as opposed to Retained Mode. There’s a Unite video about it on the website.

Cool, thanks for the hints :slight_smile:

My point is that a button is a button, even if is part of the GUI class, so i can instantiate a variable of type object in another language, but in Unity JS i have no idea how to access directly the objects…i am old school and avoid as much as possible any immediate mode or object creation :smile:

Elsewhere i would do something like this:

#include GUI (whatever name is that file that contains GUI stuff)

Button myobj = new Button() (or whatever other GUI component)

–use the button–

myobj= NULL

Now how to do it in Unity is the challenge here :wink:

GS: i know that they are not variable; they are object form a library that are instantiated on the fly (bad bad habit that Java introduced) to save the time to create a variable and then initialize and then use it… the positive thing is that if you need a text on screen and you don’t care what that text will do after that you use it, it is useful, but if you have a UI where you know that a label will contain the notes, a button will be used to do the same operation all the times, and a label will always display your health or ammo number; you want to have each element correctly organized, and is a nightmare to keep track of GUI stuff if you instantiate it with the immediate mode.

Magwo: do you recall the name of the video? is the GUI video from Unite07?

there are no objects
A gui.xxx call will commonly only return true or false depending on if they were used or not.

Objects would be slightly pointless as the object is commonly gone at the end of the frame

Are you telling me that each frame a gui component is instantiated, used and destroyed?

Man…this is really a messy situation :slight_smile:

So the only way to use the GUI is via the immediate mode then…well, take it or leave it i assume…

This resolve my problem…since there is nothing that i can do to avoid to use the GUI as everyone else does :frowning:

Thanks!

Hey, I feel your pain, I was like this when I started using the GUI too; it’s half as bad though :slight_smile:

Don’t think of GUI.Button(), etc. calls as creating an object, but rather think of them as if they were methods within one.

As far as I can remember (someone correct me if I’m wrong) OnGUI is called multiple times every frame, to prepare layout, draw GUI elements, as well as to perform event handling.

So something like

if ( GUI.Button( myRect, myString ) )
{
    // ...
}

would be a number of different methods in a Button class in a retained mode GUI (like a Draw() method to display it, a delegate to handle click events, etc) all in one.

Basically try to see it from a different angle. Using OnGUI and GUIStyles you can create a functional GUI insanely fast.

Hope this helps.