#I have a beginners question I m trying
1 messages ยท Page 1 of 1 (latest)
Not sure if this is the best method, but you could try something like in the Hogwarts Battle mod. https://steamcommunity.com/sharedfiles/filedetails/?id=2867610721&searchtext=hogwarts
--This is used by another function to locate information on what is in an area
function findObjectsAtPosition(localPos)
--We convert that local position to a global table position
local globalPos = localPos
--We then do a raycast of a sphere on that position to find objects there
--It returns a list of hits which includes references to what it hit
local objList = Physics.cast({
origin=globalPos, --Where the cast takes place
direction={0,1,0}, --Which direction it moves (up is shown)
type=2, --Type. 2 is "sphere"
size={2,2,2}, --How large that sphere is
max_distance=1, --How far it moves. Just a little bit
debug=false --If it displays the sphere when casting.
})
--Now we have objList which contains any and all objects in that area.
--But we only want decks and cards. So we will create a new list
local decksAndCards = {}
--Then go through objList adding any decks/cards to our new list
for _, obj in ipairs(objList) do
if obj.hit_object.tag == "Deck" or obj.hit_object.tag == "Card" then
table.insert(decksAndCards, obj.hit_object)
end
end
--Now we return this to where it was called with the information
return decksAndCards
end
The way its used here is to detect objects in a specific area. Which happens to be next to a little token. I would imagine you could pass along the location of tile.
Might need to change the Physics area to something like this
local objList = Physics.cast({
origin=globalPos, --Where the cast takes place
direction={0,1,0}, --Which direction it moves (up is shown)
type=3, --Type. 2 is "sphere"
size={2,2,2}, --How large that sphere is
max_distance=1, --How far it moves. Just a little bit
debug=true --If it displays the sphere when casting.
})
And then also change the size depending on what you need
if you take the tile by hand and move it, that works. Thanks but this method is too complicated for me, i'm trying to understand but it's above my hat.
Sorry I couldn't help more :(
I once made a rotating board which grabbed the pieces on it (somebody else helped me with that), I'm trying to work on that code to get it to work. Right now, it works for rotation, so it might work for movement also. Still, a bit heard to understand what every code does.
local rotate_button = {
click_function = "rotateBoard",
function_owner = self,
label = "move",
position = {0, 1, 0},
scale = {0.1, 0.1, 0.1},
width = 750,
height = 750,
font_size = 200
}
local resting_objects = {}
function onLoad(save_state)
-- Add the button.
self.createButton(rotate_button)
end
function onCollisionEnter(collision_info)
-- Grab the colliding object.
local col = collision_info.collision_object
-- If the object isn't supposed to rotate (a specific object and the table)...
if col.getGUID() ~= "3a27ae" and col.tag ~= "Surface" then
-- Add it to the list of objects resting on top of the target.
resting_objects[col.getGUID()] = true
end
end
function onObjectPickUp(player_color, picked_up_object)
-- If the object is an object resting on top of the target...
for guid, on_field in pairs(resting_objects) do
if guid == picked_up_object.getGUID() then
-- Remove it from the list of objects resting on top of the target, as it has been picked up.
resting_objects[guid] = false
return
end
end
end
function rotateBoard()
-- If the board is currently moving, do not rotate the board.
if not self.resting then
return
end
-- For each of the resting objects...
for i, v in pairs(resting_objects) do
if v then
-- Attach the object to the board.
local obj = getObjectFromGUID(i)
self.addAttachment(obj)
end
end
-- Rotate the board.
local rotation = self.getRotation()
rotation.y = rotation.y + 45
self.setRotation(rotation)
-- Detach the objects.
self.removeAttachments()
end
- use a
Physics.cast()to find the objects above, store their position local to the tile using.positionToLocal(), and their relative rotation by recording the difference between their.getRotation()tables. - move your tile
- rotate and move the objects from before, using
.positionToWorld()and applying the rotation differences to the tiles new rotation (assuming it also rotated)
will try, thank y ou
Where would you put code like this? OnObjectPickup? Player.Action.Pickup?
periodically, or onLoad(), as onObjectPickUp() is called one frame after picking up, it'd be too late if you used it
it's still very difficult for me. A template-script would be nice.
It didnt' seem to work, I'm sure I made mistakes in codes I don't fully understand ๐ฆ
object1_GUID = '2786c0'
-- -------------------------------------------------------------------------
function onLoad()
TILE = getObjectFromGUID(object1_GUID)
TILE.createButton({
label="move1", click_function="move1",
function_owner=self, position={0,0.8,2},
height=100, width=400, font_size=100
})
end
-- -------------------------------------------------------------------------
function move1()
local positionToMove = {x = 0, y = 1, z = 0}
local colliders = Physics.cast({origin=TILE.getPosition(), direction={0,1,0}, type=3, size={TILE.getBounds().size.x, 100, TILE.getBounds().size.z}, max_distance=0, debug=false})
for _, collider in ipairs(colliders) do
if collider.hit_object != TILE then
local posInTILELocal = TILE.positionToLocal(collider.hit_position)
local newPos = positionToMove
newPos.y = newPos.y + posInTILELocal.y
collider.hit_object.setPositionSmooth(TILE.positionToWorld(newPos), false, true)
end
end
end
Is his new? Normally when I boot the game the buttons appeared immediately, but now they don't appear until I press this box "play script's in the bottom right.
turn "auto-run" on in the scripting window (in TTS)
you've also skipped step 2 in my 3 step plan
because i don't need rotation i thought i could skip it. I'll try again.
I don't think it can be done.
Everytime i try i get an error but never succeed, for something as simple (picking up by hand works fine)
if you don't post your error, we can't help
if you don't post your script that produced that error, we can't help
I understand.
OK this is my script that works perfectly (except that it doesn't take the cards with it) : just two tiles, moving from one location (l1) to another (l2) and back.
object1 is just an item with the buttons
object1_GUID = 'f013da'
object2_GUID = '0d9219'
object3_GUID = '2786c0'
l1 = {-20, 20, 0}
l2 = {20, 10, 0}
l3 = {-20, 20, -10}
l4 = {20, 10, -10}
-- -------------------------------------------------------------------------
function onLoad()
rood = getObjectFromGUID(object1_GUID)
TILE2 = getObjectFromGUID(object2_GUID)
TILE1 = getObjectFromGUID(object3_GUID)
rood.createButton({
label="move1", click_function="move1",
function_owner=self, position={0,0.4,1},
height=100, width=400, font_size=100
})
rood.createButton({
label="back1", click_function="back1",
function_owner=self, position={0,0.4,1.5},
height=100, width=400, font_size=100
})
rood.createButton({
label="move2", click_function="move2",
function_owner=self, position={0,0.4,2},
height=100, width=400, font_size=100
})
rood.createButton({
label="back2", click_function="back2",
function_owner=self, position={0,0.4,2.5},
height=100, width=400, font_size=100
})
end
-- -------------------------------------------------------------------------
function move1(entity, color)
TILE1.setPositionSmooth(l1)
end
function back1(entity, color)
TILE1.setPositionSmooth(l2)
end
-- -------------------------------------------------------------------------
function move2(entity, color)
TILE2.setPositionSmooth(l3)
end
function back2(entity, color)
TILE2.setPositionSmooth(l4)
end
well, it won't take the cards with it because you've now omitted steps 1 and 3 of my 3 step plan
yes I know ๐ I'm going to add that bit by bit (my first attempt was so wrong I have to redo it completely).
step 1 : use a Physics.cast() to find the objects above, store their position local to the tile using .positionToLocal(), and their relative rotation by recording the difference between their .getRotation() tables.
step 3 : rotate and move the objects from before, using .positionToWorld() and applying the rotation differences to the tiles new rotation (assuming it also rotated)
Give me a little time to figure out how to script that (I wish there was a good tutorial somewhere online)
not working though ๐ฆ
well, you're defining MoveObjectsAboveTile1 inside another function, which probably isn't intentional.
using .setPositionSmooth() will be a problem if you are immediately changing the position of the decks, as the tile won't yet have moved
(I wish there was a good tutorial somewhere online)
You'll never find a tutorial for how to combine different scripts together. The only thing that can give you this knowledge is experience and practice.
how they combine is a fundamental part of what they do, and there would therefore be more combinations than seconds you have lived
i do understand that ๐ however that doesn't make it easy. Thanks for the help, much appreciated ๐
it took a while, but now i'm getting somewhere. In fact i made this "game" with 4 taxi's who can move forward and backwards and also move the items that are on top of them. The scripts are on the custom tokens, I would like the buttons to be separated from them, on a fixed place on the board, but i'm already very happy with the result.