Setting up Toggles

My GUI HUD is coming along, and I’ve got my first set of controls in and doing things (well, I can toggle between them). Here’s what I’m trying to do:

I’m going to have several functions on the HUD (Move, Talk, etc). During game play the player selects a function, and anything they click on performs this function until they select a different toggle (think the old Sierra point-and-click adventures). One of these functions, however, will have additional “sub-functions” that need to be linked directly to the main function. I’m thinking the best way to do this would be with toggles, and setting up my actual controls to reference which toggle is selected so it knows what to do.

What I want to do to prepare for this is:

  1. Set it up so that the only way to deselect a function is to select a different one. I have it working so that selecting one toggle turns off the currently selected one, but nothing is stopping me from turning off a function by clicking on itself.

  2. On the toggle with the “sub functions” I want to set it up so that selecting a sub-function changes what the main function does by default (IE, the Move toggle has three sub-functions: Run, Walk, Stealth. Clicking on Run should default Move to Run, etc). Selecting a sub-function should also turn on that main toggle and make it the active function (IE if the player currently has “Talk” selected, clicking on Run sets the movement type to run, and also make Move the active function).

  3. If the player clicks on a different function after selecting a Move sub-function, they should be able to resume the previously selected move function JUST by clicking on the main Move toggle itself (IE the player was Running, then clicks the Talk icon to interact with an NPC. When finished, he clicks the main Move toggle again and resumes running without selecting).

  4. Selecting a sub-function for a main function should also change the appearance (image) used by the main function.

I’m just not sure what the best approach to this would be.

Edit: Added a fourth point.

for point 1 a simple if statement should do it

where your code says to deselect old function add in

if (just_clicked_function_name != old_function_name)
deselect old function

that way it only deselects if you change functions and doesnt if you keep it the same.

point 2

You are using classes right?

if so your code should look roughly like this i assume

class gui
public members:
private members:

basically one way at least is when you define ANY function as a class whether move() or its sub function run()

give them a variable called parent that stores there parent

a sub function’s parent is its main function

a main functions parent is parent = NULL;

then when someone clicks on a function do another if statement test

if (just_clicked_function_name.Parent() == NULL) 
new_function = just_clicked_function_name;
new_function = just_clicked_function_name.Parent();

now if make run’s parent = move and moves parent = NULL
then new function will equal move if you click on either one.

point 3

this actually shouldn’t be an issue at all unless your creating like a new instance each time.

assuming you have the class move and it has a variable named move_state that stores whether your running, walking, crawling etc. that variable should be set when you click walk to walk. Now are you actually doing anything to that variable when you simply click move?

my guess is you haven’t implemented the sub function yet, when you do you’ll realize that clicking on just move probably doesn’t change the move state, it becomes whatever it was last time, unless its the first time in which case it goes to default.

if when someone just clicks move button your setting move_state = walk, and so basically defaulting every time well that’s just the wrong way to code it, when you start the class use the default class constuctor to set move_state to something so it has a default but then only change it if they hit a button and then if they dont select any it will be whatever it was before.

point 4

test to see if your in a sub fuction.

if (just_selected_function.parent != NULL) 

if so
get the parent’s image and reassign it a new image.

just_selected_function.parent.image = new_image_I_use_when_sub_function_is_selected

i’m a little confused.

this line seems to say, if someone just pressed the walk button set every toggle to false.

if (toggleWalk) toggleWalk = setMeOnly();

which i believe it how your causing every a toggle to disable itself on button press because your reseting your toggle so you can catch it the next time its pressed.

the best way to deal with this is to store the movement state, (useful because while you could do it other ways you’ll probably find later thats it’s useful to be able to determine what state someone is in, so that for example you could prevent someone who is in stealth mode from doing something like maybe changing equipment.)


var move_state = null;

if (toggleWalk && (move_state != "walk"))
  toggleWalk = setMeOnly();
  move_state = "walk";

Now each time they press walk your saving that as the move_state so later you can use that to recall the last button pressed and do the if statement basically so that if they are already walking nothing happens.

By the way I was talking about classes earlier because I know C++ and never learned anything about javascript, it doesnt even have classes which is quite odd to me, thats why point 2 didn’t make sense to you. Now i’ve googled up on JS so i can actually give some code :stuck_out_tongue:

for point 2 basically

function move()
move.parent = null;

function walk()
walk.parent = move();
function run()
run.parent = move();

basically move is a parent

moves children are

now you can use that to do the if statement to determine which toggle main toggle to mess with.


if (pressedToggle.parent != null)
activeToggle = pressedToggle.parent

basically if it’s equal to null that means someone pressed a main button like move for example, in which case you do as normal activating it.
if the parent isn’t null though it means it is a sub function in which case you do the if statement which is the same code except instead of activating pressedToggle you activate pressedToggle.parent and perform all the stuff on parent

Some update, but still having problems:

I’ve created a new state to reference called “action_state.” This is going to apply to all parent functions: Move, Interact, etc. What worked quite well is that I was able to set up the move_state and action_state selections to work independently of each other: Changing toggles for the different action_state functions does NOT deselect the move_state toggles. I think that’s half the battle to getting the Move function working right there, in that I SHOULD, in theory, be able to switch from Move to another function without also disassociating HOW the character moves, so that I can switch back to it just by selecting the Move function.

However I’m still unclear on how to link the movement type to the movement action itself, particularly how exactly that needs to be scripted. Maybe I’m missing something regarding the parenting you outlined above. I’m beginning to think that for certain I need an if/else statement for the Move toggle to check what the movement state is and select the appropriate Style from the GUISkin, either in whole or at least part (with another if statement made when selecting the movement TYPE toggle to apply it to the move action itself). I’m just still not completely following you.

The problem with being able to turn off a preselected toggle by clicking itself is also still present.