Jump to content

AggrorJorn

Members
  • Posts

    4,816
  • Joined

  • Last visited

Posts posted by AggrorJorn

    • Often starting in debug mode, can give you at least an error instead of just a crash.
    • Starting adding nil checks and print out a console warning when something is nill when it shouldn't be
  1. The source is now fixed at position vec3(0,0,1) which might not be the actual position of your helicopter. Can you upload the sound? I will try out on my pc and send you the results.

    source:SetPosition(sel.entity:GetPosition(true)) --Retrieve the position of the helicopter

    Note that if your helicopter is moving, you need to update the position of the source.

  2. 1 hour ago, grinseengel said:

    I use the FSP Player script which pretends leadwerk

    I am assuming this means 'I use the default FPS player script that comes with Leadwerks'. This script creates a listenere. So if you have antther script in your scene that also creates a listener (either by attaching the script to an object or by using main.lua) than these 2 listeners are conflicting. Try creating your sound, without creating an additional listener and move your character close to the position of the source.

  3. Are you using the default character controller like @Thirsty Panther mentioned? If you do, the character controller script already creates a listener for you. Which is then probably conflicting with the listener you made. Can you share the entire script you are using right now?

  4. If you try out the example from the documentation and swap the audio with your helicopter sound, does it work?

    https://www.leadwerks.com/learn?page=API-Reference_Object_Source_SetRange

    range = 1.5
    --Create a window
    window = Window:Create()
    context = Context:Create(window)
    
    --Load a sound
    local sound = Sound:Load("Sound/heli2.wav") 
            
    --Create a source
    source = Source:Create() 
    source:SetSound(sound) 
    sound:Release() 
    source:SetLoopMode(true)  
    source:Play() 
    source:SetPosition(Vec3(0,0,1))
    
    --Create a listener
    local listener = Listener:Create() 
    listener:SetPosition(0,0,0)
    
    while true do        
            --Press up/down keys to adjust the source range
            if (window:KeyDown(Key.Up))then range = range + Time:GetSpeed()*0.1 end
            if (window:KeyDown(Key.Down))then range =range - Time:GetSpeed()*0.1 end
            if (range < 0)then range=0 end 
            source:SetRange(range) 
    
            if window:Closed() or window:KeyHit(Key.Escape) then return false end
    
            Time:Update()
            context:SetColor(0,0,0)
            context:Clear()
    
            context:SetColor(1,1,1) 
            context:SetBlendMode(Blend.Alpha) 
            context:DrawText("Range: "..range,2,2) 
    
            context:Sync()
    
    end

     

  5. Yes, very well done. This already reads better than the very first script.

    1. So how does this script perform at the moment?
    2. Is it the intended behavior?
    3. What is the behavior you would want/expect?
    4. Do you have a video to display what is going on in your game right now?

     

  6. 4 hours ago, havenphillip said:

     It boggles my mind a bit how you can just say something like "isNegative" and go about using it without defining it anywhere. My brain will slowly absorb this.

    When a function has parameters (like the 'isNegative' example), then these do have any values automatically. They only get a value assigned to them when you call the function, and pass in existing values.

    Regardless if the calculations are correct and regardless whether this can all be done in a few lines with the right calculation, right now I am more concerned about how your code is structured. By organizing your code, we can find the solution faster. So lets try and break the code down from your very first post and imrove it step by step.


    Looking at the if statements
    Your code is currently divided in 5 if statements that check the playerY rotation. 

    image.png.5aa23c51d54ca503362223df112c3de5.png

    • Your first if statement covers the forward direction: -30 and 30. That looks good.
    • Your 2nd and 3rd if statement are for the backwards direction. These can be combined to 1 if statement. Saves you 15 lines of code.
    elseif (playerYRotation >= 150 and playerYRotation <= 180) or (playerYRotation >= -180 and playerYRotation <= -150) then

     

    DrawRect()

    Every single DrawRect you use inside the if statement does the same thing. This can be reduced to a single DrawRect() and using a single drawCoordinates variables that gets filled by 1 of the if statements. This saves you about 18 lines of code.

     

    Current status of the script

    We are not there yet, but here is how that code looks in the meantime: 36 lines shorter. Let me know if you have questions about what I have done so far. Once that is clear we will continue improving:

    Script.target = nil -- entity "Target"
    Script.dotSize = 5 -- int "Dot Size"
    
    function Script:Start()
        self.player = self.entity:GetParent()
        self.mapPos = Vec2(200, 200)
    end
    
    function Script:PostRender(context)
        local x = self.mapPos.x / 2
        local y = self.mapPos.y / 2
        local playerPos = self.player:GetPosition(true)
        local enemyPos = self.target:GetPosition(true)
        local playerYRotation = self.player:GetRotation(true).y
        local drawCoords = Vec2(0,0) --We will use this variable to store the calculated draw position 
    
        context:SetBlendMode(1)
        context:SetColor(1, 1, 1, 1)
        context:DrawRect(x, y, self.dotSize, self.dotSize)
        context:SetColor(1, 0, 0, 1)
    
        if playerYRotation <= 30 and playerYRotation >= -30 then -- facing forward
            if playerPos.x < enemyPos.x and playerPos.z < enemyPos.z then -- lower left
                drawCoords.x = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z < enemyPos.z then -- lower right
                drawCoords.x = x + (-Math:Cos(-playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(-playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x < enemyPos.x and playerPos.z > enemyPos.z then -- upper left
                drawCoords.x = x + (-Math:Cos(-playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(-playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z > enemyPos.z then -- upper right
                drawCoords.x = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            end
        elseif (playerYRotation >= 150 and playerYRotation <= 180) or (playerYRotation <= -150 and playerYRotation >= -180 ) then -- facing backward
            if playerPos.x < enemyPos.x and playerPos.z < enemyPos.z then -- lower left
                drawCoords.x = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z < enemyPos.z then -- lower right
                drawCoords.x = x + (-Math:Cos(-playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(-playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x < enemyPos.x and playerPos.z > enemyPos.z then -- upper left
                drawCoords.x = x + (-Math:Cos(-playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(-playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z > enemyPos.z then -- upper right
                drawCoords.x = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.y = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) + Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            end
        elseif playerYRotation >= 30 and playerYRotation <= 149 then -- facing right
            if playerPos.x < enemyPos.x and playerPos.z < enemyPos.z then -- lower left
                drawCoords.y = x + (Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z < enemyPos.z then -- lower right
                drawCoords.y = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (enemyPos.z - playerPos.z))
            elseif playerPos.x < enemyPos.x and playerPos.z > enemyPos.z then -- upper left
                drawCoords.y = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (enemyPos.z - playerPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z > enemyPos.z then -- upper right
                drawCoords.y = x + (Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            end
        elseif playerYRotation <= -30 and playerYRotation >= -149 then -- facing left
            if playerPos.x < enemyPos.x and playerPos.z < enemyPos.z then -- lower left
                drawCoords.y = x + (Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z < enemyPos.z then -- lower right
                drawCoords.y = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (enemyPos.z - playerPos.z))
            elseif playerPos.x < enemyPos.x and playerPos.z > enemyPos.z then -- upper left
                drawCoords.y = x + (-Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (enemyPos.z - playerPos.z))
            elseif playerPos.x > enemyPos.x and playerPos.z > enemyPos.z then -- upper right
                drawCoords.y = x + (Math:Cos(playerYRotation) * (playerPos.x - enemyPos.x) - Math:Sin(playerYRotation) * (enemyPos.x - playerPos.x))
                drawCoords.x = y + (-Math:Sin(playerYRotation) * (enemyPos.z - playerPos.z) - Math:Cos(playerYRotation) * (playerPos.z - enemyPos.z))   
            end
        end
        context:DrawRect(drawCoords.x, drawCoords.y, self.dotSize, self.dotSize)
    
        context:SetBlendMode(0)
        context:SetColor(1, 1, 1, 1)
        context:DrawText(
            Math:Round(playerPos.x) .. "," .. Math:Round(playerPos.z) .. "/" .. Math:Round(enemyPos.x) .. "," .. Math:Round(enemyPos.z),
            20,
            20
        )
        context:DrawText(playerYRotation, 20, 50)
        context:SetBlendMode(1)
    end

     

  7. Thanks for the replies @Thirsty Panther and @gamecreator.

    I am afraid the models that I bought do not have this element LayerElementColor but instead are refering to a material. There are no textures in the model pack, so I guess some other tricks are applied in other engines. Anyway, I am sure I can find some fbx batch export with color vertex somewhere for blender.

    Thanks for the help. If I find something useful, I will post back here.

  8. Okay lets first practice by making a very basic function. Lets make basic functions that returns the calculation that happens several times in your code:

    Take for instance this line: 

    local Y = y+(-Math:Sin(a)*(enemy.z-pos.z) + Math:Cos(a)*(pos.z-enemy.z))

    This line already exists 6 times in your code. So if you have an error in this calculation, you have change it 6 times. This makes your code hard to read and mistakes are easily made.

    image.thumb.png.95479a430a0ae9dac2d2231e91314033.png

    So lets try to turn that in to a basic function:

    function GetYDrawCoordinate1(y, enemy, pos)
    	return = y+(-Math:Sin(a)*(enemy.z-pos.z) + Math:Cos(a)*(pos.z-enemy.z))
    end

    This function now has 3 parameters that need to be filled in when you call it. I would also advice to rename the y and pos variable to something clearer. Something that tells you what exactly is stored in the variable.

     local Y = GetYDrawCoordinateTest1(y, enemy, pos)

     

    A generic video on how functions with parameters work:

     

     

    Let me know how that goes and we move on to the rest of the code.

    • Like 1
  9. Regardless of whether the code is right, I think your script can become a clearer/easier to read if you make use of functions. Every single if statement does more or less the same thing with only slight deviation. 

    --current first if statement
    local X = x+(-Math:Cos(-a)*(pos.x-enemy.x) - Math:Sin(-a)*(enemy.x-pos.x))
    local Y = y+(Math:Sin(a)*(enemy.z-pos.z) + Math:Cos(a)*(pos.z-enemy.z))
    context:DrawRect(X,Y,s,s)
    
    --Format to:
    if statement
      local drawCoord = GetDrawCoordinate(Vec2(X, Y), playerPos, enemyPos, true, yRotation)
      context:DrawRect(drawCoord.x, drawCoord.y, s, s)
    end
    
    --Helper function that retrieves the right DrawCoords
    function GetDrawCoordinate(mapPos, playerPos, enemyPos, isNegative, yRotation)
        local dir = 1;
        if isNegative then
            dir  = -1
        end
    
        local x = mapPos.x+(dir * Math:Cos(dir * yRotation)*(playerPos.x-enemy.x) - Math:Sin(dir * yRotation)*(enemyPos.x-playerPos.x))
        local y = mapPos.y+(-Math:Sin(yRotation)*(enemyPos.z-playerPos.z) + Math:Cos(yRotation)*(playerPos.z-enemyPos.z))
        return Vec2(x,y)
    end

     

     

     

  10. Should vertex colored fbx models work out of the box? 

    I have a collection (+100) models that are vertex colored instead of having any textures. Loading them in using the editor, they remain grey.  I tried to see if would require some sort of default empty material but to no avail. Opening them in other engines works directly.

    Does anyone have experience with this? This topic mentions altering some info in blender, but I am looking to see I can achieve this without actually altering all 100+ models in an external modeling program.  

  11. 5 hours ago, tipforeveryone said:

    Script 2 does not work, because there is nothing inside the global_table to execute, all Script:Start() function are started at the same time.

    How can I force the Start() function of script2 (attached to pivot3) to be executed after all others execution?

    There is no build in way to force the pivot3 scripts from being exucted first. What you can do is wait a couple of frames before calling the for loop in your pivot 3 script.

    • Thanks 1
×
×
  • Create New...