Help with drag and drop and some other stuff


(Ferran Ruiz Sala) #1

Hi there I’m trying to make a game where you have a number of people in a room and you control them by dragging them and dropping them on the objects you want them to interact with. I already have a drag and drop system implemented, but i cannot manage to figure how to drag only one entity if they are stacked one over another.

Also, when i put the mouse over one of the entities, a pop-up appears over their head that shows name and health bars. I don’t know how to structure it so the bar updates in real time (now, it only shows differences every time you open it up).

The pop-up comes up this way:

>    if (touching() && !holding() && !infodisplayed) {
>     	info = world.add(new Info(this));
>     	infodisplayed = true;
>     	countdown = 15;
>     }
>     if ((!touching()||holding()) && infodisplayed) {
>     	countdown -= 1;
>     	if(countdown <= 0){
>     	world.remove(info);
>     	infodisplayed = false;
>     }

Then the “info” entity does this:

>

      override public function added():void
    {
   	hapinessbar = world.add(new Hapinessbar(x, y, myHuman.hapiness));
 	healthbar = world.add(new Healthbar(x, y, myHuman.health));
  	nametag = world.add(new Nametag(x, y, myHuman.myname));
         }

 
     override public function update():void
     {
     		x = myHuman.x - 5;
     		y = myHuman.y - 40;
     			
     		healthbar.update();
    	        hapinessbar.update();
     	}

How can i manage to keep the bars updated all the time? I’m not even sure what I am doing.

Thanks a lot! :slight_smile:


(TaylorAnderson) #2

Instead of giving just the number to each bar, you should give the human itself. Then each of the bars can handle that on their own. Or, you can update all of their values individually from the info entity each frame. But just passing myHuman.happiness (you misspelled it in your code, I’m not sure if you care enough to change it tho :P) will just give it that number which will never update.


(Ferran Ruiz Sala) #3

Thanks for the answer, im kinda trying to figure it out now.


(Justin Wolf) #4

Make a global (public static) variable somewhere. I prefer to create an actual Global class and then store a bunch of public static variables from there for easy access and such. Here’s some pseudo-code:

if (!Global.humanBeingDragged)
{
     // perform the code for picking up this human here

    // set global variable to this human
    Global.humanBeingDragged = this;
}

And then when you drop that human, make sure you nullify the Global variable.

// code that drops the human here

// nullify the global variable so we can pickup another human
Global.humanBeingDragged = null;

Even if you click two at the same time, one of them will always activate the Global variable an update tick before the other, so it will never pickup the second human because the Global had already been set.


(Ferran Ruiz Sala) #5

That sounds sweet i’ll try it right away :slight_smile: Thanks


(Ferran Ruiz Sala) #6

Okey, i get the idea but im not totally sure how to execute it, i’m sorry im kind of a newbie in class-related programming :stuck_out_tongue: I created Global as a class and I wrote public static var HumanBeingDragged:Human = null; in it, but when I try to acess it by Global.humanBeingDragged it tells me Access of possibly undefined property humanBeingDragged through a reference with static type Class. Should I make Global an entity and add it to the world or what?

Thanks for dealing with my incompetence :slight_smile:

EDIT: Worked it out! Oh man global variables are amazing. Thanks!


(Justin Wolf) #7

:slight_smile: Glad you worked it out on your own! Best way to learn, imo. And to answer your question (just in case), Global doesn’t need to extend anything nor be added to the world. So long as it’s in your base src folder, you should be able to access it (and all contained public static vars/functions) from everywhere within your project without needing to import it.


(Jacob Albano) #8

noooooo what have you done

You just introduced global variables without explaining how they can screw things up if you’re not careful. Global variables can be very handy, but they can also really cause problems.

If you want to reset your world, you have to make sure to reset every one of your globals too. Each time you add a new one, you have to make sure that’s also being reset.

And what happens if you end up modifying a variable in a few different places? It’s going to end up set to the wrong value at some point and you won’t have any idea where it went wrong.

It’s not uncommon for professional game developers and big studios to outlaw global variables entirely. They can be useful, for sure, but try to avoid them whenever possible. For your sake, and for the sake of the people who are going to have to help you out later on.


(Justin Wolf) #9

I don’t see the harm in using them so long as - as you stated - you remember to reset them when necessary, which is as simple as calling a function that resets all the variables at a given time to their default state. They’re easy to use, easy to remember (if stored in one class), and they work. Obviously, you’d only need to use global variables in certain cases… however, his case is pretty straight forward as there’s only one singular place he’s setting his variable, thus resulting in no problems.

And with the logic that it’s kind of a pain that he’d have to reset his variables, what’s he going to do when he starts getting into using create/recycle? Well, he’ll have to reset his variables every time his Entity gets re-used, no different then resetting global variables, imo, albeit you’re doing it in different places and at different times, but it’s still the same idea. I’ve gotten more problems using create/recycle and the variable resetting than I ever have using global variables just from not remembering to reset them upon their re-use. It’s simple stuff and I don’t get the flak. Keep track of it and it doesn’t get out of hand. Use whatever works.

As for setting Global vars in multiple places, well if you know the issue is with one of your globals, all you’d have to do is press Ctrl+I (assuming one is using FlashDevelop) and then search all your files for where the Global var is being set from and figure out which one is causing the problem by trial and error, no?


(Jacob Albano) #10

I can’t speak to the pains of using create/recycle as I don’t find them to be particularly useful and never really use them. The benefits don’t feel worth the hassle, and besides I mainly use C# these days so I have a lot more processing power to work with.

In this specific case, obviously using a single global var is not going to cause any problems. My concern is that it’s possible to treat global state as a magic bullet instead of thinking through your project architecture and structuring things in a way that guarantees that no “trial and error” is required to make things work…because really now, games get complicated and nobody has time to test every single system after making a change. The more isolated each component is, the better, because that way if you run into a bug in a specific area you have a very limited scope in which the problem can lie, rather than potentially having to look through every piece of code in order to find the issue.

I’m not coming at this from a purely theory-driven angle either. I have plenty of experience with global variables gone wrong from my first game project. It was a total nightmare to debug and we ended up having to jump through a lot of loops just to get the game at large halfway working. Adding in a save/load feature took far too long and it was buggy for a long time before we got it right – again, all due to the variables that had to be set and reset in the right places.

Just because you don’t see the harm in using global variables doesn’t mean others who do are wrong. If you haven’t had problems with them, that’s great, but I have and I see others having the same problems on these very forums; heck, just look at all the times using FP.world.add() has thrown people off and left them without a single clue what was happening, or the threads where someone (not naming names) posts four entire source files because he’s interweaving global state throughout every one of them.

I’ve always found it to be a bad idea. That’s my experience. On my team, global variables are outlawed entirely (constants are fine), and we’ve never had a problem with that restriction.

See also:

[Edit:] removed one link because it mainly talks about Java.


Keeping the values of variables while world changes
Variables Across Classes
(Justin Wolf) #11

I definitely see your (and whomever else chooses not to use them at all) point. Though I’m not sure where I stated that you (or anyone else) were wrong in thinking so, however. Definitely wasn’t my intention and I’m sorry if you took it that way.

With that said, I agree that they can wind up causing problems in the long run if you’re using them improperly (or at all), but I think if you consolidate them to cases like his or cases similar, there shouldn’t be any issues - most of my globals are constants, so maybe that’s why I never really run into issues regarding them. My solution was more or less, as you defined it, a magic bullet that will work for what he needs it to do. Isn’t that the idea: making sure it works first and then going back and changing it up in terms of proper technique and/or optimizing it? Might not be worth the pain of going back and changing how it works, but I guess it helps to see it work first, if you catch my drift.


(Jacob Albano) #12

I’ve always found the opposite to be true, actually. You start off with a bunch of global vars to just make things work, and then during refactoring you suddenly have dependencies running throughout your whole codebase. Now you have to figure out how de-globalize your data and only pass each component the info it needs…but it turns out you’ve designed one class to rely upon having a no-args constructor, or you have no idea how to give your AI agents the position of the player in all the places they need to access it. If you’ve been modifying this data instead of just reading it, you’ve now got a whole new set of problems to deal with.

I see issues like this and while yes, you’re right – it helps to see it in action first – I can’t help but look ahead and see the problems that will inevitably crop up by using a quick solution without being aware of the drawbacks.

I went a little too far in accusing you of saying I was wrong about this, and I apologize. I was referring to your comment about not understanding the flak that global vars get where I thought I demonstrated why they’re harmful in the long run.


(Ferran Ruiz Sala) #13

Whoa this created such a debate!

How should i do it cleanly without global variables? Making a variable in each world where the entities have to be picked and dropped?


(TaylorAnderson) #14

The way I did it–I don’t know if this is ‘right’ or not–was this in my ‘Human’ class (it wasnt my human class but it will be for you)

public function get humanGrabbed():Boolean
{
	var a:Array = new Array();
	world.getClass(a, Human)
	for each(var h:Human in a)
	{
		if (h.grabbed)
			return true
	}
	return false
}

Basically you can put this function in your Human class and then use the humanGrabbed boolean just like a normal variable, and it’d operate the same as a Global variable


(Ferran Ruiz Sala) #15

I like that solution. Just a question, what does “get” do in public function get humanGrabbed?

Thanks


(TaylorAnderson) #16

I’m not entirely clear of how that whole thing works, but it basically allows you to use humanGrabbed just like any other variable, and not a function. like, this way you can do if(humanGrabbed) and dont have to worry about the extra ().