lua->如何将这些参数修改为const,或者该怎么办? [英] lua -> how to modify these arguments to be const, or what should I do?

查看:217
本文介绍了lua->如何将这些参数修改为const,或者该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部!

我来这里是因为我有一个问题困扰了我一段时间.我正在使用love2d引擎作为lua脚本的图形"补充,但是这个问题属于lua类型(至少,我相信).

I came here because I have one problem bugging me for quite some time now. I am using love2d engine as a 'graphical' addition to lua scripting, but this problem is of lua type (I believe, at least).

我有一个功能:

createNew_keepOld = function (oldImgData, oldImgDraw)
    local newImgData = oldImgData --I am assigning old value to another variable
    local newImgDraw = oldImgDraw --I am doing the same thing as with data 

    for x = 0, newImgData:getWidth()-1 do
        for y = 0, newImgData:getHeight()-1 do
            local r, g, b, a = newImgData:getPixel(x, y)
            r = 2*r
            g = 2*g
            b = 0.5*b
            a = 2*a
            newImgData:setPixel(x, y, r, g, b, a)
        end
    end

    newImgDraw:replacePixels(newImgData)

    return newImgData, newImgDraw
end

此代码完成后,我得到了所需的更改,但在需要的位置却没有.我只想生成两个将存储数据和图像对象的新变量.但是,在此过程中,原始图像被更改了.

When this code finishes, I get the change I need, but not WHERE I want it. I just want to produce two new variables which will store data and image objects. But, in process, original image gets changed.

有什么方法可以声明:

name = function (const param, const param)

    return some_things
end

这样我就可以在不更改原始内容的情况下获得所需的输出?还是我的代码有一些我看不到的问题?

So that I get output I need without changing the original stuff? Or is there some problem with my code that I cannot see?

谢谢!

推荐答案

实际上,此问题的性质在Love2D和Lua中都存在.无论如何.

Actually the nature of this problem is both in Love2D and Lua. Anyway.

Lua参考手册5.3 的引用:

表,函数,线程和(完整)用户数据值是对象:变量实际上并不包含这些值,而只是对其的引用.赋值,参数传递和函数返回始终会操纵对此类值的引用;这些操作并不意味着任何形式的复制.

Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.

Love2D中的大多数类型都是userdata Lua类型的,因此,在将它们传递给函数时,实际上是在传递对它们的引用,因此最终要修改旧"版本.这些类型通常具有复制它们的功能.

Most types from Love2D are of userdata Lua type, so when passing them to your function you are actually passing reference to them, hence you modify the "old" versions in the very end. Those types usually have functions made for copying them.

ImageData 确实有一个,并且

ImageData does have one and Image does not, but you can do the following:

createNew_keepOld = function (oldImgData, oldImgDraw)
    local newImgData = oldImgData:clone()

    for x = 0, newImgData:getWidth()-1 do
        for y = 0, newImgData:getHeight()-1 do
            local r, g, b, a = newImgData:getPixel(x, y)
            r = 2*r
            g = 2*g
            b = 0.5*b
            a = 2*a
            newImgData:setPixel(x, y, r, g, b, a)
        end
    end

    local newImgDraw = love.graphics.newImage(newImgData, oldImgDraw:getFlags())

    return newImgData, newImgDraw
end

请注意,我基于复制的ImageData和旧标记中的图像标记创建了全新的Image.

Note that I created entirely new Image based on copied ImageData and image flags from the old one.

这篇关于lua->如何将这些参数修改为const,或者该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆