setpos of buildings
10 replies



25.06.16 12:09:59 pm
Hello, can you write for me second line of code?
I've got a trouble with it as you can guess
I've got a trouble with it as you can guess
Code:
1
2
3
2
3
if object(1,"exists") then
object(1,"x")=object(1,"x")+1
end
object(1,"x")=object(1,"x")+1
end
Sniffin'Man "If it cannot be done with lua, it must be you mama, coz she's too fat"
You cannot change the position of an object. You could do that with
imagepos function, but 100% of the time, that leads to bugs.
You could, however, save every attribute of said object and spawn a new one elsewhere with the same attributes.

You could, however, save every attribute of said object and spawn a new one elsewhere with the same attributes.
edited 1×, last 25.06.16 01:44:07 pm
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
In short you can't do that.
object(1, "x") returns a number, it doesn't set it.
Look, that's what your second line looks like after object() has returned numbers:
How is it supposed to do anything?
Here's the explanation: in programming there're often 2 types of functions: get and set functions. Obviously, get functions return some values e.g. object(1, "x") where you have specified to return the X position of building with ID 1.
Now, the object is not a set function, but that's kind of how a set function would work: setObject(1, "x", 17) - here you would tell the game to set the property "x" (that is the X position) of object with ID 1 to 17. See, you need to pass an additional argument to a function, so it knows what to do under the hood.
object(1, "x") returns a number, it doesn't set it.
Look, that's what your second line looks like after object() has returned numbers:
Code:
1
5=5+1
How is it supposed to do anything?

Here's the explanation: in programming there're often 2 types of functions: get and set functions. Obviously, get functions return some values e.g. object(1, "x") where you have specified to return the X position of building with ID 1.
Now, the object is not a set function, but that's kind of how a set function would work: setObject(1, "x", 17) - here you would tell the game to set the property "x" (that is the X position) of object with ID 1 to 17. See, you need to pass an additional argument to a function, so it knows what to do under the hood.
Thanks for replies and explanation, I guess I'll use image with hitzone then
Sniffin'Man "If it cannot be done with lua, it must be you mama, coz she's too fat"
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function moveObject(old_x, old_y, new_x, new_y)
old_id = objectat(old_x, old_y)
if old_id == 0 then
return 0
-- Returns 0 when there's no object at < old > position.
end
ob_type = object(old_id, "type")
ob_rot = object(old_id, "rot")
ob_mode = object(old_id, "mode")
ob_team = object(old_id, "team")
ob_player = object(old_id, "player")
ob_health = object(old_id, "health")
killobject(old_id)
spawnobject(ob_type, new_x, new_y, ob_rot, ob_mode, ob_team, ob_player)
new_id = objectat(new_x, new_y)
damageobject(new_id, object(new_id, "health") - ob_health, 0)
-- Suggested by VADemon
return new_id
-- Returns the ID of the new object, just in case.
end
old_id = objectat(old_x, old_y)
if old_id == 0 then
return 0
-- Returns 0 when there's no object at < old > position.
end
ob_type = object(old_id, "type")
ob_rot = object(old_id, "rot")
ob_mode = object(old_id, "mode")
ob_team = object(old_id, "team")
ob_player = object(old_id, "player")
ob_health = object(old_id, "health")
killobject(old_id)
spawnobject(ob_type, new_x, new_y, ob_rot, ob_mode, ob_team, ob_player)
new_id = objectat(new_x, new_y)
damageobject(new_id, object(new_id, "health") - ob_health, 0)
-- Suggested by VADemon
return new_id
-- Returns the ID of the new object, just in case.
end
There are probably some mistakes as I didn't test it.
EDIT: If my code works, what you're trying to do would be done this way:
Code:
1
moveObject(x, y, x+1, y)
This would be so easier if we could access the objects directly (OOP).
edited 5×, last 25.06.16 08:46:15 pm
There is no way to get the old object's health then...
Unless you know the health of every type of object. Anyway, it's too messy.
Unless you know the health of every type of object. Anyway, it's too messy.
Yeah and game("mp_building_health <BUILDING>") doesn't return anything. cc: @
DC
But you already created a new building with 100% health, so you can easily get the health value of the new building:

But you already created a new building with 100% health, so you can easily get the health value of the new building:
Code:
1
damageobject(new_id, object(new_id, "health") - ob_health, 0)



