Clicking a game object

#1

Hello,

I know that the question has already been asked several times and I first tried what was said in those topics. Although, it was too complicated.

I have a factory which creates several items over time. After a few seconds, it gets destroyed with go.delete(). What I want to do is to destroy the object once it’s clicked and send a message to an other game object.

The game is based on click and I thought that it was something which worked with the hitbox. I got told that it was the case because of the Z coordinate. I also got told that there were two ways to do that:

  • Transform those game objetcs into GUI objects
  • Use an invisible object which follows the cursor and check the click and the collision

I tried also to understand the example for clicking objets but It’s complicated for me.

It is my first project and if I knew that hitboxes were not enough, I would have not chosen a game based on clicks or would have chosen a different engine for this project. As it’s started already, it is a good thing to continue and learn more :slight_smile: !

0 Likes

Blocks Unlock: Puzzle :-)
#2

I would suggest trying the cursor and collisions. It’s not that hard. Here’s a library that will simplify it further:

Check the example in the library for usage.

1 Like

#3

Hello,

First of all, thank you for your quick answer. I read several times the documentation you shared and also the example in my previous message. Although, I don’t realy get what has to been done. For example:

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("@render:", "clear_color", { color = vmath.vector4(95 /256, 129 / 256, 161 / 256, 1 ) })
	go.set("go1#sprite", "scale", vmath.vector3(1.5, 1.5, 1))
	
	goput.add("go1#sprite", nil, handle_click)
	goput.add("go2#sprite", nil, handle_click)
	goput.add("go3#sprite", nil, handle_click)
	goput.add("go4#sprite", nil, handle_click)
	goput.add("go5#sprite", vmath.vector3(100, 50, 0), handle_click)
end

I don’t understand why should I add all the sprites to an array. If it’s required, I don’t either know how to add all the elements because they are all but one generated by a factory. Or maybe didn’t I understand well? Should I just add all the sprites that would possibly be clicked?

Finally, I want to be able to send a different value depending what’s clicked. That’s why I tried before realizing that the click doesn’t work depending only on the hitbox:

-- local food = X (X depends on what's generated by the factory)

function on_input(self, action_id, action)
	if action_id == hash("click") and action.pressed == true then
		msg.post("/instance0", "food", { food })
	end
end

In this case, should cursor.script send a message to the item that has been clicked and then this item would emmit a message to instance0?

Thank you for reading.

0 Likes

#4

Ignore this stuff. It’s from the Clicking Objects example you linked above. Focus on using the solution provided in Defold-Input. Download the project to disk and open the example project. Look at how the Cursor example is created. It’s very little code and setup.

1 Like

#5

Obviously, it is more complicated if I don’t read the right example! Thanks for the link. I don’t get it all yet but I’m slowly learning :slight_smile: !

One thing I wonder: should I use the script the whole script cursor.scriptor is it better to pick what I need? Maybe the second one is too risky.

0 Likes

#6

Well, I could make it work! Thanks a lot :smile: ! Now, what I wonder is: should I send a message to the item which would send a message with a value (local value in the item) to the score? Or is it better to manage all this from controller.script?

EDIT: or maybe I don’t need a controller but manage all the events with cursor.script in the function on_message() of each items?

0 Likes

#7

The recommended way is to add Defold-Input as a library dependency to game.project:

The cursor.script will send messages to the things you click on. You can also have the cursor script send messages to the game object it is connected to. You should either put score handling logic on the things you click or on the cursor game object.

1 Like

#8

Actually, I didn’t use on_message() but on_input() from my previous attempt. It all works well :smiley: . Thanks, all is fine now!

1 Like

#9

Hello,

I just wanted to know one thing. What is the use of:

function init(self)
	if not go.get("#cursor", "acquire_input_focus") then
		self.forward_input = true
		msg.post(".", "acquire_input_focus")
	end
end

function on_input(self, action_id, action)
	if self.forward_input then
		msg.post("#cursor", "input", { action_id = action_id, action = action })
	end
end

It’s in controller.script but not greenalien.script. I don’t know if I have to add it to each items.

Thanks!

0 Likes

#10

The controller. script is added to the same game object as the cursor right? What it does in init() is to check if the cursor.script is configured to automatically acquire input focus or not via it’s script property. If it isn’t, then the controller script acquires input focus instead. And when it receives input it forwards it to the cursor script. It’s just two different ways to use the cursor script.

You should not do it on other game objects. Only on the game object that the cursor script is attached to.

But it all depends on how you plan to use the cursor script. You may not need a controller script even.

2 Likes

#11

I get it. Thanks for your time and explanations :smiley: !

1 Like