English setpos of buildings

10 replies
Goto Page
To the start Previous 1 Next To the start
25.06.16 12:09:59 pm
Up
Sudden Death
User
Offline Off
Hello, can you write for me second line of code?
I've got a trouble with it as you can guess

Code:
1
2
3
if object(1,"exists") then
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"
25.06.16 12:23:56 pm
Up
Infinite Rain
Reviewer
Offline Off
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
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
25.06.16 12:29:20 pm
Up
VADemon
User
Offline Off
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:
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.
25.06.16 01:41:25 pm
Up
Talented Doge
User
Offline Off
Kill the object, then spawn another object. It will get a new ID I suppose, though.
25.06.16 03:15:13 pm
Up
Sudden Death
User
Offline Off
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"
25.06.16 06:36:58 pm
Up
Zeik
User
Offline Off
Code:
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:
Code:
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
25.06.16 07:07:27 pm
Up
Talented Doge
User
Offline Off
@user Zeik: Even barricade has more than 100 health.
25.06.16 07:10:29 pm
Up
Zeik
User
Offline Off
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.
25.06.16 07:26:32 pm
Up
VADemon
User
Offline Off
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:
Code:
1
damageobject(new_id, object(new_id, "health") - ob_health, 0)
25.06.16 08:43:41 pm
Up
Zeik
User
Offline Off
@user VADemon: LOL. Yes!! you're right. Totally missed that.
26.06.16 09:37:19 am
Up
DC
Admin
Offline Off
@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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 Next To the start