• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows Mouse action control

T

TheAlmighty

Guest
Hey i'm using DnD to make a little game but came across issues with distance checking, made a little tree object and allowed the player to hold the left mouse button to chop the tree down, all works well, tree suffessfully chops just having trouble with distance what would be the best approach to this if i want to continue DnD.

the only approach i found was gml using distance to object something along the lines of


if distance_to_object(obj_tree) < range
{
canchop = true;
}

any help would be greatly appreciated, really enjoying myself with this :)
 

Slyddar

Member
Unfortunately with distance_to_object not being available as a code block, you are limited to doing it in GML, just like you have, afaik. Just using an execute code block is not so bad, and will accomplish what you are wanting. Sometimes you will need to mix GML and DND in order to achieve a result, unfortunately strict DND is more centered around beginner coding, and as you get better you start to sprinkle more and more GML in here and there.
 
Are you asking how to structure that in DnD, or what is the best way to identify the id of the object to be chopped?

THE FORMER:
If you go to the Control panel it has 'Set variable' and 'Test variable'

Looking at the GML you posted - it is structured the same in DnD.

create event of object:
Set variable
variable: range
value: whatever you choose

Mouse button event:
Test variable
variable: distance_to_object(object)
value: range
operation: less than
{execute code block}

Note: This is just me answering the question quite literally, and it wouldn't solve everything - as I will explain below.

THE LATTER:
distance_to_object will work, in that if you are at less distance than range it will meet the condition. However, it doesn't return an id of what the closest instance of that object is. instance_nearest as a function would return that identifying id, but it wouldn't necessarily be the tree you are clicking on.

Say, for example, two trees are very close together and you are clicking on the furthest of the two - it will act on the closest, because that is what the function returns.

You would want to look at the collision functions, and order it this way:
1) Mouse button check - does mouse_x / mouse_y register a collision with an instance of the tree?

Set variable
variable: inst
value : instance_meeting(mouse_x, mouse_y, obj_tree); // is the specific point mouse_x / y found to be in collision with the mask of obj_tree? If yes - it returns the id of that instance. If no - returns a value of 'noone'

2) Is there an instance there?
Test variable
variable: inst
value: noone
operation: equal to
tick as NOT // this reverses the condition: if 'inst' is not equal to 'noone' then we know it is instead an instance id

3) do a code block for this if it's true - 'inst' is not equal to 'noone', and so there is an instance at the mouse coordinates
Test variable
variable: point_distance(x, y, inst.x, inst.y) // get the distance between self (x / y) and inst x / y
value: range
operation: less than
{can chop}
else
{can't chop}


dnd.png

I haven't quite set up my test project in the same way as you might, seeing as I just wanted to test this rather than use specific details. But the above code block takes into account whether there is an instance, and is it within a certain distance.
 
Last edited:
T

TheAlmighty

Guest
Oh my goodness. i don't even have words for ur reply :D:eek:

Thanking you guys very much for shedding some light on it, i think i have the right approach now, will post updates :) Thanks again! <3
 
T

TheAlmighty

Guest
With all this infomation i still can't seem to get it working as intended :O
 
Other than not communicating properly that 'range' must be set in the create event, and that you may also need to define 'inst' there too (with a value set to 'noone') - the image up above shows how it can be set up. I'm not sure why it doesn't work for you, as I tested it without any issues. All of the processes explained above are visible in the picture, the only thing else I might have missed is that these actions are to be applied to 'self'

Unfortunately I think I might have deleted the project now, but I'm pretty sure those are the only details I forgot to include.

create event:
variable: range
value: whatever you choose

variable: inst
value: noone

All variables and test variables are set to 'self'
 
T

TheAlmighty

Guest
this is what i'm currently at with abit of tweaking, it moves the node 5 pixels but the range is still infinite
 
Top