Position binding to the character depending on its scale [SOLVED]

  • #1, by red363Tuesday, 30. August 2022, 00:56 A year ago
    Hi!

    Can you please tell me if it is possible to attach particles or light sources to a character, regardless of its size in a particular scene? Those. something like:

    local c = Characters["name"]

    shaderActivateLighting(1)
    shaderLamp(0, 1, {1000,500,1}, {0,0,0}, {0.01,0.005,0}, {0,0,0}, {1,1,1}, 1)
    bind("light1", "lights[0].position",
    shaderPoint(inverty(scrollfix(offset(field("Characters.name.Position"),{0+c.Size.x\3, 0+c.Size.y\2}))),10))

    --place a dot in the middle of the character sprite at Y , and 1/3 at X, at any sprite size. The idea is, for example, if the character carries a torch, a lantern, etc.

    Of course, I can assign new bindings from scene to scene, but if there is an easier solution, that would be great. Thank you.

    Forum Fan

    101 Posts


  • #2, by afrlmeTuesday, 30. August 2022, 13:13 A year ago
    First off, I'm not going to be providing you with a solution as I still haven't learnt how to use the bind() function, I tend to use mainLoop instead if I want to dynamically loop lighting things as I find it much easier to read than the bind function.

    Anyway, I'm just going to break a few things down for you & maybe you will be able to figure out the calculations you need to make to get it to work yourself as math is something I am not very good at.

    game.CurrentCharacter.Size or Characters["Tom"].Size equals the current percentage scale value of the character & not the height & width of the character in pixels, to do that you would use something like this: graphics.getAnimationSize(game.CurrentCharacter).

    game.CurrentCharacter.Position or Characters["Tom"].Position is the coordinate on the character that you defined the animation center, which is usually placed somewhere down near the feet, so you need to take that into consideration when you are wanting to offset from it, especially if the animation center coordinate is not the same value in each of the animations belonging to same character.

    What I believe you actually need to do is figure out the x & y offset value that you would want to use if the character scale was at 100% & then you would do some math calculations against that based on the current character scale to get the adjusted offset, which I think (but I could be wrong - I'm bad at math remember) would be something along the lines of this...
    x = offset x / 100 * character size -- only if you want to offset x
    
    y = offset y  / 100 * character size

    Anyway, I think you should have enough information now to probably figure it out yourself.

    P.S: in regards to particles you should also be able to reposition them with scripting too, but they would be linked to the scene they are assigned to. I don't know if it's possible to change the scene or object they are assigned to.

    local p = graphics.getParticles(game.CurrentScene.Objects["example"])
    
    p.center = {game.CurrentCharacter.Position.x, game.CurrentCharacter.Position.y - 50}


    Imperator

    7278 Posts

  • #3, by red363Tuesday, 30. August 2022, 23:07 A year ago
    Ok, thanks for the reply, I'll try to figure it out.

    Forum Fan

    101 Posts

  • #4, by red363Wednesday, 31. August 2022, 04:54 A year ago
    Here is a solution that seems to work (in this case, the lamp is in the middle of the game character):

    function name()
    shaderActivateLighting(1)
    local c = game.CurrentCharacter
    shaderLamp(0, 1, {c.Position.x,c.Position.y-c.Size*5,1}, {0,0,1}, {0.0,0.0,0.0005}, {0,0,0}, {1,1,1}, 1)
    end
    registerEventHandler("mainLoop", "name")

    @Afrlme, another question, what command to cancel this action if necessary (exit to the menu, etc.)? shaderDeactivateLighting() does not work in this case. Thanks.

    Forum Fan

    101 Posts

  • #5, by afrlmeWednesday, 31. August 2022, 12:16 A year ago
    script section: definition type script
    local c = game.CurrentCharacter
    
    
    
    function initLamp()
    
     shaderActivateLighting(1)
    
     registerEventHandler("mainLoop", "Lamp")
    
    end
    
    
    
    function Lamp()
    
     shaderLamp(0, 1, {c.Position.x,c.Position.y-c.Size*5,1}, {0,0,1}, {0.0,0.0,0.0005}, {0,0,0}, {1,1,1}, 1)
    
    end
    
    
    
    function killLamp()
    
     shaderDeactivateLighting()
    
     unregisterEventHandler("mainLoop", "Lamp")
    
    end

    execute a script >

    initLamp() -- start lamp


    execute a script >

    killLamp() -- stop lamp

    The reason it wouldn't end with shaderDeactivateLighting() is because you were turning the lighting back on inside of the function you were looping.

    Imperator

    7278 Posts

  • #6, by red363Wednesday, 31. August 2022, 23:28 A year ago
    thank you very much

    Forum Fan

    101 Posts

  • #7, by afrlmeThursday, 01. September 2022, 02:16 A year ago
    Sorry, updated the killLamp() function. I made a small typo. Was supposed to unregister the loop not register it, oops!

    Imperator

    7278 Posts

  • #8, by red363Saturday, 03. September 2022, 04:25 A year ago
    Yes thanks, I figured it out. I just use unregisterEventHandler.

    Forum Fan

    101 Posts