Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Player interaction  (Read 4082 times)

0 Members and 1 Guest are viewing this topic.

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Player interaction
« on: April 22, 2016, 05:05:05 am »
Hello, I have a question about how a player can interact with 'objects' in a game. With my small project I would like for the player to use the mouse and drag a copy of a sprite to a location on the map. For example, you have a menu that shows a red square, the player clicks on the square and drags it to a location. The red square will still be in the menu, but the mouse is dragging a red square as well. If you need me to explain this better just ask..

I am not sure of where to start on this process nor on how it is done. If anyone has done this, could you share some information?  :)

Thanks,
lane

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Player interaction
« Reply #1 on: April 22, 2016, 05:54:44 am »
Hi lane,

I don't have any source code that does this now, but one solution I can think is to maintain a vector of Sprites using std::vector<sf::Sprite> or some container like that and then you need an update routine that will check your mouse actions.

If the mouse is within the coordinates of the existing sprite, and the user clicks the left mouse button, you make a temporary copy of the sprite and move its position with the mouse.  When the user releases the mouse button, that triggers an action to actually push an element into the Sprite vector at the position where the mouse was released.

If you aren't very comfortable yet with events and monitoring mouse click actions, I would experiment with a simpler detection of mouse clicking, button releasing, etc before trying to solve this exact issue.
« Last Edit: April 22, 2016, 05:56:56 am by erdrick22 »

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Player interaction
« Reply #2 on: April 22, 2016, 06:30:16 am »
What you want is a Button and a Drag and Drop behavior. User interacts with your program using the input hardware that he has available (mouse, keyboard, etc...). You can read these inputs using sf::Events (see detailed description). You can take a look at the available SFML GUI libraries and see if you like what you see, but for learning purposes I would recommend you to make your own button, at least the first one! Buttons are the most basic GUI element and most of the other elements are just specialized buttons.

A suggestion of how to implement a button.
A button has 3 states:
- non-active: mouse outside of the button.
- hovered: mouse over button AND mouse button not pressed.
- active: mouse over button AND mouse button pressed.
A button also has a trigger, something that happens when it goes from active to hovered (trigger on release) and/or from hovered to active (trigger on press).

Make a button class, create a button, put it in your game loop, update the state according to the mouse state and make a function to be called when the button is triggered. This function will create a new object, a brand new object, the button that you made doesn't even need to know what it just did.

Now your other object also behaves like a button (things happen when you click on it) so you can either inherit from button (not my favorite) or have a button inside it (weeks from now, when you start reading about ECS vs OOP you will understand this better). Now, what's the trigger of this other button?

Let 'drag' be a boolean type inside your object. This button sets drag to true on press and drag to false on release. And then in another part of your game loop you check if the object is being draged and update according to the mouse position.

Easy enough, right?
« Last Edit: April 22, 2016, 07:52:49 pm by Tukimitzu »

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Player interaction
« Reply #3 on: April 23, 2016, 05:07:02 am »
Thank you both for your posts! I now have a place to start! I definitely have some things to search out and to test before I continue.  8)

Some questions:

Quote
This function will create a new object, a brand new object, the button that you made doesn't even need to know what it just did.

I am curious of how to do this. How would I create a brand new object?

I will most likely have more questions as I continue this goal..



Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Player interaction
« Reply #4 on: April 23, 2016, 05:32:13 pm »
The next best step, in my opinion, would be to do more studying of C++ in general, and learning when variables go in and out of scope, how containers work, etc.

Its just my opinion that before you should attempt to manage game objects it might be helpful to practice creating a containers of basic C++ types like integers and how you would the use the language to add a new integer, for example, to a vector or other C++ container type.

Once you have this down for basic types the concepts can translate almost directly to more complex SFML types.

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Player interaction
« Reply #5 on: April 23, 2016, 06:29:18 pm »
How would I create a brand new object?

That's your answer:

The next best step, in my opinion, would be to do more studying of C++ in general, and learning when variables go in and out of scope, how containers work, etc.

You use a container (list, vector, map... there are many options) to store all your game objects. When I say "create a brand new object" I mean add an item to that container.

As for "then what do I do with the objects in this container", you keep updating them accordingly, until they cease to exist. For instance, once you've added your player to that list, keep updating it as long as it exists in your game. If you are not familiar with what does a game loop looks like it goes like this:

GameObjects objects
while game is open
  for each object in objects
    object.update()
  end for
end while

or

GameObjects objects
Systems systems
while game is open
  for each system in systems
    system.update(objects)
  end for
end while

For learning purposes, it doesn't matter which one you choose.

lane

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Player interaction
« Reply #6 on: May 01, 2016, 05:11:46 pm »
I have given some time for my brain to rest, so that I have a clear mind for this quest. I have many issues with learning what I need to do. I understand I need to make classes for my buttons and objects, but I have no idea how to: a.) make the class drawable and b.) fill the class with useful functions. I don't know what to put in these classes! I try to look at examples, but there is no clear understanding for me. I still do not understand how you can make an object(for example a texture or sprite) to be brand new to the button or the game for making the copy of it. Is there any reference's that can make this clear for me? Can someone explain these for me? :-[

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Player interaction
« Reply #7 on: May 02, 2016, 01:30:43 am »
Have you ever taken an C++ training course before or are you just trying to learn by looking at others' code?

I would like to recommend you watch this entire thing and re-watch as required

https://mva.microsoft.com/en-us/training-courses/c-a-general-purpose-language-and-library-jump-start-8251

Try the examples for yourself, and when you finish i think you'll have a better idea of things.


 

anything