[UNITY] GUI stuff and selection for an rts

13lade619

is now a game developer :)
Reaction score
399
(looking for unity help, or helpful tips and concepts in general programming)

So my group for class decided to make a basic RTS for our project.
For the prototype I'm in charge of the mainly interface other stuff.

First thing is the selection, basically one unit selection first.
What I was thinking is recording clicks on the gameobject and then adding it to an array,
but how do I exactly do that in UNITY? I want to have a global array that's accessible by multiple scripts (or maybe passed to others through Messages)

Second is the command buttons. I want that if a unit is selected there's a SET of command buttons for the specific unit (just like the command cards in SCII, I believe).

so in theory.. this is what I want to achieve:
Click Unit (store unit somewhere, make command card appear)
Click Button on Command Card (store 'order' somewhere)
Click on Gameworld (have the selected unit execute the order)
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
So, you basically want to do something like this for your RTS objects:

Code:
var wasClicked : boolean;

function OnMouseDown() {
    wasClicked = true;
    Activate();
}

function OnMouseUp() {
    wasClicked = false;
    Deactivate();
}

...only that you also want keyboard input as well? Not tough, set booleans for keys and have separate key function listeners for them as well, then throw in if statements into the mouse-down, mouse-up events.

The cleanest way to do what you want would be to not use globals, and simply have a main script that has a listener for when an object is clicked, and updates the unit variable it stores when something new is selected. That way, other scripts can access its public variable to get any information that is necessary.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Code:
struct UNIT
{
    unsigned int Id;
    unsigned int UnitTypeId;
    // etc...
};

struct COMMAND_BUTTON
{
    unsigned int Id;
    unsigned int CommandButtonTypeId;
    // etc...
};

UNIT* currentUnit; // Currently selected unit
COMMAND_BUTTON* currentCommandButton // Currently selected Commad Button


void Init()
{
    currentUnit = NULL;
}

void UnitSelected(UNIT* u)
{
    currentUnit = u; // Currently selected unit = selected unit
    displayCommandUI(u->UnitTypeId); // Display command buttons according to the unit type 
}

void CommandButtonSelected(COMMAND_BUTTON* cb) // Selected -> Clicked & Hotkey'd
{
    currentCommandButton = cb; // Currently select command button = selected command button
    displayEscapeUI(); // WARCRAFT III like escape button.
}

void OnLeftClick(EVENT& evt)
{
    if (currentCommandButton->CommandButtonTypeId == COMMAND_MOVE) // The command button selected is a MOVE command button
    {
        OrderUnitTo("move", currentUnit, evt.X, evt.Y);
    }
}
That's how I'd see it.
 

13lade619

is now a game developer :)
Reaction score
399
^ yeah, but Unity doesnt work that way. I dont have a huge block of code where I place everything.

Instead, you need to attach Scripts to all GameObjects and then pass/reference values around all the scripts that are involved.
(unless I'm mistaken)

For the command card,
I'm thinking using an empty gameobject, attaching a script where I can send Messages to and have that script have maybe 12 variables for a total of 12 buttons (which will be prefabs)
which I can create on selection and destroy on deselection..

Also, if you know a Unity focused forum please point me to that direction.
The official one, http://forum.unity3d.com/forums/ is unexpectedly VERY INACTIVE. I have a question there stuck 0 replies in 2 days...
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Just read about Unity and seams funny (actually won awards).

Isn't a ~RTS like a too high aim for guys not familiar with some software?

What scripting language are you going to use?

>I dont have a huge block of code where I place everything.
>Instead, you need to attach Scripts to all GameObjects and then pass/reference values around all the scripts that are involved.
(unless I'm mistaken)

Sounds like a mess. I think Blizzard got it right with their approach in wc3/sc2 regarding scripting.

I wonder who calls those scripts attached to the game objects.

@GetTriggerUnit-

Using the source engine is kinda more complex no?
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
What scripting language are you going to use?

The language must be in Javascript or C# (both very similar in structure).

I'll say again: The cleanest way to do what you want would be to not use globals, and simply have a main script that has a listener for when an object is clicked, and updates the unit variable it stores when something new is selected. That way, other scripts can access its public variable which stores the selected unit to get any information that is necessary (e.g. update the health bar, where it is, how big is it, how much % health remains).

The main script should store every Unit class type created within itself, and different Unit type should have subclasses based off of the Unit class so that they all share the basic functions together SUCH AS mouse interaction (which I posted above).
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
I've already set-up the Listener (empty gameobject with the script attached.)

So what do you still not understand, out of curiousity?

Use 'Input.GetAxis("Mouse X")' to get the appropriate axes, if need be (change Y for Y axis, Z for Z axis) if you wanted to do some weird multi-click to be able to select one unit...

I mean, that's what it sounds like you want to do. Why do you need multiple clicks for one unit in an RTS?
 

tooltiperror

Super Moderator
Reaction score
231
I think he means store all objects of the unit type.
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
I think he means store all objects of the unit type.

Correct.

So, say if you make a level-loading system and you load X amount of units. The main script should store the X units within it in an array. That way, you can search through it every game tick and do things (Update() in Unity, right? Do the for statement through the array in an Update() method).
 

SerraAvenger

Cuz I can
Reaction score
234
My 2cts: Don't do an RTS. Either do a turnbased strategy game, if you have like 3 months+; If you have less, do a space shooter like Asteroids or something. You can get your 5+ prototypes running in a few days (asteroids, ship movement, fire ing, asteroids breaking, moving through borders, more if you need to check graphical capabilities or want to implement sound) (assuming you have other stuff to do) and you'll have the rest of the time for your project, which you can do in a week or two if your prototypes were good and helpful, + the time you need for the graphics and sound. If you have 6 months+ you can actually go and try do that RTS, but it might become really stressful and frustrating if your time management isn't good (Who knows what happens in 6 months?).

EDIT: You can always polish a project lateron by adding multiple lives, score tracking, powerups (temp/perm), save\load, classes, additional features, shops...
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top