Results 1 to 7 of 7

Thread: Logic statements with moving objects

  1. #1

    Logic statements with moving objects

    Hello Guys,

    my Name is Martin and I'm new to Tabletop Simulator as well as to scripting. So I hope you guys can help me with problems in future.
    At the moment I got issues with a Logic Statement. I tried did read through some guides but it seems I did not understand the functionality of logic statements. Here is what I want to do:
    I would like to destroy an object, when another object is moved by a click on a button to a certain position.
    So i thought with the following script this would happen:
    I click on Button and object named "blau" is moving to position ({-2.74, 3.84, -4.93}) wich then would trigger the if statement to be true and then execute the order to destroy object named "tilebig". But what it does is, that if I save & play the script the object named "blau" is directly moved to the certain position and the object "tilebig" is destroyed.


    I tried the following script:


    button_parameters = {}
    button_parameters.click_function = 'buttonClicked'
    button_parameters.function_owner = nil
    button_parameters.label = 'press me'
    button_parameters.position = {0,0.8,0.5}
    button_parameters.rotation = {0,0,0}
    button_parameters.width = 600
    button_parameters.height = 250
    button_parameters.fontsize = 100


    function onload()
    blau = getObjectFromGUID('70e8d5')
    checker = getObjectFromGUID('2bb802')
    tilebig = getObjectFromGUID('1f05f5')
    blau.setName('Blue')
    checker.setName('checko')
    tilebig.setName('großeFliese')
    checker.createButton(button_parameters)
    if blau.setPositionSmooth({-2.74, 3.84, -4.93}) then
    tilebig.destruct()
    end

    end


    function buttonClicked()
    blau.setPositionSmooth({-2.74, 3.84, -4.93})
    end


    I would be very happy if you can help me.
    Thanks in advance and kind regards
    Martin

  2. #2
    I am not really a great scripter.. so maybe I am wrong.. but I think the problem here is that you are not actually testing anything here... and you seam to have a bunc of stuff outside of the function in onload for some reason.

    onLoad happens EVERYtime you load the mod, when you do an undo (ctrl+z), which just loads a auto save and when you take something out of a Bag or Deck.... I "think" you only want this to happen when you click the button?

    try this...

    Code:
    --[[
    	Create Buttons!
    	This is a code trick I use for buttons. Each button is stored in a table and the use of buttonIndex handeling the index 
    	values means you can copy and paste, move and add buttons at will and not need to write any new code.
    	Also it is a good idea to name things in a clearer way than this so you can read it better at another time!
    	"button_parameters" and "buttonClicked" will be hard to read if you ever have more buttons!
    ]]
    buttonIndex = 1
    button_parameters = {}
    	button_parameters.click_function = 'buttonClicked'
    	button_parameters.function_owner = nil
    	button_parameters.label = 'press me'
    	button_parameters.position = {0,0.8,0.5}
    	button_parameters.rotation = {0,0,0}
    	button_parameters.width = 600
    	button_parameters.height = 250
    	button_parameters.fontsize = 100
    table.insert(buttons, button_parameters)
    buttonIndex = buttonIndex + 1
    
    function onload()
    --This is a trick I learnt to make buttons. This one code will create ALL buttons listed above
    --No matter how many you add or change.
        for _, button in ipairs(buttons) do
    		self.createButton(button)
        end
    end
    
    function buttonClicked()
    --These are all just setting a variabe to an object. They can go up the top if global to this scritt, 
    --but local if only used in this function. I'll just put them in here for now.
    	local blau = getObjectFromGUID('70e8d5')
    	local checker = getObjectFromGUID('2bb802')
    	local tilebig = getObjectFromGUID('1f05f5')
    	
    --Now you use this bit to set the names.
    	blau.setName('Blue')
    	checker.setName('checko')
    	tilebig.setName('groeFliese')
    
    --Here is the replacment for the IF statment.
    	tilebig.destruct()
    	blau.setPositionSmooth({-2.74, 3.84, -4.93}) -- sets blau's position
    
    end -- End of buttonClicked()
    Basically you do not "need" an if statement, as far as I can tell. You just want to destroy the object as another one moves.. so all you need to do is MOVE and DESTROY.. so no need for a if statement at all.


    So clicking this button will..

    • Set the names of the objects
    • move blau to your cords
    • delete tilebag.

    Anyway.. when doing a "IF" statment you need to test "IF SOMETHING IS TRUE OR FALSE"...

    So for example to test if an object is in the same position as another miht be somthing like

    Code:
    if blau.getPosition() == tilebig.getPosition() then
    	do stuff here
    end
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  3. #3
    Hello,

    thank you for your help!
    I'm not able to test the script now, but i will do in the afternoon.
    The explanation for the if statement was, that i wanted to seperate the button click function (--> moving of blau) and the effect wich is triggered by this. Just because several buttons shall move blau to a certain position, but there is an error if several buttons will trigger the destruction of an object wich is yet destroyed.
    In fact i want to make a game, where the player figurine can be moved by buttons and if the figurine is entering a room an object should be destroyed. Several buttons can move a figurine to the same position (its a labyrinth) but if every button would have also the function to destroy a certain object wich is destroyed before an error occurs.

    Kind regards
    Martin

  4. #4
    Hi again,

    i tested your script and it moved the object blau of course but it did not destroy the tilebig. I'm wondering why and dont get any clou about it oO


    button_parameters = {}
    button_parameters.click_function = 'buttonClicked'
    button_parameters.function_owner = nil
    button_parameters.label = 'press me'
    button_parameters.position = {0,0.8,0.5}
    button_parameters.rotation = {0,0,0}
    button_parameters.width = 600
    button_parameters.height = 250
    button_parameters.fontsize = 100


    function onload()
    blau = getObjectFromGUID('70e8d5')
    checker = getObjectFromGUID('2bb802')
    tilebig = getObjectFromGUID('1f05f5')
    blau.setName('Blue')
    checker.setName('checko')
    tilebig.setName('großeFliese')
    checker.createButton(button_parameters)
    if blau.getPosition() == tilebig.getPosition() then
    tilebig.destruct()
    end
    end


    function buttonClicked()
    blau.setPositionSmooth({-3.03, 2.96, -5.02}) -- This is the Position of tilebig
    end


    EDIT: other functions for "then do..." as for example print('hi') also dont work

    kind regards
    Martin

  5. #5
    The if-statement is only called once, when the game is loaded. At this point blau and tilebig don't have the same position, so nothing happens. You will have to test the positions, every time you change them. Try putting the if-statement into a function and call that at the end of buttonClicked.

    I'd also suggest reading some tutorials on Lua scripting. You have a much easier time, once you understand how programs are executed.

  6. #6
    Also, you can't compare tables directly, something like obj1.getPosition() == obj2.getPosition() will always return false. This page has some info on relational operators: https://www.lua.org/pil/3.2.html

  7. #7
    Hi,

    thank u all.

    I tried some beginner guides and i walk trough them. But its quite hard for me to understand everything. I tried the beginner guide on Steam but i dont get all the stuff.

    Kind regards
    Martin

Similar Threads

  1. [ADDED] [v9.6] Moving Custom objects to your hand, like cards
    By TheRaven81 in forum Suggestions
    Replies: 2
    Last Post: 08-10-2017, 02:20 PM
  2. Replies: 2
    Last Post: 05-17-2017, 02:59 AM
  3. Moving multiple mods/saved objects in a folder at once
    By Unreal_Ed in forum Suggestions
    Replies: 5
    Last Post: 03-09-2017, 09:57 PM
  4. Replies: 5
    Last Post: 07-21-2015, 12:21 PM
  5. Moving objects through ruins? Also a few questions
    By Gadget_Steamhoof in forum General Discussion
    Replies: 4
    Last Post: 01-30-2015, 01:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •