Forum

> > CS2D > Scripts > setpos of buildings
Forums overviewCS2D overview Scripts overviewLog in to reply

English setpos of buildings

10 replies
To the start Previous 1 Next To the start

old setpos of buildings

Sudden Death
User Off Offline

Quote
Hello, can you write for me second line of code?
I've got a trouble with it as you can guess

1
2
3
if object(1,"exists") then
object(1,"x")=object(1,"x")+1
end

old Re: setpos of buildings

Infinite Rain
Reviewer Off Offline

Quote
You cannot change the position of an object. You could do that with cs2d lua cmd 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.
edited 1×, last 25.06.16 01:44:07 pm

old Re: setpos of buildings

VADemon
User Off Offline

Quote
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:
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.

old Re: setpos of buildings

Zeik
User Off Offline

Quote
1
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
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:
1
moveObject(x, y, x+1, y)
More >


This would be so easier if we could access the objects directly (OOP).
edited 5×, last 25.06.16 08:46:15 pm

old Re: setpos of buildings

Zeik
User Off Offline

Quote
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.

old Re: setpos of buildings

VADemon
User Off Offline

Quote
Yeah and game("mp_building_health <BUILDING>") doesn't return anything. cc: @user DC

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

old Re: setpos of buildings

DC
Admin Off Offline

Quote
@user VADemon: cs2d lua cmd game only works with simple game settings, not with advanced ones which have multiple parameters. The method signature with only one parameter (for the name of the setting) kind of implies this. I should add that detail to the documentation anyway.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview